Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin with / in the name #553

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading