From 491ffec9ebeeef044c4fb179b36f2fb27bc5a044 Mon Sep 17 00:00:00 2001 From: Tomas Saghy Date: Fri, 28 Jun 2024 13:59:27 +0200 Subject: [PATCH] Fix plugin with `/` in the name (#553) * Fix plugin with `/` in the name --- composer.json | 1 + src/Lib/Route/RouteDecorator.php | 2 +- .../TestCase/Lib/Route/RouteDecoratorTest.php | 23 ++++++ .../Special/src/Controller/MyController.php | 19 +++++ .../plugins/Orgname/Special/src/Plugin.php | 81 +++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 tests/test_app/plugins/Orgname/Special/src/Controller/MyController.php create mode 100644 tests/test_app/plugins/Orgname/Special/src/Plugin.php diff --git a/composer.json b/composer.json index 1ebbbba3..921b0dcf 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,7 @@ "SwaggerBake\\Test\\": "tests/", "SwaggerBakeTest\\App\\": "tests/test_app/src/", "Demo\\": "tests/test_app/plugins/Demo/src/", + "Orgname\\Special\\": "tests/test_app/plugins/Orgname/Special/src/", "Cake\\Test\\": "vendor/cakephp/cakephp/tests/" } }, diff --git a/src/Lib/Route/RouteDecorator.php b/src/Lib/Route/RouteDecorator.php index 98b10212..d04a8c31 100644 --- a/src/Lib/Route/RouteDecorator.php +++ b/src/Lib/Route/RouteDecorator.php @@ -350,7 +350,7 @@ private function findControllerFqn(): ?string } $app = $this->cakeConfigure::read('App.namespace'); - $fqn = $this->plugin ? $this->plugin . '\\' : $app . '\\'; + $fqn = $this->plugin ? str_replace('/', '\\', $this->plugin) . '\\' : $app . '\\'; $fqn .= 'Controller\\'; $fqn .= $this->prefix ? str_replace('/', '\\', $this->prefix) . '\\' : ''; diff --git a/tests/TestCase/Lib/Route/RouteDecoratorTest.php b/tests/TestCase/Lib/Route/RouteDecoratorTest.php index 83aafb6f..5cfaf3eb 100644 --- a/tests/TestCase/Lib/Route/RouteDecoratorTest.php +++ b/tests/TestCase/Lib/Route/RouteDecoratorTest.php @@ -91,4 +91,27 @@ public function test_prefixes_with_forward_slash(): void $routeDecorator->getControllerFqn() ); } + + /** + * When `__construct` is called with a route plugin containing a forward `/` slash, the slash is flipped `\` and + * the controller FQN is found. + */ + public function test_plugins_with_forward_slash(): void + { + $defaults = [ + 'plugin' => 'Orgname/Special', + 'prefix' => null, + 'controller' => 'My', + 'action' => 'index', + '_method' => ['GET', 'POST'] + ]; + + $routeDecorator = (new RouteDecorator(new Route('/my/index', $defaults))); + $this->assertEquals($defaults['plugin'], $routeDecorator->getPlugin()); + $this->assertEquals($defaults['_method'], $routeDecorator->getMethods()); + $this->assertEquals( + 'Orgname\Special\Controller\MyController', + $routeDecorator->getControllerFqn() + ); + } } \ No newline at end of file diff --git a/tests/test_app/plugins/Orgname/Special/src/Controller/MyController.php b/tests/test_app/plugins/Orgname/Special/src/Controller/MyController.php new file mode 100644 index 00000000..0858cb44 --- /dev/null +++ b/tests/test_app/plugins/Orgname/Special/src/Controller/MyController.php @@ -0,0 +1,19 @@ +plugin( + 'Orgname/Special', + ['path' => '/special'], + function (RouteBuilder $builder) { + // Add custom routes here + $builder->setExtensions(['json','xml']); + $builder->resources('My', ['only' => 'index']); + $builder->fallbacks(); + } + ); + parent::routes($routes); + } + + /** + * Add middleware for the plugin. + * + * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update. + * @return \Cake\Http\MiddlewareQueue + */ + public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue + { + // Add your middlewares here + + return $middlewareQueue; + } + + /** + * Add commands for the plugin. + * + * @param \Cake\Console\CommandCollection $commands The command collection to update. + * @return \Cake\Console\CommandCollection + */ + public function console(CommandCollection $commands) : CommandCollection + { + // Add your commands here + + $commands = parent::console($commands); + + return $commands; + } +}