Skip to content

Commit 0efc561

Browse files
authored
Merge pull request #8321 from kenjis/feat-translateUriToCamelCase
feat: [Auto Routing Improved] add option to translate uri to camel case
2 parents eef7990 + 4f419cd commit 0efc561

File tree

13 files changed

+495
-39
lines changed

13 files changed

+495
-39
lines changed

app/Config/Routing.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Routing extends BaseRouting
2020
{
2121
/**
22+
* For Defined Routes.
2223
* An array of files that contain route definitions.
2324
* Route files are read in order, with the first match
2425
* found taking precedence.
@@ -30,6 +31,7 @@ class Routing extends BaseRouting
3031
];
3132

3233
/**
34+
* For Defined Routes and Auto Routing.
3335
* The default namespace to use for Controllers when no other
3436
* namespace has been specified.
3537
*
@@ -38,6 +40,7 @@ class Routing extends BaseRouting
3840
public string $defaultNamespace = 'App\Controllers';
3941

4042
/**
43+
* For Auto Routing.
4144
* The default controller to use when no other controller has been
4245
* specified.
4346
*
@@ -46,6 +49,7 @@ class Routing extends BaseRouting
4649
public string $defaultController = 'Home';
4750

4851
/**
52+
* For Defined Routes and Auto Routing.
4953
* The default method to call on the controller when no other
5054
* method has been set in the route.
5155
*
@@ -54,7 +58,8 @@ class Routing extends BaseRouting
5458
public string $defaultMethod = 'index';
5559

5660
/**
57-
* Whether to translate dashes in URIs to underscores.
61+
* For Auto Routing.
62+
* Whether to translate dashes in URIs for controller/method to underscores.
5863
* Primarily useful when using the auto-routing.
5964
*
6065
* Default: false
@@ -91,6 +96,7 @@ class Routing extends BaseRouting
9196
public bool $autoRoute = false;
9297

9398
/**
99+
* For Defined Routes.
94100
* If TRUE, will enable the use of the 'prioritize' option
95101
* when defining routes.
96102
*
@@ -99,7 +105,8 @@ class Routing extends BaseRouting
99105
public bool $prioritize = false;
100106

101107
/**
102-
* Map of URI segments and namespaces. For Auto Routing (Improved).
108+
* For Auto Routing (Improved).
109+
* Map of URI segments and namespaces.
103110
*
104111
* The key is the first URI segment. The value is the controller namespace.
105112
* E.g.,
@@ -110,4 +117,15 @@ class Routing extends BaseRouting
110117
* @var array [ uri_segment => namespace ]
111118
*/
112119
public array $moduleRoutes = [];
120+
121+
/**
122+
* For Auto Routing (Improved).
123+
* Whether to translate dashes in URIs for controller/method to CamelCase.
124+
* E.g., blog-controller -> BlogController
125+
*
126+
* If you enable this, $translateURIDashes is ignored.
127+
*
128+
* Default: false
129+
*/
130+
public bool $translateUriToCamelCase = false;
113131
}

phpstan-baseline.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,11 +2606,6 @@
26062606
'count' => 1,
26072607
'path' => __DIR__ . '/system/Router/AutoRouter.php',
26082608
];
2609-
$ignoreErrors[] = [
2610-
'message' => '#^Only booleans are allowed in &&, Config\\\\Routing given on the right side\\.$#',
2611-
'count' => 1,
2612-
'path' => __DIR__ . '/system/Router/AutoRouterImproved.php',
2613-
];
26142609
$ignoreErrors[] = [
26152610
'message' => '#^PHPDoc type int of property CodeIgniter\\\\Router\\\\Exceptions\\\\RedirectException\\:\\:\\$code is not the same as PHPDoc type mixed of overridden property Exception\\:\\:\\$code\\.$#',
26162611
'count' => 1,

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ final class ControllerMethodReader
3535
private array $httpMethods;
3636

3737
private bool $translateURIDashes;
38+
private bool $translateUriToCamelCase;
3839

3940
/**
4041
* @param string $namespace the default namespace
@@ -44,8 +45,9 @@ public function __construct(string $namespace, array $httpMethods)
4445
$this->namespace = $namespace;
4546
$this->httpMethods = $httpMethods;
4647

47-
$config = config(Routing::class);
48-
$this->translateURIDashes = $config->translateURIDashes;
48+
$config = config(Routing::class);
49+
$this->translateURIDashes = $config->translateURIDashes;
50+
$this->translateUriToCamelCase = $config->translateUriToCamelCase;
4951
}
5052

5153
/**
@@ -67,15 +69,15 @@ public function read(string $class, string $defaultController = 'Home', string $
6769
$classShortname = $reflection->getShortName();
6870

6971
$output = [];
70-
$classInUri = $this->getUriByClass($classname);
72+
$classInUri = $this->convertClassNameToUri($classname);
7173

7274
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
7375
$methodName = $method->getName();
7476

7577
foreach ($this->httpMethods as $httpVerb) {
7678
if (strpos($methodName, strtolower($httpVerb)) === 0) {
7779
// Remove HTTP verb prefix.
78-
$methodInUri = $this->getUriByMethod($httpVerb, $methodName);
80+
$methodInUri = $this->convertMethodNameToUri($httpVerb, $methodName);
7981

8082
// Check if it is the default method.
8183
if ($methodInUri === $defaultMethod) {
@@ -164,7 +166,7 @@ private function getParameters(ReflectionMethod $method): array
164166
*
165167
* @return string URI path part from the folder(s) and controller
166168
*/
167-
private function getUriByClass(string $classname): string
169+
private function convertClassNameToUri(string $classname): string
168170
{
169171
// remove the namespace
170172
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
@@ -181,25 +183,33 @@ private function getUriByClass(string $classname): string
181183

182184
$classUri = rtrim($classPath, '/');
183185

184-
if ($this->translateURIDashes) {
185-
$classUri = str_replace('_', '-', $classUri);
186-
}
187-
188-
return $classUri;
186+
return $this->translateToUri($classUri);
189187
}
190188

191189
/**
192190
* @return string URI path part from the method
193191
*/
194-
private function getUriByMethod(string $httpVerb, string $methodName): string
192+
private function convertMethodNameToUri(string $httpVerb, string $methodName): string
195193
{
196194
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));
197195

198-
if ($this->translateURIDashes) {
199-
$methodUri = str_replace('_', '-', $methodUri);
196+
return $this->translateToUri($methodUri);
197+
}
198+
199+
/**
200+
* @param string $string classname or method name
201+
*/
202+
private function translateToUri(string $string): string
203+
{
204+
if ($this->translateUriToCamelCase) {
205+
$string = strtolower(
206+
preg_replace('/([a-z\d])([A-Z])/', '$1-$2', $string)
207+
);
208+
} elseif ($this->translateURIDashes) {
209+
$string = str_replace('_', '-', $string);
200210
}
201211

202-
return $methodUri;
212+
return $string;
203213
}
204214

205215
/**

system/Config/Routing.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class Routing extends BaseConfig
2020
{
2121
/**
22+
* For Defined Routes.
2223
* An array of files that contain route definitions.
2324
* Route files are read in order, with the first match
2425
* found taking precedence.
@@ -30,6 +31,7 @@ class Routing extends BaseConfig
3031
];
3132

3233
/**
34+
* For Defined Routes and Auto Routing.
3335
* The default namespace to use for Controllers when no other
3436
* namespace has been specified.
3537
*
@@ -38,6 +40,7 @@ class Routing extends BaseConfig
3840
public string $defaultNamespace = 'App\Controllers';
3941

4042
/**
43+
* For Auto Routing.
4144
* The default controller to use when no other controller has been
4245
* specified.
4346
*
@@ -46,6 +49,7 @@ class Routing extends BaseConfig
4649
public string $defaultController = 'Home';
4750

4851
/**
52+
* For Defined Routes and Auto Routing.
4953
* The default method to call on the controller when no other
5054
* method has been set in the route.
5155
*
@@ -54,7 +58,8 @@ class Routing extends BaseConfig
5458
public string $defaultMethod = 'index';
5559

5660
/**
57-
* Whether to translate dashes in URIs to underscores.
61+
* For Auto Routing.
62+
* Whether to translate dashes in URIs for controller/method to underscores.
5863
* Primarily useful when using the auto-routing.
5964
*
6065
* Default: false
@@ -91,6 +96,7 @@ class Routing extends BaseConfig
9196
public bool $autoRoute = false;
9297

9398
/**
99+
* For Defined Routes.
94100
* If TRUE, will enable the use of the 'prioritize' option
95101
* when defining routes.
96102
*
@@ -99,7 +105,8 @@ class Routing extends BaseConfig
99105
public bool $prioritize = false;
100106

101107
/**
102-
* Map of URI segments and namespaces. For Auto Routing (Improved).
108+
* For Auto Routing (Improved).
109+
* Map of URI segments and namespaces.
103110
*
104111
* The key is the first URI segment. The value is the controller namespace.
105112
* E.g.,
@@ -110,4 +117,15 @@ class Routing extends BaseConfig
110117
* @var array [ uri_segment => namespace ]
111118
*/
112119
public array $moduleRoutes = [];
120+
121+
/**
122+
* For Auto Routing (Improved).
123+
* Whether to translate dashes in URIs for controller/method to CamelCase.
124+
* E.g., blog-controller -> BlogController
125+
*
126+
* If you enable this, $translateURIDashes is ignored.
127+
*
128+
* Default: false
129+
*/
130+
public bool $translateUriToCamelCase = false;
113131
}

0 commit comments

Comments
 (0)