Skip to content

Commit

Permalink
Fix plugin with / in the name (#553)
Browse files Browse the repository at this point in the history
* Fix plugin with `/` in the name
  • Loading branch information
segy authored Jun 28, 2024
1 parent 9a24990 commit 491ffec
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Route/RouteDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '\\' : '';

Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/Lib/Route/RouteDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Orgname\Special\Controller;

use SwaggerBake\Lib\Attribute\OpenApiResponse;

class MyController
{
/**
* Just an example of a plugin.
*
* @return \Cake\Http\Response
*/
#[OpenApiResponse(description: 'my', mimeTypes: ['text/plain'])]
public function index()
{

}
}
81 changes: 81 additions & 0 deletions tests/test_app/plugins/Orgname/Special/src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);

namespace Orgname\Special;

use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\RouteBuilder;
use Cake\Console\CommandCollection;

/**
* Plugin for Orgname\Special
*/
class Plugin extends BasePlugin
{
/**
* Load all the plugin configuration and bootstrap logic.
*
* The host application is provided as an argument. This allows you to load
* additional plugin dependencies, or attach events.
*
* @param \Cake\Core\PluginApplicationInterface $app The host application
* @return void
*/
public function bootstrap(PluginApplicationInterface $app): void
{
}

/**
* Add routes for the plugin.
*
* If your plugin has many routes and you would like to isolate them into a separate file,
* you can create `$plugin/config/routes.php` and delete this method.
*
* @param \Cake\Routing\RouteBuilder $routes The route builder to update.
* @return void
*/
public function routes(RouteBuilder $routes): void
{
$routes->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;
}
}

0 comments on commit 491ffec

Please sign in to comment.