From e1aa5699f3271f4ef3888a961014f1b84c4d833c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 13:23:54 +0300 Subject: [PATCH 01/12] refactor --- src/FileRouter.php | 60 ++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index 7733f35..2dcf606 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -155,14 +155,10 @@ private function parseRequestPath(ServerRequestInterface $request): Generator } } - $possibleAction = null; - if ($path === '/') { yield [ - $this->cleanClassname( - $this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $this->defaultControllerName . $this->classPostfix - ), - $possibleAction, + $this->makeClassName($this->defaultControllerName), + null, ]; return; } @@ -173,43 +169,43 @@ private function parseRequestPath(ServerRequestInterface $request): Generator $path, ); - if (!preg_match('#^(.*?)/([^/]+)/?$#', $controllerName, $matches)) { + if (!preg_match('#^/?(.*?)/([^/]+)/?$#', $controllerName, $matches)) { return; } - $directoryPath = $matches[1]; - $controllerName = $matches[2]; + [$_, $directoryPath, $controllerName] = $matches; yield [ - $this->cleanClassname( - $this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix - ), - $possibleAction, + $this->makeClassName($controllerName, $directoryPath), + null, ]; - if (preg_match('#^(.*?)/([^/]+)/?$#', $directoryPath, $matches)) { - $possibleAction = strtolower($controllerName); - $directoryPath = $matches[1]; - $controllerName = $matches[2]; + if ($directoryPath === '') { + yield [ + $this->makeClassName($this->defaultControllerName, $controllerName), + null, + ]; } else { - $directoryPath = $controllerName; - $controllerName = $this->defaultControllerName; + yield [ + $this->makeClassName($directoryPath), + strtolower($controllerName), + ]; } - - yield [ - $this->cleanClassname( - $this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix - ), - $possibleAction, - ]; } - private function cleanClassname(string $className): string + private function makeClassName(string $controllerName, string $directoryPath = ''): string { - return str_replace( - ['\\/\\', '\\/', '\\\\'], - '\\', - $className, - ); + $parts = []; + if ($this->namespace !== '') { + $parts[] = $this->namespace; + } + if ($this->baseControllerDirectory !== '') { + $parts[] = $this->baseControllerDirectory; + } + if ($directoryPath !== '') { + $parts[] = $directoryPath; + } + $parts[] = $controllerName . $this->classPostfix; + return implode('\\', $parts); } } From 8ef269f139bb43998d440a2291a9e5313ffae4d2 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 13:59:32 +0300 Subject: [PATCH 02/12] test --- tests/FileRouterTest.php | 8 ++++++++ .../Controller/IndexController.php" | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 "tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 3257814..fb04aad 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -370,6 +370,13 @@ public static function dataModularity(): iterable '/m/o/d/u/l/e/index', 'Hello, module2!', ]; + yield 'модуль3 /index' => [ + 'Yiisoft\\FileRouter\\Tests\\Support\\App5\\Модуль3', + '/Модуль3', + 'GET', + '/Модуль3/index', + 'Hello, модуль3!', + ]; } public function testModularityFastPath(): void @@ -421,6 +428,7 @@ private function createRouter(): FileRouter App5\Module1\Controller\IndexController::class => new App5\Module1\Controller\IndexController(), App5\Module2\Controller\IndexController::class => new App5\Module2\Controller\IndexController(), + App5\Модуль3\Controller\IndexController::class => new App5\Модуль3\Controller\IndexController(), ]); return new FileRouter( diff --git "a/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" "b/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" new file mode 100644 index 0000000..c3d0c64 --- /dev/null +++ "b/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" @@ -0,0 +1,16 @@ + Date: Sun, 24 Nov 2024 14:02:57 +0300 Subject: [PATCH 03/12] test --- tests/FileRouterTest.php | 10 ++++++++++ tests/Support/App1/Controller/UserController.php | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index fb04aad..66a397e 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -103,6 +103,16 @@ public static function dataRouter(): iterable '/user', 'Hello, delete!', ]; + yield 'HEAD /user' => [ + 'HEAD', + '/user', + 'Hello, head!', + ]; + yield 'OPTIONS /user' => [ + 'OPTIONS', + '/user', + 'Hello, options!', + ]; } public function testUnsupportedMethod(): void diff --git a/tests/Support/App1/Controller/UserController.php b/tests/Support/App1/Controller/UserController.php index 1cc4418..7b17972 100644 --- a/tests/Support/App1/Controller/UserController.php +++ b/tests/Support/App1/Controller/UserController.php @@ -21,6 +21,16 @@ public function index(): ResponseInterface return new TextResponse('Hello, index!'); } + public function head(): ResponseInterface + { + return new TextResponse('Hello, head!'); + } + + public function options(): ResponseInterface + { + return new TextResponse('Hello, options!'); + } + public function create(): ResponseInterface { return new TextResponse('Hello, create!'); From bacdef2ad92ea9c96b157d42bd1d0603a1b28982 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 14:09:07 +0300 Subject: [PATCH 04/12] test --- tests/FileRouterTest.php | 13 +++++++++---- .../App3/Controller/User/IndexController.php | 4 ++-- tests/Support/App3/Controller/UserController.php | 7 ++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 66a397e..a4ae52c 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -282,16 +282,21 @@ public function testRoutesCollision(string $method, string $uri, string $expecte public static function dataRoutesCollision(): iterable { - yield 'direct' => [ + yield 'direct get' => [ 'GET', '/user', - 'Hello, Controller/UserController!', + 'Hello, index Controller/UserController!', + ]; + yield 'direct delete' => [ + 'DELETE', + '/user', + 'Hello, delete Controller/UserController!', ]; - yield 'indirect' => [ + yield 'indirect post' => [ 'POST', '/user', - 'Hello, Controller/User/IndexController!', + 'Hello, create Controller/User/IndexController!', ]; } diff --git a/tests/Support/App3/Controller/User/IndexController.php b/tests/Support/App3/Controller/User/IndexController.php index 373b64f..df42563 100644 --- a/tests/Support/App3/Controller/User/IndexController.php +++ b/tests/Support/App3/Controller/User/IndexController.php @@ -11,11 +11,11 @@ class IndexController { public function index(): ResponseInterface { - return new TextResponse('Hello, Controller/User/IndexController!'); + return new TextResponse('Hello, index Controller/User/IndexController!'); } public function create(): ResponseInterface { - return new TextResponse('Hello, Controller/User/IndexController!'); + return new TextResponse('Hello, create Controller/User/IndexController!'); } } diff --git a/tests/Support/App3/Controller/UserController.php b/tests/Support/App3/Controller/UserController.php index 1fe13fa..1332de2 100644 --- a/tests/Support/App3/Controller/UserController.php +++ b/tests/Support/App3/Controller/UserController.php @@ -11,6 +11,11 @@ class UserController { public function index(): ResponseInterface { - return new TextResponse('Hello, Controller/UserController!'); + return new TextResponse('Hello, index Controller/UserController!'); + } + + public function delete(): ResponseInterface + { + return new TextResponse('Hello, delete Controller/UserController!'); } } From aa16eca7c1b38e49c248c131f7306cc7cf30481c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 14:18:24 +0300 Subject: [PATCH 05/12] test --- tests/FileRouterTest.php | 21 ++++++++++++++++ .../App6/Controller/User/IndexController.php | 16 ++++++++++++ .../App6/Controller/UserController.php | 25 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 tests/Support/App6/Controller/User/IndexController.php create mode 100644 tests/Support/App6/Controller/UserController.php diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index a4ae52c..0a5c575 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -17,6 +17,7 @@ use Yiisoft\FileRouter\Tests\Support\App3; use Yiisoft\FileRouter\Tests\Support\App4; use Yiisoft\FileRouter\Tests\Support\App5; +use Yiisoft\FileRouter\Tests\Support\App6; use Yiisoft\FileRouter\Tests\Support\HeaderMiddleware; use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; use Yiisoft\Middleware\Dispatcher\MiddlewareFactory; @@ -300,6 +301,23 @@ public static function dataRoutesCollision(): iterable ]; } + public function testActions(): void + { + $router = $this->createRouter(); + $router = $router->withNamespace('Yiisoft\FileRouter\Tests\Support\App6'); + + $handler = $this->createExceptionHandler(); + $request = new ServerRequest( + method: 'POST', + uri: '/user', + ); + + $response = $router->process($request, $handler); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('Hello, create Controller/User/IndexController!', (string) $response->getBody()); + } + #[DataProvider('dataUnicodeRoutes')] public function testTestUnicodeRoutes(string $method, string $uri, string $expectedResponse): void { @@ -444,6 +462,9 @@ private function createRouter(): FileRouter App5\Module1\Controller\IndexController::class => new App5\Module1\Controller\IndexController(), App5\Module2\Controller\IndexController::class => new App5\Module2\Controller\IndexController(), App5\Модуль3\Controller\IndexController::class => new App5\Модуль3\Controller\IndexController(), + + App6\Controller\UserController::class => new App6\Controller\UserController(), + App6\Controller\User\IndexController::class => new App6\Controller\User\IndexController(), ]); return new FileRouter( diff --git a/tests/Support/App6/Controller/User/IndexController.php b/tests/Support/App6/Controller/User/IndexController.php new file mode 100644 index 0000000..9ffd892 --- /dev/null +++ b/tests/Support/App6/Controller/User/IndexController.php @@ -0,0 +1,16 @@ + 'index', + ]; + + public function index(): ResponseInterface + { + return new TextResponse('Hello, index Controller/UserController!'); + } + + public function create(): ResponseInterface + { + return new TextResponse('Hello, create Controller/UserController!'); + } +} From e2e642dcbbd34a009094dbe9a2d95dc0f372a015 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 14:23:00 +0300 Subject: [PATCH 06/12] test --- tests/FileRouterTest.php | 2 +- .../Controller/Index/IndexController.php" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename "tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" => "tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/Index/IndexController.php" (95%) diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 0a5c575..008a7f8 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -461,7 +461,7 @@ private function createRouter(): FileRouter App5\Module1\Controller\IndexController::class => new App5\Module1\Controller\IndexController(), App5\Module2\Controller\IndexController::class => new App5\Module2\Controller\IndexController(), - App5\Модуль3\Controller\IndexController::class => new App5\Модуль3\Controller\IndexController(), + App5\Модуль3\Controller\Index\IndexController::class => new App5\Модуль3\Controller\Index\IndexController(), App6\Controller\UserController::class => new App6\Controller\UserController(), App6\Controller\User\IndexController::class => new App6\Controller\User\IndexController(), diff --git "a/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" "b/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/Index/IndexController.php" similarity index 95% rename from "tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" rename to "tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/Index/IndexController.php" index c3d0c64..811f578 100644 --- "a/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/IndexController.php" +++ "b/tests/Support/App5/\320\234\320\276\320\264\321\203\320\273\321\2143/Controller/Index/IndexController.php" @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\FileRouter\Tests\Support\App5\Модуль3\Controller; +namespace Yiisoft\FileRouter\Tests\Support\App5\Модуль3\Controller\Index; use HttpSoft\Response\TextResponse; use Psr\Http\Message\ResponseInterface; From af1fc0f469ffe2912f22dd4e229d7d93a0179fd1 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 14:25:52 +0300 Subject: [PATCH 07/12] refactor --- src/FileRouter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index 2dcf606..1cf7193 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -164,8 +164,8 @@ private function parseRequestPath(ServerRequestInterface $request): Generator } $controllerName = preg_replace_callback( - '#(/.)#u', - static fn(array $matches) => mb_strtoupper($matches[1]), + '#/.#u', + static fn(array $matches) => mb_strtoupper($matches[0]), $path, ); From 226f6b4621851cccf41b9ecf2f6c83d409382c1b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Sun, 24 Nov 2024 14:44:29 +0300 Subject: [PATCH 08/12] refactor --- infection.json.dist | 3 ++- src/FileRouter.php | 4 ++-- tests/FileRouterTest.php | 6 ++++++ .../Controller/User/Profile/ViewController.php | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/Support/App1/Controller/User/Profile/ViewController.php diff --git a/infection.json.dist b/infection.json.dist index 3776e22..dfda80e 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -12,5 +12,6 @@ }, "mutators": { "@default": true - } + }, + "minMsi": 100 } diff --git a/src/FileRouter.php b/src/FileRouter.php index 1cf7193..9d0a8f4 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -203,9 +203,9 @@ private function makeClassName(string $controllerName, string $directoryPath = ' $parts[] = $this->baseControllerDirectory; } if ($directoryPath !== '') { - $parts[] = $directoryPath; + $parts[] = str_replace('/', '\\', $directoryPath); } - $parts[] = $controllerName . $this->classPostfix; + $parts[] = str_replace('/', '\\', $controllerName) . $this->classPostfix; return implode('\\', $parts); } } diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 008a7f8..8b81b47 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -114,6 +114,11 @@ public static function dataRouter(): iterable '/user', 'Hello, options!', ]; + yield 'GET /user/profile/view' => [ + 'GET', + '/user/profile/view', + 'Hello, User\Profile\IndexController!', + ]; } public function testUnsupportedMethod(): void @@ -447,6 +452,7 @@ private function createRouter(): FileRouter $container = new SimpleContainer([ HeaderMiddleware::class => new HeaderMiddleware(), + App1\Controller\User\Profile\ViewController::class => new App1\Controller\User\Profile\ViewController(), App1\Controller\User\BlogController::class => new App1\Controller\User\BlogController(), App1\Controller\UserController::class => new App1\Controller\UserController(), App1\Controller\IndexController::class => new App1\Controller\IndexController(), diff --git a/tests/Support/App1/Controller/User/Profile/ViewController.php b/tests/Support/App1/Controller/User/Profile/ViewController.php new file mode 100644 index 0000000..202dfaa --- /dev/null +++ b/tests/Support/App1/Controller/User/Profile/ViewController.php @@ -0,0 +1,16 @@ + Date: Sun, 24 Nov 2024 14:47:29 +0300 Subject: [PATCH 09/12] test --- tests/FileRouterTest.php | 5 +++++ tests/Support/App1/Controller/User/BlogController.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 8b81b47..c7c433e 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -119,6 +119,11 @@ public static function dataRouter(): iterable '/user/profile/view', 'Hello, User\Profile\IndexController!', ]; + yield 'GET /user/blog/view' => [ + 'GET', + '/user/blog/view', + 'Hello, blog view!', + ]; } public function testUnsupportedMethod(): void diff --git a/tests/Support/App1/Controller/User/BlogController.php b/tests/Support/App1/Controller/User/BlogController.php index 5215cda..d001105 100644 --- a/tests/Support/App1/Controller/User/BlogController.php +++ b/tests/Support/App1/Controller/User/BlogController.php @@ -20,4 +20,9 @@ public function index(): ResponseInterface { return new TextResponse('Hello, index!'); } + + public function view(): ResponseInterface + { + return new TextResponse('Hello, blog view!'); + } } From ddaa97e0fba36994695f53cba4a2d9ca05edf960 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 25 Nov 2024 14:16:33 +0300 Subject: [PATCH 10/12] refactor --- src/FileRouter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index 9d0a8f4..812bdc8 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -173,7 +173,7 @@ private function parseRequestPath(ServerRequestInterface $request): Generator return; } - [$_, $directoryPath, $controllerName] = $matches; + [, $directoryPath, $controllerName] = $matches; yield [ $this->makeClassName($controllerName, $directoryPath), From 79f8a2cd27ccf937702743942ca67f8457a7e924 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 25 Nov 2024 14:29:07 +0300 Subject: [PATCH 11/12] improve --- src/FileRouter.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index 812bdc8..d0b8f26 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -163,16 +163,15 @@ private function parseRequestPath(ServerRequestInterface $request): Generator return; } - $controllerName = preg_replace_callback( + $pathAsNamespace = preg_replace_callback( '#/.#u', static fn(array $matches) => mb_strtoupper($matches[0]), $path, ); - if (!preg_match('#^/?(.*?)/([^/]+)/?$#', $controllerName, $matches)) { + if (!preg_match('#^/?(.*?)/([^/]+)/?$#', $pathAsNamespace, $matches)) { return; } - [, $directoryPath, $controllerName] = $matches; yield [ From 1a237e39a685e13e4a54346ac9cf87d0c12bceb0 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 25 Nov 2024 14:34:14 +0300 Subject: [PATCH 12/12] fix --- src/FileRouter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index d0b8f26..ebcce4a 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -157,7 +157,7 @@ private function parseRequestPath(ServerRequestInterface $request): Generator if ($path === '/') { yield [ - $this->makeClassName($this->defaultControllerName), + $this->makeClassName($this->defaultControllerName, ''), null, ]; return; @@ -186,13 +186,13 @@ private function parseRequestPath(ServerRequestInterface $request): Generator ]; } else { yield [ - $this->makeClassName($directoryPath), + $this->makeClassName($directoryPath, ''), strtolower($controllerName), ]; } } - private function makeClassName(string $controllerName, string $directoryPath = ''): string + private function makeClassName(string $controllerName, string $directoryPath): string { $parts = []; if ($this->namespace !== '') {