diff options
author | minstel <minstel@yandex.ru> | 2016-10-17 13:52:32 +0300 |
---|---|---|
committer | minstel <minstel@yandex.ru> | 2016-10-17 13:52:32 +0300 |
commit | 7d4be81777f3a9afcbc366e528bf2284265c16b6 (patch) | |
tree | 7237097fbfc379ee9c3bf6df73f4c0322d29a796 | |
parent | f9df2a343453c80ac982cb46af60c7b40ace74fc (diff) | |
download | router-7d4be81777f3a9afcbc366e528bf2284265c16b6.zip router-7d4be81777f3a9afcbc366e528bf2284265c16b6.tar.gz router-7d4be81777f3a9afcbc366e528bf2284265c16b6.tar.bz2 |
Add the route as ServerRequest attribute
-rw-r--r-- | src/Router.php | 1 | ||||
-rw-r--r-- | src/Router/Runner.php | 29 | ||||
-rw-r--r-- | src/Router/Runner/Callback.php | 3 | ||||
-rw-r--r-- | src/Router/Runner/Controller.php | 5 | ||||
-rw-r--r-- | src/Router/Runner/PhpScript.php | 15 | ||||
-rw-r--r-- | tests/Router/Runner/CallbackTest.php | 4 | ||||
-rw-r--r-- | tests/Router/Runner/ControllerTest.php | 2 | ||||
-rw-r--r-- | tests/Router/Runner/PhpScriptTest.php | 6 | ||||
-rw-r--r-- | tests/Router/RunnerTest.php | 3 | ||||
-rw-r--r-- | tests/RouterTest.php | 17 |
10 files changed, 32 insertions, 53 deletions
diff --git a/src/Router.php b/src/Router.php index 955e1e1..7b16381 100644 --- a/src/Router.php +++ b/src/Router.php @@ -125,6 +125,7 @@ class Router if (!$route) return $this->notFound($response); + $request->withAttribute('route', $route); $runner = Runner::create($route); return $runner($request, $response, $next); diff --git a/src/Router/Runner.php b/src/Router/Runner.php index 51300a4..35fd9b9 100644 --- a/src/Router/Runner.php +++ b/src/Router/Runner.php @@ -10,32 +10,7 @@ use Jasny\Router\Route; * A runner can be invoked in order to run the action specified in a route */ abstract class Runner -{ - /** - * @var \stdClass - */ - protected $route; - - /** - * Class constructor - * - * @param \stdClass $route - */ - public function __construct(\stdClass $route) - { - $this->route = $route; - } - - /** - * Get runner route - * - * @return Route - */ - public function getRoute() - { - return $this->route; - } - +{ /** * Invoke the action specified in the route * @@ -83,7 +58,7 @@ abstract class Runner throw new \RuntimeException("Route has neither 'controller', 'fn' or 'file' defined"); } - return new $class($route); + return new $class(); } } diff --git a/src/Router/Runner/Callback.php b/src/Router/Runner/Callback.php index 34ebc4c..7f4c457 100644 --- a/src/Router/Runner/Callback.php +++ b/src/Router/Runner/Callback.php @@ -22,7 +22,8 @@ class Callback extends Runner */ public function run(ServerRequestInterface $request, ResponseInterface $response) { - $callback = !empty($this->route->fn) ? $this->route->fn : null; + $route = $request->getAttribute('route'); + $callback = !empty($route->fn) ? $route->fn : null; if (!is_callable($callback)) { throw new \RuntimeException("'fn' property of route shoud be a callable"); diff --git a/src/Router/Runner/Controller.php b/src/Router/Runner/Controller.php index 0488be5..56cf2b5 100644 --- a/src/Router/Runner/Controller.php +++ b/src/Router/Runner/Controller.php @@ -22,7 +22,8 @@ class Controller extends Runner */ public function run(ServerRequestInterface $request, ResponseInterface $response) { - $class = !empty($this->route->controller) ? $this->route->controller : null; + $route = $request->getAttribute('route'); + $class = !empty($route->controller) ? $route->controller : null; if (!class_exists($class)) { throw new \RuntimeException("Can not route to controller '$class': class not exists"); @@ -32,7 +33,7 @@ class Controller extends Runner throw new \RuntimeException("Can not route to controller '$class': class does not have '__invoke' method"); } - $controller = new $class($this->route); + $controller = new $class($route); return $controller($request, $response); } diff --git a/src/Router/Runner/PhpScript.php b/src/Router/Runner/PhpScript.php index f6595a3..d223bd7 100644 --- a/src/Router/Runner/PhpScript.php +++ b/src/Router/Runner/PhpScript.php @@ -10,17 +10,7 @@ use Psr\Http\Message\ResponseInterface; * Route to a PHP script */ class PhpScript extends Runner -{ - /** - * Return route file path - * - * @return string - */ - public function __toString() - { - return (string)$this->route->file; - } - +{ /** * Route to a file * @@ -30,7 +20,8 @@ class PhpScript extends Runner */ public function run(ServerRequestInterface $request, ResponseInterface $response) { - $file = !empty($this->route->file) ? ltrim($this->route->file, '/') : ''; + $route = $request->getAttribute('route'); + $file = !empty($route->file) ? ltrim($route->file, '/') : ''; if (!file_exists($file)) { throw new \RuntimeException("Failed to route using '$file': File '$file' doesn't exist."); diff --git a/tests/Router/Runner/CallbackTest.php b/tests/Router/Runner/CallbackTest.php index b4ee4ef..8a31794 100644 --- a/tests/Router/Runner/CallbackTest.php +++ b/tests/Router/Runner/CallbackTest.php @@ -15,12 +15,12 @@ class CallbackTest extends PHPUnit_Framework_TestCase * @param boolean $positive */ public function testCallback($route, $positive) - { + { $runner = new Callback($route); - $this->assertEquals($route, $runner->getRoute(), "Route was not set correctly"); $request = $this->createMock(ServerRequestInterface::class); $response = $this->createMock(ResponseInterface::class); + $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route)); if (!$positive) $this->expectException(\RuntimeException::class); $result = $runner->run($request, $response); diff --git a/tests/Router/Runner/ControllerTest.php b/tests/Router/Runner/ControllerTest.php index 1d64622..433d36d 100644 --- a/tests/Router/Runner/ControllerTest.php +++ b/tests/Router/Runner/ControllerTest.php @@ -23,10 +23,10 @@ class ControllerTest extends PHPUnit_Framework_TestCase public function testPhpScript($route, $positive) { $runner = new Controller($route); - $this->assertEquals($route, $runner->getRoute(), "Route was not set correctly"); $request = $this->createMock(ServerRequestInterface::class); $response = $this->createMock(ResponseInterface::class); + $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route)); if (!$positive) $this->expectException(\RuntimeException::class); $result = $runner->run($request, $response); diff --git a/tests/Router/Runner/PhpScriptTest.php b/tests/Router/Runner/PhpScriptTest.php index 12b9219..2f31f73 100644 --- a/tests/Router/Runner/PhpScriptTest.php +++ b/tests/Router/Runner/PhpScriptTest.php @@ -17,18 +17,14 @@ class PhpScriptTest extends PHPUnit_Framework_TestCase public function testPhpScript($route, $positive) { $runner = new PhpScript($route); - $this->assertEquals($route, $runner->getRoute(), "Route was not set correctly"); $request = $this->createMock(ServerRequestInterface::class); $response = $this->createMock(ResponseInterface::class); + $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route)); if (!$positive) $this->expectException(\RuntimeException::class); $result = $runner->run($request, $response); - if (!$positive) return; - - $this->assertEquals($runner->getRoute()->file, (string)$runner); - if ($route->type === 'returnTrue') { $this->assertEquals($response, $result, "Request object was not returned as result"); } else { diff --git a/tests/Router/RunnerTest.php b/tests/Router/RunnerTest.php index 60b80b9..2b5cd07 100644 --- a/tests/Router/RunnerTest.php +++ b/tests/Router/RunnerTest.php @@ -24,10 +24,7 @@ class RunnerTest extends PHPUnit_Framework_TestCase $runner = Runner::create($route); - if (!$positive) return; - $this->assertInstanceOf($class, $runner, "Runner object has invalid class"); - $this->assertEquals($route, $runner->getRoute(), "Route was not set correctly"); } /** diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 9b63360..5b2f281 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -53,6 +53,8 @@ class RouterTest extends PHPUnit_Framework_TestCase ]; list($request, $response) = $this->getRequests(); + $this->expectRequestRoute($request, $routes['/foo']); + $router = new Router($routes); $result = $router($request, $response); @@ -73,6 +75,8 @@ class RouterTest extends PHPUnit_Framework_TestCase ]; list($request, $response) = $this->getRequests(); + $this->expectRequestRoute($request, $routes['/foo']); + $router = new Router($routes); $result = $router($request, $response, function($arg1, $arg2) { return ['request' => $arg1, 'response' => $arg2]; @@ -154,6 +158,8 @@ class RouterTest extends PHPUnit_Framework_TestCase ]; list($request, $response) = $this->getRequests(); + $this->expectRequestRoute($request, $routes['/foo']); + $router = new Router($routes); $router->add([$this, 'getMiddlewareCalledLast'])->add([$this, 'getMiddlewareCalledFirst']); @@ -224,4 +230,15 @@ class RouterTest extends PHPUnit_Framework_TestCase $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf()); $response->expects($this->once())->method('withStatus')->with($this->equalTo(404), $this->equalTo('Not Found'))->will($this->returnSelf()); } + + /** + * Expect that request will return given route + * + * @param ServerRequestInterface $request + * @param Route $route + */ + public function expectRequestRoute(ServerRequestInterface $request, $route) + { + $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route)); + } } |