From 6140973ca699d4f79844d23c5563742d034fbc11 Mon Sep 17 00:00:00 2001 From: Mateusz Tymek Date: Sun, 24 Jan 2016 23:31:40 +0100 Subject: [PATCH 1/2] use UrlHelper::setBasePath instead of custom helper --- README.md | 22 +++--------------- composer.json | 2 +- src/BaseUrlMiddleware.php | 3 ++- src/UrlHelper.php | 24 -------------------- src/UrlHelperFactory.php | 29 ------------------------ test/BaseUrlMiddlewareTest.php | 5 ++--- test/UrlHelperFactoryTest.php | 41 ---------------------------------- test/UrlHelperTest.php | 21 ----------------- 8 files changed, 8 insertions(+), 139 deletions(-) delete mode 100644 src/UrlHelper.php delete mode 100644 src/UrlHelperFactory.php delete mode 100644 test/UrlHelperFactoryTest.php delete mode 100644 test/UrlHelperTest.php diff --git a/README.md b/README.md index 4a0f4f9..81f5c87 100644 --- a/README.md +++ b/README.md @@ -72,25 +72,9 @@ echo $request->getAttribute(BaseUrlMiddleware::BASE_PATH); ### Generating URLs -While `BaseUrlMiddleware` fixes the routing process, all URLs generated by your application -will likely point outside the subdirectory. `Blast\BaseUrl` comes with replacement `UrlHelper`, - -Here's how to enable it: - -```php -return [ - 'dependencies' => [ - 'factories' => [ - // setup factory - Blast\BaseUrl\UrlHelper::class => Blast\BaseUrl\UrlHelperFactory::class, - ], - 'aliases' => [ - // alias default UrlHelper with Blast\BaseUrl alternative - Zend\Expressive\Helper\UrlHelper::class => Blast\BaseUrl\UrlHelper::class, - ], - ], -]; -``` +`BaseUrlMiddleware` is able to automatically configure `UrlHelper`, so that all URLs generated +by this helper will have appropriate prefix. This will be done automatically if `UrlHelper` +is available in service container. ### Accessing assets - base path diff --git a/composer.json b/composer.json index c21f80e..6e389cc 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "phpunit/phpunit": "^4.6", "squizlabs/php_codesniffer": "^2.3", "zendframework/zend-servicemanager": "^2.6", - "zendframework/zend-expressive-helpers": "^1.2", + "zendframework/zend-expressive-helpers": "^1.4 || ^2.0", "zendframework/zend-diactoros": "^1.3", "zendframework/zend-expressive-zendviewrenderer": "^1.0" }, diff --git a/src/BaseUrlMiddleware.php b/src/BaseUrlMiddleware.php index c3a1741..77360a2 100644 --- a/src/BaseUrlMiddleware.php +++ b/src/BaseUrlMiddleware.php @@ -4,6 +4,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Zend\Expressive\Helper\UrlHelper; class BaseUrlMiddleware { @@ -84,7 +85,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res } if ($this->urlHelper) { - $this->urlHelper->setBaseUrl($baseUrl); + $this->urlHelper->setBasePath($baseUrl); } if ($this->basePathHelper) { diff --git a/src/UrlHelper.php b/src/UrlHelper.php deleted file mode 100644 index 080d2db..0000000 --- a/src/UrlHelper.php +++ /dev/null @@ -1,24 +0,0 @@ -baseUrl = $baseUrl; - } - - public function generate($route = null, array $params = []) - { - return $this->baseUrl . '/' . ltrim(parent::generate($route, $params), '/'); - } -} diff --git a/src/UrlHelperFactory.php b/src/UrlHelperFactory.php deleted file mode 100644 index f8811b1..0000000 --- a/src/UrlHelperFactory.php +++ /dev/null @@ -1,29 +0,0 @@ -has(RouterInterface::class)) { - throw new MissingRouterException(sprintf( - '%s requires a %s implementation; none found in container', - UrlHelper::class, - RouterInterface::class - )); - } - - return new UrlHelper($container->get(RouterInterface::class)); - } -} diff --git a/test/BaseUrlMiddlewareTest.php b/test/BaseUrlMiddlewareTest.php index 49d7226..7a58bf4 100644 --- a/test/BaseUrlMiddlewareTest.php +++ b/test/BaseUrlMiddlewareTest.php @@ -4,12 +4,11 @@ use Blast\BaseUrl\BasePathHelper; use Blast\BaseUrl\BaseUrlMiddleware; -use Blast\BaseUrl\UrlHelper; use PHPUnit_Framework_TestCase; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response; -use Zend\Diactoros\ServerRequest; use Zend\Diactoros\ServerRequestFactory; +use Zend\Expressive\Helper\UrlHelper; class BaseUrlMiddlewareTest extends PHPUnit_Framework_TestCase { @@ -46,7 +45,7 @@ public function testMiddlewareInjectsUrlHelperWithBaseUrl() $middleware = new BaseUrlMiddleware(); $urlHelper = $this->prophesize(UrlHelper::class); - $urlHelper->setBaseUrl('/index.php')->shouldBeCalled(); + $urlHelper->setBasePath('/index.php')->shouldBeCalled(); $middleware->setUrlHelper($urlHelper->reveal()); $middleware($request, new Response(), function (ServerRequestInterface $request) { diff --git a/test/UrlHelperFactoryTest.php b/test/UrlHelperFactoryTest.php deleted file mode 100644 index b0764e0..0000000 --- a/test/UrlHelperFactoryTest.php +++ /dev/null @@ -1,41 +0,0 @@ -container = $this->prophesize(ContainerInterface::class); - $router = $this->prophesize(RouterInterface::class); - - $this->container->get(RouterInterface::class)->willReturn($router); - } - - public function testFactory() - { - $factory = new UrlHelperFactory(); - $this->container->has(RouterInterface::class)->willReturn(true); - - $homePage = $factory($this->container->reveal()); - - $this->assertTrue($homePage instanceof UrlHelper); - } - - public function testFactoryRisesExceptionIfRouterIsNotAvailable() - { - $factory = new UrlHelperFactory(); - $this->container->has(RouterInterface::class)->willReturn(false); - $this->setExpectedException(MissingRouterException::class); - - $factory($this->container->reveal()); - } -} diff --git a/test/UrlHelperTest.php b/test/UrlHelperTest.php deleted file mode 100644 index 3748f44..0000000 --- a/test/UrlHelperTest.php +++ /dev/null @@ -1,21 +0,0 @@ -prophesize(RouterInterface::class); - $router->generateUri('route_name', [])->willReturn('/foo/bar'); - - $helper = new UrlHelper($router->reveal()); - $helper->setBaseUrl('/~user/project/public/index.php'); - $url = $helper->generate('route_name', []); - $this->assertEquals('/~user/project/public/index.php/foo/bar', $url); - } -} From 6bcee270703dfd284f02f99e0658a30c20e53d05 Mon Sep 17 00:00:00 2001 From: Mateusz Tymek Date: Mon, 25 Jan 2016 20:31:25 +0100 Subject: [PATCH 2/2] missing import --- src/BaseUrlMiddlewareFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BaseUrlMiddlewareFactory.php b/src/BaseUrlMiddlewareFactory.php index 4b87bbe..708c397 100644 --- a/src/BaseUrlMiddlewareFactory.php +++ b/src/BaseUrlMiddlewareFactory.php @@ -3,6 +3,7 @@ namespace Blast\BaseUrl; use Interop\Container\ContainerInterface; +use Zend\Expressive\Helper\UrlHelper; class BaseUrlMiddlewareFactory {