Skip to content

Commit eef7990

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
2 parents bc97a0e + 8f2c065 commit eef7990

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
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1515

16+
use Config\Routing;
1617
use ReflectionClass;
1718
use ReflectionMethod;
1819

@@ -33,13 +34,18 @@ final class ControllerMethodReader
3334
*/
3435
private array $httpMethods;
3536

37+
private bool $translateURIDashes;
38+
3639
/**
3740
* @param string $namespace the default namespace
3841
*/
3942
public function __construct(string $namespace, array $httpMethods)
4043
{
4144
$this->namespace = $namespace;
4245
$this->httpMethods = $httpMethods;
46+
47+
$config = config(Routing::class);
48+
$this->translateURIDashes = $config->translateURIDashes;
4349
}
4450

4551
/**
@@ -69,7 +75,7 @@ public function read(string $class, string $defaultController = 'Home', string $
6975
foreach ($this->httpMethods as $httpVerb) {
7076
if (strpos($methodName, strtolower($httpVerb)) === 0) {
7177
// Remove HTTP verb prefix.
72-
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));
78+
$methodInUri = $this->getUriByMethod($httpVerb, $methodName);
7379

7480
// Check if it is the default method.
7581
if ($methodInUri === $defaultMethod) {
@@ -173,7 +179,27 @@ private function getUriByClass(string $classname): string
173179
$classPath .= lcfirst($part) . '/';
174180
}
175181

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

179205
/**

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

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

1414
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1515

16+
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller;
1617
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
18+
use CodeIgniter\Config\Factories;
1719
use CodeIgniter\Test\CIUnitTestCase;
20+
use Config\Routing;
1821
use Tests\Support\Controllers\Newautorouting;
1922
use Tests\Support\Controllers\Remap;
2023

@@ -68,6 +71,43 @@ public function testRead(): void
6871
$this->assertSame($expected, $routes);
6972
}
7073

74+
public function testReadTranslateURIDashes(): void
75+
{
76+
$config = config(Routing::class);
77+
$config->translateURIDashes = true;
78+
Factories::injectMock('config', Routing::class, $config);
79+
80+
$reader = $this->createControllerMethodReader(
81+
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
82+
);
83+
84+
$routes = $reader->read(Dash_controller::class);
85+
86+
$expected = [
87+
0 => [
88+
'method' => 'get',
89+
'route' => 'dash-folder/dash-controller/somemethod',
90+
'route_params' => '[/..]',
91+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getSomemethod',
92+
'params' => [
93+
'p1' => false,
94+
],
95+
],
96+
[
97+
'method' => 'get',
98+
'route' => 'dash-folder/dash-controller/dash-method',
99+
'route_params' => '/..[/..]',
100+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getDash_method',
101+
'params' => [
102+
'p1' => true,
103+
'p2' => false,
104+
],
105+
],
106+
];
107+
108+
$this->assertSame($expected, $routes);
109+
}
110+
71111
public function testReadDefaultController(): void
72112
{
73113
$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)