From a396cb788c20ee74075ed070294b1564b5ef6aac Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 07:47:35 -0300 Subject: [PATCH 01/12] =?UTF-8?q?#44=20=E2=80=94=20composer=20require=20co?= =?UTF-8?q?ntainer-interop/container-interop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 028c609..24d1e63 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ ], "require": { "php": "^5.3.0|^7.0", - "fig/http-message-util": "^1.1" + "fig/http-message-util": "^1.1", + "container-interop/container-interop": "^1.1" }, "require-dev": { "malukenho/docheader": "^0.1.5", From c8a9c4fd592ab475ae11a74bd0679e55a552f890 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 07:54:21 -0300 Subject: [PATCH 02/12] =?UTF-8?q?#44=20=E2=80=94=20pass=20container=20to?= =?UTF-8?q?=20Router=20via=20constructor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Router.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Router.php b/src/Router.php index e9bd478..a7bf2c8 100755 --- a/src/Router.php +++ b/src/Router.php @@ -18,7 +18,12 @@ namespace PHPRouter; use Exception; +<<<<<<< a396cb788c20ee74075ed070294b1564b5ef6aac use Fig\Http\Message\RequestMethodInterface; +======= +use Interop\Container\ContainerInterface; +use PHPRouter\RouteCollection; +>>>>>>> #44 — pass container to Router via constructor /** * Routing class to match request URL's against given routes and map them to a controller action. @@ -45,9 +50,15 @@ class Router private $basePath = ''; /** - * @param RouteCollection $collection + * @var ContainerInterface */ - public function __construct(RouteCollection $collection) + private $container; + + /** + * @param RouteCollection $collection + * @param ContainerInterface $container + */ + public function __construct(RouteCollection $collection, ContainerInterface $container) { $this->routes = $collection; @@ -57,6 +68,8 @@ public function __construct(RouteCollection $collection) $this->namedRoutes[$name] = $route; } } + + $this->container = $container; } /** From e6214e3bed221a8fabd2cc9f822a771df8597aae Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 07:54:54 -0300 Subject: [PATCH 03/12] =?UTF-8?q?#44=20=E2=80=94=20Add=20todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Router.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Router.php b/src/Router.php index a7bf2c8..66aa5db 100755 --- a/src/Router.php +++ b/src/Router.php @@ -62,6 +62,7 @@ public function __construct(RouteCollection $collection, ContainerInterface $con { $this->routes = $collection; + // @todo remove this heavy operation from construct foreach ($this->routes->all() as $route) { $name = $route->getName(); if (null !== $name) { From cce97c26ec775fc1bdfcdcc8a9ef76bce57c6a1a Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:04:31 -0300 Subject: [PATCH 04/12] =?UTF-8?q?#44=20=E2=80=94=20Make=20container=20not?= =?UTF-8?q?=20mandatory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Router.php b/src/Router.php index 66aa5db..402a4a4 100755 --- a/src/Router.php +++ b/src/Router.php @@ -58,7 +58,7 @@ class Router * @param RouteCollection $collection * @param ContainerInterface $container */ - public function __construct(RouteCollection $collection, ContainerInterface $container) + public function __construct(RouteCollection $collection, ContainerInterface $container = null) { $this->routes = $collection; From 4d8918a0ec7980ac30f6d8bf1cab124f60450460 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:09:36 -0300 Subject: [PATCH 05/12] =?UTF-8?q?#44=20=E2=80=94=20Work=20with=20container?= =?UTF-8?q?=20Interop=20interface=20for=20DI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Route.php | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Route.php b/src/Route.php index 16bf99e..cb15792 100755 --- a/src/Route.php +++ b/src/Route.php @@ -19,6 +19,7 @@ use Fig\Http\Message\RequestMethodInterface; use Exception; +use Interop\Container\ContainerInterface; class Route { @@ -75,12 +76,24 @@ class Route */ private $config; + /** + * @var ContainerInterface + */ + private $container; + + /** + * @var string + */ + private $action; + /** * @param $resource * @param array $config */ public function __construct($resource, array $config) { + + // @todo get action and controller when create the object instance $this->url = $resource; $this->config = $config; $this->methods = isset($config['methods']) ? (array) $config['methods'] : array(); @@ -89,6 +102,11 @@ public function __construct($resource, array $config) $this->parameters = isset($config['parameters']) ? $config['parameters'] : array(); } + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + } + public function getUrl() { return $this->url; @@ -187,13 +205,24 @@ public function setParameters(array $parameters) public function dispatch() { - $action = explode('::', $this->config['_controller']); + list($controller, $action) = explode('::', $this->config['_controller']); + + $this->action = !$action && trim($action) !== '' ? $action : null; if ($this->parametersByName) { $this->parameters = array($this->parameters); } - $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null; + if ($this->container && $this->container->has($controller)) { + $instance = $this->container->get($controller); + call_user_func_array( + // @todo action seems to be inconsistent + array($instance, $this->action), + $this->parameters + ); + + return; + } if (!is_null($this->action)) { $instance = new $action[0]; From 7fb11a89587baaa5c7f5d8e8faeddc8dbb62d4e9 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:17:59 -0300 Subject: [PATCH 06/12] =?UTF-8?q?#44=20=E2=80=94=20Fix=20test=20error=20du?= =?UTF-8?q?e=20missing=20SomeController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index 24d1e63..e72a6b5 100644 --- a/composer.json +++ b/composer.json @@ -33,5 +33,10 @@ "psr-4": { "PHPRouter\\": "src/" } + }, + "autoload-dev": { + "classmap": [ + "tests/Fixtures/SomeController.php" + ] } } From d1ce0d99a3fc1a20dc53e75fedef1fcfab818406 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:20:53 -0300 Subject: [PATCH 07/12] =?UTF-8?q?#44=20=E2=80=94=20Guard=20controller=20an?= =?UTF-8?q?d=20action=20name=20when=20instantiate=20the=20Route=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Route.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Route.php b/src/Route.php index cb15792..8ccf461 100755 --- a/src/Route.php +++ b/src/Route.php @@ -86,16 +86,24 @@ class Route */ private $action; + /** + * @var string + */ + private $controller; + /** * @param $resource * @param array $config */ public function __construct($resource, array $config) { + $this->url = $resource; + $this->config = $config; + + list($controller, $action) = explode('::', $this->config['_controller']); - // @todo get action and controller when create the object instance - $this->url = $resource; - $this->config = $config; + $this->controller = $controller; + $this->action = $action; $this->methods = isset($config['methods']) ? (array) $config['methods'] : array(); $this->target = isset($config['target']) ? $config['target'] : null; $this->name = isset($config['name']) ? $config['name'] : null; @@ -205,35 +213,32 @@ public function setParameters(array $parameters) public function dispatch() { - list($controller, $action) = explode('::', $this->config['_controller']); - - $this->action = !$action && trim($action) !== '' ? $action : null; - if ($this->parametersByName) { $this->parameters = array($this->parameters); } + $controller = $this->controller; + if ($this->container && $this->container->has($controller)) { $instance = $this->container->get($controller); call_user_func_array( - // @todo action seems to be inconsistent - array($instance, $this->action), + array($instance, $this->getAction()), $this->parameters ); return; } - if (!is_null($this->action)) { - $instance = new $action[0]; - call_user_func_array(array($instance, $this->action), $this->parameters); + if (!is_null($this->getAction())) { + $instance = new $controller; + call_user_func_array(array($instance, $this->getAction()), $this->parameters); } else { - $instance = new $action[0]($this->parameters); + $instance = new $controller($this->parameters); } } public function getAction() { - return $this->action; + return '' !== trim($this->action) ? $this->action : null; } } From 44a25948e91b071bbe53a30cdad5f1afc81a1582 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:37:02 -0300 Subject: [PATCH 08/12] =?UTF-8?q?#44=20=E2=80=94=20Added=20router=20tests?= =?UTF-8?q?=20for=20use=20of=20Di=20containers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/src/PHPRouterTest/RouteTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/src/PHPRouterTest/RouteTest.php b/tests/src/PHPRouterTest/RouteTest.php index e43a18e..5dbe0d2 100644 --- a/tests/src/PHPRouterTest/RouteTest.php +++ b/tests/src/PHPRouterTest/RouteTest.php @@ -18,10 +18,14 @@ namespace PHPRouterTest\Test; use PHPRouter\Route; +use PHPRouter\Test\SomeController; use PHPUnit_Framework_TestCase; class RouteTest extends PHPUnit_Framework_TestCase { + /** + * @var Route + */ private $routeWithParameters; protected function setUp() @@ -93,4 +97,24 @@ public function testGetAction() { self::assertEquals('page', $this->routeWithParameters->getAction()); } + + public function testShouldGetInstanceFromContainerIfContainerIsProvided() + { + /* @var $container \PHPUnit_Framework_MockObject_MockObject|\Interop\Container\ContainerInterface */ + $container = $this->getMock('Interop\Container\ContainerInterface'); + + $container->expects(self::once()) + ->method('has') + ->with('PHPRouter\Test\SomeController') + ->willReturn(true); + + $container->expects(self::once()) + ->method('get') + ->with('PHPRouter\Test\SomeController') + ->willReturn(new SomeController()); + + $this->routeWithParameters->setContainer($container); + + $this->routeWithParameters->dispatch(); + } } From 7e0d58c20bbb5012317b1c78e7cd43db87e4e7e2 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:47:53 -0300 Subject: [PATCH 09/12] =?UTF-8?q?#44=20=E2=80=94=20Remove=20duplicated=20c?= =?UTF-8?q?all=5Fuser=5Ffunc=5Farray=5Fcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Route.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Route.php b/src/Route.php index 8ccf461..0a11cff 100755 --- a/src/Route.php +++ b/src/Route.php @@ -20,6 +20,7 @@ use Fig\Http\Message\RequestMethodInterface; use Exception; use Interop\Container\ContainerInterface; +use SebastianBergmann\GlobalState\RuntimeException; class Route { @@ -213,6 +214,12 @@ public function setParameters(array $parameters) public function dispatch() { + if (null === $this->getAction()) { + throw new RuntimeException( + sprintf('Could not find an action to controller "%s"', $this->controller) + ); + } + if ($this->parametersByName) { $this->parameters = array($this->parameters); } @@ -221,6 +228,11 @@ public function dispatch() if ($this->container && $this->container->has($controller)) { $instance = $this->container->get($controller); + } else { + $instance = new $controller; + } + + if (method_exists($this, $this->getAction())){ call_user_func_array( array($instance, $this->getAction()), $this->parameters @@ -229,12 +241,7 @@ public function dispatch() return; } - if (!is_null($this->getAction())) { - $instance = new $controller; - call_user_func_array(array($instance, $this->getAction()), $this->parameters); - } else { - $instance = new $controller($this->parameters); - } + new $controller($this->parameters); } public function getAction() From 26916ac364062c8130cae5e75fdce83cab5776a0 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 19 Dec 2016 08:58:51 -0300 Subject: [PATCH 10/12] =?UTF-8?q?#44=20=E2=80=94=20Test=20throw=20exceptio?= =?UTF-8?q?n=20in=20case=20of=20null=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/src/PHPRouterTest/RouteTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/src/PHPRouterTest/RouteTest.php b/tests/src/PHPRouterTest/RouteTest.php index 5dbe0d2..2587b0f 100644 --- a/tests/src/PHPRouterTest/RouteTest.php +++ b/tests/src/PHPRouterTest/RouteTest.php @@ -117,4 +117,20 @@ public function testShouldGetInstanceFromContainerIfContainerIsProvided() $this->routeWithParameters->dispatch(); } + + public function testShouldRaiseAnExceptionIfActionIsNull() + { + $route = new Route( + '/page/:page_id', + array( + '_controller' => 'PHPRouter\Test\SomeController::', + 'methods' => array('GET'), + 'target' => 'thisIsAString', + 'name' => 'page' + ) + ); + + $this->setExpectedException('\RuntimeException'); + $route->dispatch(); + } } From 0bffb023638ed43fecac88795a1e758e901788b8 Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Tue, 25 Apr 2017 05:10:30 -0300 Subject: [PATCH 11/12] add a section on readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index b33ad36..f970f21 100755 --- a/README.md +++ b/README.md @@ -120,6 +120,16 @@ $router = Router::parseConfig($config); $router->matchCurrentRequest(); ``` +## Using a Service Container + +PHPRouter provides a way to work with Containers just by passing it via `PHPRouter\Router` constructor. + +``` + Date: Tue, 25 Apr 2017 05:10:49 -0300 Subject: [PATCH 12/12] fixes for git conflict --- src/Route.php | 8 ++++++-- src/Router.php | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Route.php b/src/Route.php index 0a11cff..dcaf1ed 100755 --- a/src/Route.php +++ b/src/Route.php @@ -31,10 +31,14 @@ class Route private $url; /** - * Accepted HTTP methods for this route. * @var string[] */ - private $methods = array( + private $methods; + + /** + * @var string[] + */ + private $httpMethods = array( RequestMethodInterface::METHOD_GET, RequestMethodInterface::METHOD_POST, RequestMethodInterface::METHOD_PUT, diff --git a/src/Router.php b/src/Router.php index 402a4a4..86d1e47 100755 --- a/src/Router.php +++ b/src/Router.php @@ -18,12 +18,8 @@ namespace PHPRouter; use Exception; -<<<<<<< a396cb788c20ee74075ed070294b1564b5ef6aac use Fig\Http\Message\RequestMethodInterface; -======= use Interop\Container\ContainerInterface; -use PHPRouter\RouteCollection; ->>>>>>> #44 — pass container to Router via constructor /** * Routing class to match request URL's against given routes and map them to a controller action.