Skip to content

Commit 8f2c065

Browse files
authored
Merge pull request #8320 from kenjis/fix-spark-routes-auto-routing-translateURIDashes
fix: [Auto Routing Improved] `spark routes` shows incorrect routes when translateURIDashes is enabled
2 parents 2250f03 + 66bb97c commit 8f2c065

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1313

14+
use Config\Routing;
1415
use ReflectionClass;
1516
use ReflectionMethod;
1617

@@ -31,13 +32,18 @@ final class ControllerMethodReader
3132
*/
3233
private array $httpMethods;
3334

35+
private bool $translateURIDashes;
36+
3437
/**
3538
* @param string $namespace the default namespace
3639
*/
3740
public function __construct(string $namespace, array $httpMethods)
3841
{
3942
$this->namespace = $namespace;
4043
$this->httpMethods = $httpMethods;
44+
45+
$config = config(Routing::class);
46+
$this->translateURIDashes = $config->translateURIDashes;
4147
}
4248

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

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

174-
return rtrim($classPath, '/');
180+
$classUri = rtrim($classPath, '/');
181+
182+
if ($this->translateURIDashes) {
183+
$classUri = str_replace('_', '-', $classUri);
184+
}
185+
186+
return $classUri;
187+
}
188+
189+
/**
190+
* @return string URI path part from the method
191+
*/
192+
private function getUriByMethod(string $httpVerb, string $methodName): string
193+
{
194+
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));
195+
196+
if ($this->translateURIDashes) {
197+
$methodUri = str_replace('_', '-', $methodUri);
198+
}
199+
200+
return $methodUri;
175201
}
176202

177203
/**

tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1313

14+
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller;
1415
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
16+
use CodeIgniter\Config\Factories;
1517
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\Routing;
1619
use Tests\Support\Controllers\Newautorouting;
1720
use Tests\Support\Controllers\Remap;
1821

@@ -66,6 +69,43 @@ public function testRead(): void
6669
$this->assertSame($expected, $routes);
6770
}
6871

72+
public function testReadTranslateURIDashes(): void
73+
{
74+
$config = config(Routing::class);
75+
$config->translateURIDashes = true;
76+
Factories::injectMock('config', Routing::class, $config);
77+
78+
$reader = $this->createControllerMethodReader(
79+
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
80+
);
81+
82+
$routes = $reader->read(Dash_controller::class);
83+
84+
$expected = [
85+
0 => [
86+
'method' => 'get',
87+
'route' => 'dash-folder/dash-controller/somemethod',
88+
'route_params' => '[/..]',
89+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getSomemethod',
90+
'params' => [
91+
'p1' => false,
92+
],
93+
],
94+
[
95+
'method' => 'get',
96+
'route' => 'dash-folder/dash-controller/dash-method',
97+
'route_params' => '/..[/..]',
98+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getDash_method',
99+
'params' => [
100+
'p1' => true,
101+
'p2' => false,
102+
],
103+
],
104+
];
105+
106+
$this->assertSame($expected, $routes);
107+
}
108+
69109
public function testReadDefaultController(): void
70110
{
71111
$reader = $this->createControllerMethodReader(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder;
15+
16+
use CodeIgniter\Controller;
17+
18+
class Dash_controller extends Controller
19+
{
20+
public function getSomemethod($p1 = ''): void
21+
{
22+
}
23+
24+
public function getDash_method($p1, $p2 = ''): void
25+
{
26+
}
27+
}

0 commit comments

Comments
 (0)