Skip to content

Commit

Permalink
Merge pull request #8320 from kenjis/fix-spark-routes-auto-routing-tr…
Browse files Browse the repository at this point in the history
…anslateURIDashes

fix: [Auto Routing Improved] `spark routes` shows incorrect routes when translateURIDashes is enabled
  • Loading branch information
kenjis authored Dec 11, 2023
2 parents 2250f03 + 66bb97c commit 8f2c065
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
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
{
}
}

0 comments on commit 8f2c065

Please sign in to comment.