From 3daedefbbecaff3e5ddf3b41742d490a0ff0d352 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 01:14:49 +0700 Subject: [PATCH 1/2] Add optimisation --- src/FileRouter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/FileRouter.php b/src/FileRouter.php index cdc498d..4fc90f0 100644 --- a/src/FileRouter.php +++ b/src/FileRouter.php @@ -129,16 +129,20 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface private function parseRequestPath(ServerRequestInterface $request): iterable { - $possibleAction = null; $path = urldecode($request->getUri()->getPath()); - if ($this->routePrefix !== '' && str_starts_with($path, $this->routePrefix)) { - $path = mb_substr($path, strlen($this->routePrefix)); + if ($this->routePrefix !== '') { + if (!str_starts_with($path, $this->routePrefix)) { + return; + } + $path = mb_substr($path, mb_strlen($this->routePrefix)); if ($path === '') { $path = '/'; } } + $possibleAction = null; + if ($path === '/') { yield [ $this->cleanClassname( From 74f337698d0d1b3b704250e3eca31b9a4ba603b0 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 12 Jan 2024 01:20:15 +0700 Subject: [PATCH 2/2] Fix CC --- tests/FileRouterTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/FileRouterTest.php b/tests/FileRouterTest.php index 88df990..28d6f74 100644 --- a/tests/FileRouterTest.php +++ b/tests/FileRouterTest.php @@ -377,6 +377,25 @@ public static function dataModularity(): iterable ]; } + public function testModularityFastPath(): void + { + $router = $this->createRouter(); + $router = $router + ->withNamespace('Yiisoft\\FileRouter\\Tests\\Support\\App5\\Module1') + ->withRoutePrefix('/module1'); + + + $handler = $this->createExceptionHandler(); + $request = new ServerRequest( + method: 'GET', + uri: '/module2', + ); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Not implemented from tests.'); + $router->process($request, $handler); + } + private function createRouter(): FileRouter { $container = new SimpleContainer([