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: [Auto Routing Improved] spark routes shows incorrect routes when translateURIDashes is enabled #8320

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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;

use Config\Routing;
use ReflectionClass;
use ReflectionMethod;

Expand All @@ -31,13 +32,18 @@ final class ControllerMethodReader
*/
private array $httpMethods;

private bool $translateURIDashes;

/**
* @param string $namespace the default namespace
*/
public function __construct(string $namespace, array $httpMethods)
{
$this->namespace = $namespace;
$this->httpMethods = $httpMethods;

$config = config(Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
}

/**
Expand Down Expand Up @@ -67,7 +73,7 @@ public function read(string $class, string $defaultController = 'Home', string $
foreach ($this->httpMethods as $httpVerb) {
if (strpos($methodName, $httpVerb) === 0) {
// Remove HTTP verb prefix.
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));
$methodInUri = $this->getUriByMethod($httpVerb, $methodName);

// Check if it is the default method.
if ($methodInUri === $defaultMethod) {
Expand Down Expand Up @@ -171,7 +177,27 @@ private function getUriByClass(string $classname): string
$classPath .= lcfirst($part) . '/';
}

return rtrim($classPath, '/');
$classUri = rtrim($classPath, '/');

if ($this->translateURIDashes) {
$classUri = str_replace('_', '-', $classUri);
}

return $classUri;
}

/**
* @return string URI path part from the method
*/
private function getUriByMethod(string $httpVerb, string $methodName): string
{
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));

if ($this->translateURIDashes) {
$methodUri = str_replace('_', '-', $methodUri);
}

return $methodUri;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;

use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller;
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Routing;
use Tests\Support\Controllers\Newautorouting;
use Tests\Support\Controllers\Remap;

Expand Down Expand Up @@ -66,6 +69,43 @@ public function testRead(): void
$this->assertSame($expected, $routes);
}

public function testReadTranslateURIDashes(): void
{
$config = config(Routing::class);
$config->translateURIDashes = true;
Factories::injectMock('config', Routing::class, $config);

$reader = $this->createControllerMethodReader(
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
);

$routes = $reader->read(Dash_controller::class);

$expected = [
0 => [
'method' => 'get',
'route' => 'dash-folder/dash-controller/somemethod',
'route_params' => '[/..]',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getSomemethod',
'params' => [
'p1' => false,
],
],
[
'method' => 'get',
'route' => 'dash-folder/dash-controller/dash-method',
'route_params' => '/..[/..]',
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getDash_method',
'params' => [
'p1' => true,
'p2' => false,
],
],
];

$this->assertSame($expected, $routes);
}

public function testReadDefaultController(): void
{
$reader = $this->createControllerMethodReader(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder;

use CodeIgniter\Controller;

class Dash_controller extends Controller
{
public function getSomemethod($p1 = ''): void
{
}

public function getDash_method($p1, $p2 = ''): void
{
}
}
Loading