From 7fbf7267bd53f4a601e94b790c46067a37cbfa6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:26:42 +0500 Subject: [PATCH 1/7] update CHANGELOG.md --- CHANGELOG.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ffbd83..a4cc563b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ -## v2.13.0 +# v2.16.0 -* Supports for events using the `symfony/event-dispatcher`. +* New method: `Router::hasRoute(string):bool`. + +## v2.15.0 + +* New middleware: `Sunrise\Http\Router\Middleware\JsonPayloadDecodingMiddleware`. ## v2.14.0 @@ -9,6 +13,6 @@ * New method: `Router::getRoutesByHostname(string):array`; * New method: `RouterBuilder::setEventDispatcher(?EventDispatcherInterface):void`. -## v2.15.0 +## v2.13.0 -* New middleware: `Sunrise\Http\Router\Middleware\JsonPayloadDecodingMiddleware`. +* Supports for events using the `symfony/event-dispatcher`. From 04eb0d11bfa631a9acfeadb937ca0e304d79ae53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:26:49 +0500 Subject: [PATCH 2/7] update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 19aa2e33..5786b11a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Sunrise // PHP +Copyright (c) 2018 Anatoly Nekhay Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 159aacd9ff066b8f118c34947c43a49bb686340b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:27:06 +0500 Subject: [PATCH 3/7] update composer.json --- composer.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 2b3b0962..304c6856 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,23 @@ { "name": "sunrise/http-router", "homepage": "https://github.com/sunrise-php/http-router", - "description": "Sunrise // HTTP router for PHP 7.1+ based on PSR-7 and PSR-15 with support for annotations/attributes and OpenAPI (Swagger) Specification", + "description": "HTTP router for PHP 7.1+ based on PSR-7 and PSR-15 with support for annotations/attributes and OpenAPI (Swagger) Specification", "license": "MIT", "keywords": [ "fenric", "sunrise", "http", "router", - "annotations", + "request-handler", "middlewares", + "annotations", + "attributes", "openapi", + "swagger", "psr-7", "psr-15", "php7", - "php8", - "attributes", - "php-attributes", - "swagger" + "php8" ], "authors": [ { @@ -28,12 +28,12 @@ ], "require": { "php": "^7.1|^8.0", + "fig/http-message-util": "^1.1", "psr/container": "^1.0", "psr/http-message": "^1.0", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", - "psr/simple-cache": "^1.0", - "fig/http-message-util": "^1.1" + "psr/simple-cache": "^1.0" }, "require-dev": { "phpunit/phpunit": "7.5.20|9.5.0", @@ -58,7 +58,7 @@ }, "autoload-dev": { "psr-4": { - "Sunrise\\Http\\Router\\Tests\\Fixtures\\": "tests/fixtures/" + "Sunrise\\Http\\Router\\Tests\\": "tests/" } }, "scripts": { From d6f13417d32c8f76b29ee5089d34f9f99fc7a0ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:27:32 +0500 Subject: [PATCH 4/7] a new method hasRoute was added --- src/Router.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Router.php b/src/Router.php index eb178ae9..6b973897 100644 --- a/src/Router.php +++ b/src/Router.php @@ -365,6 +365,16 @@ public function getAllowedMethods() : array return array_keys($methods); } + /** + * Checks if a route exists by the given name + * + * @return bool + */ + public function hasRoute(string $name) : bool + { + return isset($this->routes[$name]); + } + /** * Gets a route for the given name * From e029f5871351a3d220eb8560e5452c70993a7083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:27:51 +0500 Subject: [PATCH 5/7] tests improved --- tests/Fixtures/CacheAwareTrait.php | 32 ++++++++ tests/Fixtures/ContainerAwareTrait.php | 28 +++++++ .../Controllers/AbstractController.php | 57 +++++++++++++ .../Annotated/AbstractAnnotatedController.php | 12 +++ .../CacheableAnnotatedController.php | 16 ++++ .../Annotated/GroupedAnnotatedController.php | 75 ++++++++++++++++++ .../0/FirstLoadableAnnotatedController.php | 16 ++++ .../1/SecondLoadableAnnotatedController.php | 16 ++++ .../MaximallyAnnotatedController.php | 27 +++++++ .../MinimallyAnnotatedController.php | 16 ++++ .../ResolvableAnnotatedController.php | 19 +++++ .../FirstSortableAnnotatedController.php | 17 ++++ .../SecondSortableAnnotatedController.php | 17 ++++ .../ThirdSortableAnnotatedController.php | 17 ++++ .../GroupedAttributedController.php | 43 ++++++++++ .../MaximallyAttributedController.php | 27 +++++++ .../MinimallyAttributedController.php | 15 ++++ .../Fixtures/Controllers/BlankController.php | 22 ++++++ .../Middlewares/AbstractMiddleware.php | 79 +++++++++++++++++++ .../Fixtures/Middlewares/BlankMiddleware.php | 24 ++++++ tests/Fixtures/Route.php | 34 ++++++++ tests/Fixtures/routes/bar.php | 7 ++ tests/Fixtures/routes/foo.php | 7 ++ .../routes/resolvable/class-method-name.php | 11 +++ .../Fixtures/routes/resolvable/class-name.php | 11 +++ tests/Fixtures/routes/resolvable/closure.php | 16 ++++ tests/Loader/ConfigLoaderTest.php | 8 +- tests/Loader/DescriptorLoaderTest.php | 2 +- tests/RouterBuilderTest.php | 14 ++-- 29 files changed, 672 insertions(+), 13 deletions(-) create mode 100644 tests/Fixtures/CacheAwareTrait.php create mode 100644 tests/Fixtures/ContainerAwareTrait.php create mode 100644 tests/Fixtures/Controllers/AbstractController.php create mode 100644 tests/Fixtures/Controllers/Annotated/AbstractAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/CacheableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/GroupedAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/Loadable/1/SecondLoadableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/MaximallyAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/MinimallyAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/ResolvableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/Sortable/FirstSortableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/Sortable/SecondSortableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Annotated/Sortable/ThirdSortableAnnotatedController.php create mode 100644 tests/Fixtures/Controllers/Attributed/GroupedAttributedController.php create mode 100644 tests/Fixtures/Controllers/Attributed/MaximallyAttributedController.php create mode 100644 tests/Fixtures/Controllers/Attributed/MinimallyAttributedController.php create mode 100644 tests/Fixtures/Controllers/BlankController.php create mode 100644 tests/Fixtures/Middlewares/AbstractMiddleware.php create mode 100644 tests/Fixtures/Middlewares/BlankMiddleware.php create mode 100644 tests/Fixtures/Route.php create mode 100644 tests/Fixtures/routes/bar.php create mode 100644 tests/Fixtures/routes/foo.php create mode 100644 tests/Fixtures/routes/resolvable/class-method-name.php create mode 100644 tests/Fixtures/routes/resolvable/class-name.php create mode 100644 tests/Fixtures/routes/resolvable/closure.php diff --git a/tests/Fixtures/CacheAwareTrait.php b/tests/Fixtures/CacheAwareTrait.php new file mode 100644 index 00000000..1224f7ee --- /dev/null +++ b/tests/Fixtures/CacheAwareTrait.php @@ -0,0 +1,32 @@ +createMock(CacheInterface::class); + $cache->storage = []; + + $cache->method('get')->will($this->returnCallback(function ($key) use ($cache) { + return $cache->storage[$key] ?? null; + })); + + $cache->method('has')->will($this->returnCallback(function ($key) use ($cache) { + return isset($cache->storage[$key]); + })); + + $cache->method('set')->will($this->returnCallback(function ($key, $value) use ($cache) { + $cache->storage[$key] = $value; + })); + + return $cache; + } +} diff --git a/tests/Fixtures/ContainerAwareTrait.php b/tests/Fixtures/ContainerAwareTrait.php new file mode 100644 index 00000000..31bc8498 --- /dev/null +++ b/tests/Fixtures/ContainerAwareTrait.php @@ -0,0 +1,28 @@ +createMock(ContainerInterface::class); + $container->storage = $definitions; + + $container->method('get')->will($this->returnCallback(function ($key) use ($container) { + return $container->storage[$key] ?? null; + })); + + $container->method('has')->will($this->returnCallback(function ($key) use ($container) { + return isset($container->storage[$key]); + })); + + return $container; + } +} diff --git a/tests/Fixtures/Controllers/AbstractController.php b/tests/Fixtures/Controllers/AbstractController.php new file mode 100644 index 00000000..fb36ce4c --- /dev/null +++ b/tests/Fixtures/Controllers/AbstractController.php @@ -0,0 +1,57 @@ +isRunned; + } + + /** + * Gets the request that was handled by the called method + * + * @return ServerRequestInterface|null + */ + public function getRequest() : ?ServerRequestInterface + { + return $this->request; + } + + /** + * {@inheritdoc} + */ + public function handle(ServerRequestInterface $request) : ResponseInterface + { + $this->isRunned = true; + $this->request = $request; + + return (new ResponseFactory)->createResponse(200); + } +} diff --git a/tests/Fixtures/Controllers/Annotated/AbstractAnnotatedController.php b/tests/Fixtures/Controllers/Annotated/AbstractAnnotatedController.php new file mode 100644 index 00000000..8a83239f --- /dev/null +++ b/tests/Fixtures/Controllers/Annotated/AbstractAnnotatedController.php @@ -0,0 +1,12 @@ +handle($request); + } + + /** + * @Route("second-from-grouped-annotated-controller", path="/second") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + */ + public function second($request) + { + return $this->handle($request); + } + + /** + * @Route("third-from-grouped-annotated-controller", path="/third") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") + */ + public function third($request) + { + return $this->handle($request); + } + + /** + * @Route("private-from-grouped-annotated-controller", path="/") + */ + private function privateAction() + { + } + + /** + * @Route("protected-from-grouped-annotated-controller", path="/") + */ + protected function protectedAction() + { + } + + /** + * @Route("static-from-grouped-annotated-controller", path="/") + */ + public static function staticAction() + { + } + + public function shouldBeIgnored() + { + } +} diff --git a/tests/Fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php b/tests/Fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php new file mode 100644 index 00000000..81972be7 --- /dev/null +++ b/tests/Fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php @@ -0,0 +1,16 @@ +handle($request); + } + + #[Routing\Route('second-from-grouped-attributed-controller', path: '/second')] + #[Routing\Middleware(BlankMiddleware::class)] + #[Routing\Middleware(BlankMiddleware::class)] + #[Routing\Middleware(BlankMiddleware::class)] + public function second($request) + { + return $this->handle($request); + } + + #[Routing\Route('third-from-grouped-attributed-controller', path: '/third')] + #[Routing\Middleware(BlankMiddleware::class)] + #[Routing\Middleware(BlankMiddleware::class)] + #[Routing\Middleware(BlankMiddleware::class)] + public function third($request) + { + return $this->handle($request); + } +} diff --git a/tests/Fixtures/Controllers/Attributed/MaximallyAttributedController.php b/tests/Fixtures/Controllers/Attributed/MaximallyAttributedController.php new file mode 100644 index 00000000..27446c9f --- /dev/null +++ b/tests/Fixtures/Controllers/Attributed/MaximallyAttributedController.php @@ -0,0 +1,27 @@ + 'bar', + ], + summary: 'Lorem ipsum', + description: 'Lorem ipsum dolor sit amet', + tags: ['foo', 'bar'], +)] +final class MaximallyAttributedController extends AbstractController +{ +} diff --git a/tests/Fixtures/Controllers/Attributed/MinimallyAttributedController.php b/tests/Fixtures/Controllers/Attributed/MinimallyAttributedController.php new file mode 100644 index 00000000..ec8f7767 --- /dev/null +++ b/tests/Fixtures/Controllers/Attributed/MinimallyAttributedController.php @@ -0,0 +1,15 @@ +handle($request)->withStatus(305); + } +} diff --git a/tests/Fixtures/Middlewares/AbstractMiddleware.php b/tests/Fixtures/Middlewares/AbstractMiddleware.php new file mode 100644 index 00000000..df27cc1d --- /dev/null +++ b/tests/Fixtures/Middlewares/AbstractMiddleware.php @@ -0,0 +1,79 @@ +isBreakable = $isBreakable; + } + + /** + * Checks if the middleware was runned + * + * @return bool + */ + public function isRunned() : bool + { + return $this->isRunned; + } + + /** + * Gets the request that was handled by the called method + * + * @return ServerRequestInterface|null + */ + public function getRequest() : ?ServerRequestInterface + { + return $this->request; + } + + /** + * {@inheritdoc} + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface + { + $this->isRunned = true; + $this->request = $request; + + if ($this->isBreakable) { + return (new ResponseFactory)->createResponse(200); + } + + return $handler->handle($request); + } +} diff --git a/tests/Fixtures/Middlewares/BlankMiddleware.php b/tests/Fixtures/Middlewares/BlankMiddleware.php new file mode 100644 index 00000000..79b6e8e2 --- /dev/null +++ b/tests/Fixtures/Middlewares/BlankMiddleware.php @@ -0,0 +1,24 @@ +process($request, $handler)->withStatus(305); + } +} diff --git a/tests/Fixtures/Route.php b/tests/Fixtures/Route.php new file mode 100644 index 00000000..2fbd50f7 --- /dev/null +++ b/tests/Fixtures/Route.php @@ -0,0 +1,34 @@ +get('bar', '/bar', new BlankController()); diff --git a/tests/Fixtures/routes/foo.php b/tests/Fixtures/routes/foo.php new file mode 100644 index 00000000..1421ca93 --- /dev/null +++ b/tests/Fixtures/routes/foo.php @@ -0,0 +1,7 @@ +get('foo', '/foo', new BlankController()); diff --git a/tests/Fixtures/routes/resolvable/class-method-name.php b/tests/Fixtures/routes/resolvable/class-method-name.php new file mode 100644 index 00000000..78ebcf26 --- /dev/null +++ b/tests/Fixtures/routes/resolvable/class-method-name.php @@ -0,0 +1,11 @@ +get('resolvable-class-method-name-route', '/', $controller, [$middleware]); diff --git a/tests/Fixtures/routes/resolvable/class-name.php b/tests/Fixtures/routes/resolvable/class-name.php new file mode 100644 index 00000000..4abc3e73 --- /dev/null +++ b/tests/Fixtures/routes/resolvable/class-name.php @@ -0,0 +1,11 @@ +get('resolvable-class-name-route', '/', $controller, [$middleware]); diff --git a/tests/Fixtures/routes/resolvable/closure.php b/tests/Fixtures/routes/resolvable/closure.php new file mode 100644 index 00000000..d625941c --- /dev/null +++ b/tests/Fixtures/routes/resolvable/closure.php @@ -0,0 +1,16 @@ +handle($request); +}; + +$middleware = function ($request, $handler) { + return (new BlankMiddleware)->process($request, $handler); +}; + +$this->get('resolvable-closure-route', '/', $controller, [$middleware]); diff --git a/tests/Loader/ConfigLoaderTest.php b/tests/Loader/ConfigLoaderTest.php index 0e4d0d2c..fa114f86 100644 --- a/tests/Loader/ConfigLoaderTest.php +++ b/tests/Loader/ConfigLoaderTest.php @@ -80,7 +80,7 @@ public function testLoadFile() : void { $loader = new ConfigLoader(); - $loader->attach(__DIR__ . '/../fixtures/routes/foo.php'); + $loader->attach(__DIR__ . '/../Fixtures/routes/foo.php'); $routes = $loader->load(); @@ -97,8 +97,8 @@ public function testLoadSeveralFiles() : void $loader = new ConfigLoader(); $loader->attachArray([ - __DIR__ . '/../fixtures/routes/foo.php', - __DIR__ . '/../fixtures/routes/bar.php', + __DIR__ . '/../Fixtures/routes/foo.php', + __DIR__ . '/../Fixtures/routes/bar.php', ]); $routes = $loader->load(); @@ -116,7 +116,7 @@ public function testLoadDirectory() : void { $loader = new ConfigLoader(); - $loader->attach(__DIR__ . '/../fixtures/routes'); + $loader->attach(__DIR__ . '/../Fixtures/routes'); $routes = $loader->load(); diff --git a/tests/Loader/DescriptorLoaderTest.php b/tests/Loader/DescriptorLoaderTest.php index 805aeb0f..929e29f9 100644 --- a/tests/Loader/DescriptorLoaderTest.php +++ b/tests/Loader/DescriptorLoaderTest.php @@ -321,7 +321,7 @@ public function testLoadSeveralAnnotatedClasses() : void public function testLoadDirectoryWithAnnotatedClasses() : void { $loader = new DescriptorLoader(); - $loader->attach(__DIR__ . '/../fixtures/Controllers/Annotated/Loadable'); + $loader->attach(__DIR__ . '/../Fixtures/Controllers/Annotated/Loadable'); $routes = $loader->load(); $this->assertTrue($routes->has('first-loadable-annotated-controller')); diff --git a/tests/RouterBuilderTest.php b/tests/RouterBuilderTest.php index fd68dc4e..953da79d 100644 --- a/tests/RouterBuilderTest.php +++ b/tests/RouterBuilderTest.php @@ -50,8 +50,8 @@ public function testBuild() : void ->setCache($cache) ->setCacheKey('foo') ->useConfigLoader([ - __DIR__ . '/fixtures/routes/foo.php', - __DIR__ . '/fixtures/routes/bar.php', + __DIR__ . '/Fixtures/routes/foo.php', + __DIR__ . '/Fixtures/routes/bar.php', ]) ->useMetadataLoader([ Fixtures\Controllers\Annotated\MinimallyAnnotatedController::class, @@ -70,11 +70,9 @@ public function testBuild() : void $this->assertSame($hosts, $router->getHosts()); $this->assertSame($middlewares, $router->getMiddlewares()); $this->assertSame($eventDispatcher, $router->getEventDispatcher()); - - $router->getRoutes('foo'); - $router->getRoutes('bar'); - - $router->getRoutes('minimally-annotated-controller'); - $router->getRoutes('maximally-annotated-controller'); + $this->assertTrue($router->hasRoute('foo')); + $this->assertTrue($router->hasRoute('bar')); + $this->assertTrue($router->hasRoute('minimally-annotated-controller')); + $this->assertTrue($router->hasRoute('maximally-annotated-controller')); } } From c7eddc8687dcd3e0924e870eb93f0e5b652c4f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:30:59 +0500 Subject: [PATCH 6/7] rename --- tests/fixtures/CacheAwareTrait.php | 32 -------- tests/fixtures/ContainerAwareTrait.php | 28 ------- .../Controllers/AbstractController.php | 57 ------------- .../Annotated/AbstractAnnotatedController.php | 12 --- .../CacheableAnnotatedController.php | 16 ---- .../Annotated/GroupedAnnotatedController.php | 75 ------------------ .../0/FirstLoadableAnnotatedController.php | 16 ---- .../1/SecondLoadableAnnotatedController.php | 16 ---- .../MaximallyAnnotatedController.php | 27 ------- .../MinimallyAnnotatedController.php | 16 ---- .../ResolvableAnnotatedController.php | 19 ----- .../FirstSortableAnnotatedController.php | 17 ---- .../SecondSortableAnnotatedController.php | 17 ---- .../ThirdSortableAnnotatedController.php | 17 ---- .../GroupedAttributedController.php | 43 ---------- .../MaximallyAttributedController.php | 27 ------- .../MinimallyAttributedController.php | 15 ---- .../fixtures/Controllers/BlankController.php | 22 ------ .../Middlewares/AbstractMiddleware.php | 79 ------------------- .../fixtures/Middlewares/BlankMiddleware.php | 24 ------ tests/fixtures/Route.php | 34 -------- tests/fixtures/routes/bar.php | 7 -- tests/fixtures/routes/foo.php | 7 -- .../routes/resolvable/class-method-name.php | 11 --- .../fixtures/routes/resolvable/class-name.php | 11 --- tests/fixtures/routes/resolvable/closure.php | 16 ---- 26 files changed, 661 deletions(-) delete mode 100644 tests/fixtures/CacheAwareTrait.php delete mode 100644 tests/fixtures/ContainerAwareTrait.php delete mode 100644 tests/fixtures/Controllers/AbstractController.php delete mode 100644 tests/fixtures/Controllers/Annotated/AbstractAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/CacheableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/GroupedAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/Loadable/1/SecondLoadableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/MaximallyAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/MinimallyAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/ResolvableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/Sortable/FirstSortableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/Sortable/SecondSortableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Annotated/Sortable/ThirdSortableAnnotatedController.php delete mode 100644 tests/fixtures/Controllers/Attributed/GroupedAttributedController.php delete mode 100644 tests/fixtures/Controllers/Attributed/MaximallyAttributedController.php delete mode 100644 tests/fixtures/Controllers/Attributed/MinimallyAttributedController.php delete mode 100644 tests/fixtures/Controllers/BlankController.php delete mode 100644 tests/fixtures/Middlewares/AbstractMiddleware.php delete mode 100644 tests/fixtures/Middlewares/BlankMiddleware.php delete mode 100644 tests/fixtures/Route.php delete mode 100644 tests/fixtures/routes/bar.php delete mode 100644 tests/fixtures/routes/foo.php delete mode 100644 tests/fixtures/routes/resolvable/class-method-name.php delete mode 100644 tests/fixtures/routes/resolvable/class-name.php delete mode 100644 tests/fixtures/routes/resolvable/closure.php diff --git a/tests/fixtures/CacheAwareTrait.php b/tests/fixtures/CacheAwareTrait.php deleted file mode 100644 index 1224f7ee..00000000 --- a/tests/fixtures/CacheAwareTrait.php +++ /dev/null @@ -1,32 +0,0 @@ -createMock(CacheInterface::class); - $cache->storage = []; - - $cache->method('get')->will($this->returnCallback(function ($key) use ($cache) { - return $cache->storage[$key] ?? null; - })); - - $cache->method('has')->will($this->returnCallback(function ($key) use ($cache) { - return isset($cache->storage[$key]); - })); - - $cache->method('set')->will($this->returnCallback(function ($key, $value) use ($cache) { - $cache->storage[$key] = $value; - })); - - return $cache; - } -} diff --git a/tests/fixtures/ContainerAwareTrait.php b/tests/fixtures/ContainerAwareTrait.php deleted file mode 100644 index 31bc8498..00000000 --- a/tests/fixtures/ContainerAwareTrait.php +++ /dev/null @@ -1,28 +0,0 @@ -createMock(ContainerInterface::class); - $container->storage = $definitions; - - $container->method('get')->will($this->returnCallback(function ($key) use ($container) { - return $container->storage[$key] ?? null; - })); - - $container->method('has')->will($this->returnCallback(function ($key) use ($container) { - return isset($container->storage[$key]); - })); - - return $container; - } -} diff --git a/tests/fixtures/Controllers/AbstractController.php b/tests/fixtures/Controllers/AbstractController.php deleted file mode 100644 index fb36ce4c..00000000 --- a/tests/fixtures/Controllers/AbstractController.php +++ /dev/null @@ -1,57 +0,0 @@ -isRunned; - } - - /** - * Gets the request that was handled by the called method - * - * @return ServerRequestInterface|null - */ - public function getRequest() : ?ServerRequestInterface - { - return $this->request; - } - - /** - * {@inheritdoc} - */ - public function handle(ServerRequestInterface $request) : ResponseInterface - { - $this->isRunned = true; - $this->request = $request; - - return (new ResponseFactory)->createResponse(200); - } -} diff --git a/tests/fixtures/Controllers/Annotated/AbstractAnnotatedController.php b/tests/fixtures/Controllers/Annotated/AbstractAnnotatedController.php deleted file mode 100644 index 8a83239f..00000000 --- a/tests/fixtures/Controllers/Annotated/AbstractAnnotatedController.php +++ /dev/null @@ -1,12 +0,0 @@ -handle($request); - } - - /** - * @Route("second-from-grouped-annotated-controller", path="/second") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - */ - public function second($request) - { - return $this->handle($request); - } - - /** - * @Route("third-from-grouped-annotated-controller", path="/third") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - * @Middleware("Sunrise\Http\Router\Tests\Fixtures\Middlewares\BlankMiddleware") - */ - public function third($request) - { - return $this->handle($request); - } - - /** - * @Route("private-from-grouped-annotated-controller", path="/") - */ - private function privateAction() - { - } - - /** - * @Route("protected-from-grouped-annotated-controller", path="/") - */ - protected function protectedAction() - { - } - - /** - * @Route("static-from-grouped-annotated-controller", path="/") - */ - public static function staticAction() - { - } - - public function shouldBeIgnored() - { - } -} diff --git a/tests/fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php b/tests/fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php deleted file mode 100644 index 81972be7..00000000 --- a/tests/fixtures/Controllers/Annotated/Loadable/0/FirstLoadableAnnotatedController.php +++ /dev/null @@ -1,16 +0,0 @@ -handle($request); - } - - #[Routing\Route('second-from-grouped-attributed-controller', path: '/second')] - #[Routing\Middleware(BlankMiddleware::class)] - #[Routing\Middleware(BlankMiddleware::class)] - #[Routing\Middleware(BlankMiddleware::class)] - public function second($request) - { - return $this->handle($request); - } - - #[Routing\Route('third-from-grouped-attributed-controller', path: '/third')] - #[Routing\Middleware(BlankMiddleware::class)] - #[Routing\Middleware(BlankMiddleware::class)] - #[Routing\Middleware(BlankMiddleware::class)] - public function third($request) - { - return $this->handle($request); - } -} diff --git a/tests/fixtures/Controllers/Attributed/MaximallyAttributedController.php b/tests/fixtures/Controllers/Attributed/MaximallyAttributedController.php deleted file mode 100644 index 27446c9f..00000000 --- a/tests/fixtures/Controllers/Attributed/MaximallyAttributedController.php +++ /dev/null @@ -1,27 +0,0 @@ - 'bar', - ], - summary: 'Lorem ipsum', - description: 'Lorem ipsum dolor sit amet', - tags: ['foo', 'bar'], -)] -final class MaximallyAttributedController extends AbstractController -{ -} diff --git a/tests/fixtures/Controllers/Attributed/MinimallyAttributedController.php b/tests/fixtures/Controllers/Attributed/MinimallyAttributedController.php deleted file mode 100644 index ec8f7767..00000000 --- a/tests/fixtures/Controllers/Attributed/MinimallyAttributedController.php +++ /dev/null @@ -1,15 +0,0 @@ -handle($request)->withStatus(305); - } -} diff --git a/tests/fixtures/Middlewares/AbstractMiddleware.php b/tests/fixtures/Middlewares/AbstractMiddleware.php deleted file mode 100644 index df27cc1d..00000000 --- a/tests/fixtures/Middlewares/AbstractMiddleware.php +++ /dev/null @@ -1,79 +0,0 @@ -isBreakable = $isBreakable; - } - - /** - * Checks if the middleware was runned - * - * @return bool - */ - public function isRunned() : bool - { - return $this->isRunned; - } - - /** - * Gets the request that was handled by the called method - * - * @return ServerRequestInterface|null - */ - public function getRequest() : ?ServerRequestInterface - { - return $this->request; - } - - /** - * {@inheritdoc} - */ - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface - { - $this->isRunned = true; - $this->request = $request; - - if ($this->isBreakable) { - return (new ResponseFactory)->createResponse(200); - } - - return $handler->handle($request); - } -} diff --git a/tests/fixtures/Middlewares/BlankMiddleware.php b/tests/fixtures/Middlewares/BlankMiddleware.php deleted file mode 100644 index 79b6e8e2..00000000 --- a/tests/fixtures/Middlewares/BlankMiddleware.php +++ /dev/null @@ -1,24 +0,0 @@ -process($request, $handler)->withStatus(305); - } -} diff --git a/tests/fixtures/Route.php b/tests/fixtures/Route.php deleted file mode 100644 index 2fbd50f7..00000000 --- a/tests/fixtures/Route.php +++ /dev/null @@ -1,34 +0,0 @@ -get('bar', '/bar', new BlankController()); diff --git a/tests/fixtures/routes/foo.php b/tests/fixtures/routes/foo.php deleted file mode 100644 index 1421ca93..00000000 --- a/tests/fixtures/routes/foo.php +++ /dev/null @@ -1,7 +0,0 @@ -get('foo', '/foo', new BlankController()); diff --git a/tests/fixtures/routes/resolvable/class-method-name.php b/tests/fixtures/routes/resolvable/class-method-name.php deleted file mode 100644 index 78ebcf26..00000000 --- a/tests/fixtures/routes/resolvable/class-method-name.php +++ /dev/null @@ -1,11 +0,0 @@ -get('resolvable-class-method-name-route', '/', $controller, [$middleware]); diff --git a/tests/fixtures/routes/resolvable/class-name.php b/tests/fixtures/routes/resolvable/class-name.php deleted file mode 100644 index 4abc3e73..00000000 --- a/tests/fixtures/routes/resolvable/class-name.php +++ /dev/null @@ -1,11 +0,0 @@ -get('resolvable-class-name-route', '/', $controller, [$middleware]); diff --git a/tests/fixtures/routes/resolvable/closure.php b/tests/fixtures/routes/resolvable/closure.php deleted file mode 100644 index d625941c..00000000 --- a/tests/fixtures/routes/resolvable/closure.php +++ /dev/null @@ -1,16 +0,0 @@ -handle($request); -}; - -$middleware = function ($request, $handler) { - return (new BlankMiddleware)->process($request, $handler); -}; - -$this->get('resolvable-closure-route', '/', $controller, [$middleware]); From 88e7969a97ce5980c57d90560c60f4f6c50dfe8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=9D?= =?UTF-8?q?=D0=B5=D1=85=D0=B0=D0=B9?= Date: Tue, 19 Apr 2022 04:34:21 +0500 Subject: [PATCH 7/7] update README.md --- README.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f3f7e4ee..a20787bf 100644 --- a/README.md +++ b/README.md @@ -480,6 +480,16 @@ use Sunrise\Http\Router\Middleware\JsonPayloadDecodingMiddleware; $router->addMiddleware(new JsonPayloadDecodingMiddleware()); ``` +### Get a route by name + +```php +// checks if a route is exists +$router->hasRoute('foo'); + +// gets a route by name +$router->getRoute('foo'); +``` + ### Get a current route #### Through Router @@ -642,9 +652,10 @@ $route->getHolder(); // return Reflector (class, method or function) ```php $router = (new RouterBuilder) - ->setContainer(null) // null or PSR-11 container instance... - ->setCache(null) // null or PSR-16 cache instance... (only for descriptor loader) - ->setCacheKey(null) // null or string... (only for descriptor loader) + ->setEventDispatcher(...) // null or use to symfony/event-dispatcher... + ->setContainer(...) // null or PSR-11 container instance... + ->setCache(...) // null or PSR-16 cache instance... (only for descriptor loader) + ->setCacheKey(...) // null or string... (only for descriptor loader) ->useConfigLoader([]) // array with files or directory with files... ->useDescriptorLoader([]) // array with classes or directory with classes... ->setHosts([]) //