Legacy URL Support

This commit is contained in:
Igor Scheller 2018-03-31 05:19:49 +02:00
parent 9ca36bed34
commit 6962c2b790
10 changed files with 154 additions and 15 deletions

View File

@ -43,6 +43,9 @@ return [
'1' => 'Engelsystem dark' '1' => 'Engelsystem dark'
], ],
// Rewrite URLs with mod_rewrite
'rewrite_urls' => true,
// Number of News shown on one site // Number of News shown on one site
'display_news' => 6, 'display_news' => 6,

View File

@ -0,0 +1,25 @@
<?php
namespace Engelsystem\Routing;
class LegacyUrlGenerator extends UrlGenerator
{
/**
* @param string $path
* @param array $parameters
* @return string
*/
public function to($path, $parameters = [])
{
$page = ltrim($path, '/');
if (!empty($page)) {
$page = str_replace('-', '_', $page);
$parameters = array_merge(['p' => $page], $parameters);
}
$uri = parent::to('index.php', $parameters);
$uri = preg_replace('~(/index\.php)+~', '/index.php', $uri);
return $uri;
}
}

View File

@ -8,7 +8,14 @@ class RoutingServiceProvider extends ServiceProvider
{ {
public function register() public function register()
{ {
$urlGenerator = $this->app->make(UrlGenerator::class); $config = $this->app->get('config');
$class = UrlGenerator::class;
if (!$config->get('rewrite_urls', true)) {
$class = LegacyUrlGenerator::class;
}
$urlGenerator = $this->app->make($class);
$this->app->instance('routing.urlGenerator', $urlGenerator); $this->app->instance('routing.urlGenerator', $urlGenerator);
$this->app->bind(UrlGeneratorInterface::class, 'routing.urlGenerator');
} }
} }

View File

@ -2,7 +2,7 @@
namespace Engelsystem\Routing; namespace Engelsystem\Routing;
class UrlGenerator class UrlGenerator implements UrlGeneratorInterface
{ {
/** /**
* @param string $path * @param string $path

View File

@ -0,0 +1,13 @@
<?php
namespace Engelsystem\Routing;
interface UrlGeneratorInterface
{
/**
* @param string $path
* @param array $parameters
* @return string
*/
public function to($path, $parameters = []);
}

View File

@ -5,7 +5,7 @@ use Engelsystem\Application;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
/** /**
@ -114,7 +114,7 @@ function session($key = null, $default = null)
/** /**
* @param string $path * @param string $path
* @param array $parameters * @param array $parameters
* @return UrlGenerator|string * @return UrlGeneratorInterface|string
*/ */
function url($path = null, $parameters = []) function url($path = null, $parameters = [])
{ {

View File

@ -7,7 +7,7 @@ use Engelsystem\Config\Config;
use Engelsystem\Container\Container; use Engelsystem\Container\Container;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
@ -171,8 +171,7 @@ class HelpersTest extends TestCase
*/ */
public function testUrl() public function testUrl()
{ {
$urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) $urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
->getMock();
$this->getAppMock('routing.urlGenerator', $urlGeneratorMock); $this->getAppMock('routing.urlGenerator', $urlGeneratorMock);
$this->assertEquals($urlGeneratorMock, url()); $this->assertEquals($urlGeneratorMock, url());

View File

@ -0,0 +1,54 @@
<?php
namespace Engelsystem\Test\Unit\Routing;
use Engelsystem\Application;
use Engelsystem\Container\Container;
use Engelsystem\Http\Request;
use Engelsystem\Routing\LegacyUrlGenerator;
use Engelsystem\Routing\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase;
class LegacyUrlGeneratorTest extends TestCase
{
public function provideLinksTo()
{
return [
['/', 'http://foo.bar/index.php', [], 'http://foo.bar/index.php'],
['/foo-path', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo_path'],
['/foo', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo'],
['foo', 'http://foo.bar/index.php', ['test' => 'abc'], 'http://foo.bar/index.php?p=foo&test=abc'],
];
}
/**
* @dataProvider provideLinksTo
* @covers \Engelsystem\Routing\LegacyUrlGenerator::to
*
* @param string $urlToPath
* @param string $willReturn
* @param string[] $arguments
* @param string $expectedUrl
*/
public function testTo($urlToPath, $willReturn, $arguments, $expectedUrl)
{
$app = new Container();
Application::setInstance($app);
$request = $this->getMockBuilder(Request::class)
->getMock();
$request->expects($this->once())
->method('getUriForPath')
->with('/index.php')
->willReturn($willReturn);
$app->instance('request', $request);
$urlGenerator = new LegacyUrlGenerator();
$this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
$url = $urlGenerator->to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url);
}
}

View File

@ -2,10 +2,13 @@
namespace Engelsystem\Test\Unit\Routing; namespace Engelsystem\Test\Unit\Routing;
use Engelsystem\Config\Config;
use Engelsystem\Routing\LegacyUrlGenerator;
use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\RoutingServiceProvider;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGenerator;
use Engelsystem\Routing\UrlGeneratorInterface;
use Engelsystem\Test\Unit\ServiceProviderTest; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject as MockObject;
class RoutingServiceProviderTest extends ServiceProviderTest class RoutingServiceProviderTest extends ServiceProviderTest
{ {
@ -14,16 +17,48 @@ class RoutingServiceProviderTest extends ServiceProviderTest
*/ */
public function testRegister() public function testRegister()
{ {
/** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */ $app = $this->getApp(['make', 'instance', 'bind', 'get']);
$urlGenerator = $this->getMockBuilder(UrlGenerator::class) /** @var MockObject|Config $config */
->getMock(); $config = $this->getMockBuilder(Config::class)->getMock();
/** @var MockObject|UrlGeneratorInterface $urlGenerator */
$urlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
/** @var MockObject|UrlGeneratorInterface $legacyUrlGenerator */
$legacyUrlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class);
$app = $this->getApp(); $config->expects($this->atLeastOnce())
->method('get')
->with('rewrite_urls')
->willReturnOnConsecutiveCalls(
true,
false
);
$this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator); $this->setExpects($app, 'get', ['config'], $config, $this->atLeastOnce());
$this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]);
$app->expects($this->atLeastOnce())
->method('make')
->withConsecutive(
[UrlGenerator::class],
[LegacyUrlGenerator::class]
)
->willReturnOnConsecutiveCalls(
$urlGenerator,
$legacyUrlGenerator
);
$app->expects($this->atLeastOnce())
->method('instance')
->withConsecutive(
['routing.urlGenerator', $urlGenerator],
['routing.urlGenerator', $legacyUrlGenerator]
);
$this->setExpects(
$app, 'bind',
[UrlGeneratorInterface::class, 'routing.urlGenerator'], null,
$this->atLeastOnce()
);
$serviceProvider = new RoutingServiceProvider($app); $serviceProvider = new RoutingServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();
$serviceProvider->register();
} }
} }

View File

@ -6,6 +6,7 @@ use Engelsystem\Application;
use Engelsystem\Container\Container; use Engelsystem\Container\Container;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGenerator;
use Engelsystem\Routing\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class UrlGeneratorTest extends TestCase class UrlGeneratorTest extends TestCase
@ -32,7 +33,6 @@ class UrlGeneratorTest extends TestCase
public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
{ {
$app = new Container(); $app = new Container();
$urlGenerator = new UrlGenerator();
Application::setInstance($app); Application::setInstance($app);
$request = $this->getMockBuilder(Request::class) $request = $this->getMockBuilder(Request::class)
@ -45,6 +45,9 @@ class UrlGeneratorTest extends TestCase
$app->instance('request', $request); $app->instance('request', $request);
$urlGenerator = new UrlGenerator();
$this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator);
$url = $urlGenerator->to($urlToPath, $arguments); $url = $urlGenerator->to($urlToPath, $arguments);
$this->assertEquals($expectedUrl, $url); $this->assertEquals($expectedUrl, $url);
} }