diff --git a/src/App.php b/src/App.php index 2140b4b..f68b8ae 100644 --- a/src/App.php +++ b/src/App.php @@ -55,9 +55,7 @@ public function __construct($container = []) public function run($silent = false) { //Allow routing on application level via invokable class - if ($this->getContainer()->has('app_router')) { - $this->getContainer()['app_router']($this); - } + $this->getContainer()['app_router']($this); return parent::run($silent); } diff --git a/src/Provider.php b/src/Provider.php index 0234c98..14f41e9 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -20,12 +20,15 @@ class Provider implements ServiceProviderInterface */ public function register(Container $container): void { - $container['suit_config'] = $this->getSuitConfig(); - $container['config'] = $this->getConfig($container); - $container['sentry'] = $this->getSentry($container); - $container['controller'] = $this->setControllerLoader($container); - $container['errorHandler'] = $this->setErrorHandler($container); - $container['phpErrorHandler'] = $this->setErrorHandler($container); + $container['suit_config'] = function ($c) { + return new Config($c); + }; + $container['config'] = $container->protect(function (string $string, $default = null) use ($container) { + return $container['suit_config']->__invoke($string, $default); + }); + $container['app_router'] = function ($c) { + return new Router($c); + }; $container['globalrequest_middleware'] = $container->protect(function ($request, $response, $next) use ($container) { if ($container->has('request')) { unset($container['request']); @@ -34,32 +37,10 @@ public function register(Container $container): void return $next($request, $response); }); - } - - /** - * Prepare config object. - * - * @return callable - */ - protected function getSuitConfig(): callable - { - return function ($c) { - return new Config($c); - }; - } - - /** - * Add 'shortcut' to suit config __invoke(). - * - * @param Container $container - * - * @return callable - */ - protected function getConfig(Container $container): callable - { - return $container->protect(function (...$args) use ($container) { - return \call_user_func_array($container['suit_config'], $args); - }); + $container['sentry'] = $this->getSentry($container); + $container['controller'] = $this->setControllerLoader($container); + $container['errorHandler'] = $this->setErrorHandler($container); + $container['phpErrorHandler'] = $this->setErrorHandler($container); } /** diff --git a/src/Router.php b/src/Router.php new file mode 100644 index 0000000..942a5d5 --- /dev/null +++ b/src/Router.php @@ -0,0 +1,37 @@ +config('routes') as $group_name => $routes) { + $app->group($group_name, function () use ($group_name, $routes): void { + $controller = ('/' === $group_name || !$group_name) ? 'index' : \trim($group_name, '/'); + + foreach ($routes as $name => $route) { + $methods = $route['methods'] ?? ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']; + $pattern = $route['pattern'] ?? ''; + $callable = function (Request $request, Response $response, array $args = []) use ($controller, $route) { + return $this['controller']($controller)->__invoke($request, $response, $args); + }; + $this->map($methods, $pattern, $callable)->setName($controller.'-'.$name); + } + }); + } + } +} diff --git a/tests/AppTest.php b/tests/AppTest.php index f233600..d001549 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -44,6 +44,7 @@ public function testRun(): void //test app_router foreach ($app->getContainer()->get('router')->getRoutes() as $route) { $this->assertAttributeContains('/test/route', 'pattern', $route); + $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $route($app->getContainer()->request, $app->getContainer()->response)); } $appRouter = $app->getContainer()->get('app_router'); diff --git a/tests/ProviderTest.php b/tests/ProviderTest.php index 3684ac4..1ed1e60 100644 --- a/tests/ProviderTest.php +++ b/tests/ProviderTest.php @@ -17,6 +17,11 @@ protected function setUp(): void $this->container = $app->getContainer(); } + public function testAppRouter(): void + { + $this->assertInstanceOf('\Wtf\Router', $this->container->app_router); + } + public function testSentryWithoutUserContext(): void { $this->assertInstanceOf('\Raven_Client', $this->container->sentry); diff --git a/tests/data/config/routes.php b/tests/data/config/routes.php index 9bb3bdb..e132aa2 100644 --- a/tests/data/config/routes.php +++ b/tests/data/config/routes.php @@ -3,5 +3,13 @@ declare(strict_types=1); return [ - '/test/route' => [], + 'DummyController' => [ + 'test_route' => [ + 'pattern' => '/test/route', + 'rbac' => [ + 'admin' => 'get', + 'user' => 'post', + ], + ], + ], ]; diff --git a/tests/data/dummy/DummyController.php b/tests/data/dummy/DummyController.php index 7f919d8..4bf0f64 100644 --- a/tests/data/dummy/DummyController.php +++ b/tests/data/dummy/DummyController.php @@ -20,17 +20,6 @@ class DummyController extends \Wtf\Root */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface { - $this->request = $request; - $this->response = $response; - $name = \explode('-', $request->getAttribute('route')->getName()); - $action = \strtolower(\end($name)).'Action'; - - if (\method_exists($this, $action)) { - $response = \call_user_func([$this, $action]); - } else { - $response = $this->notFoundHandler($request, $response); - } - return $response; } } diff --git a/tests/data/dummy/Provider.php b/tests/data/dummy/Provider.php index 2db4089..0874b7d 100644 --- a/tests/data/dummy/Provider.php +++ b/tests/data/dummy/Provider.php @@ -15,10 +15,6 @@ public function register(\Pimple\Container $container): void return 'Hello, world!'; }; - $container['app_router'] = function ($c) { - return new Router($c); - }; - $container['example_middleware'] = $container->protect(function ($request, $response, $next) use ($container) { return $next($request, $response); }); diff --git a/tests/data/dummy/Router.php b/tests/data/dummy/Router.php deleted file mode 100644 index 7ad5e39..0000000 --- a/tests/data/dummy/Router.php +++ /dev/null @@ -1,17 +0,0 @@ -getContainer()['config']('routes') as $pattern => $info) { - $app->map($info['methods'] ?? ['GET'], $pattern, $info['closure'] ?? function ($request, $response, $args) { - return $response->write('Hello world!'); - }); - } - } -}