@@ -36,6 +36,8 @@ final class ControllerMethodReader
36
36
37
37
private bool $ translateURIDashes ;
38
38
39
+ private bool $ translateUriToCamelCase ;
40
+
39
41
/**
40
42
* @param string $namespace the default namespace
41
43
*/
@@ -44,8 +46,9 @@ public function __construct(string $namespace, array $httpMethods)
44
46
$ this ->namespace = $ namespace ;
45
47
$ this ->httpMethods = $ httpMethods ;
46
48
47
- $ config = config (Routing::class);
48
- $ this ->translateURIDashes = $ config ->translateURIDashes ;
49
+ $ config = config (Routing::class);
50
+ $ this ->translateURIDashes = $ config ->translateURIDashes ;
51
+ $ this ->translateUriToCamelCase = $ config ->translateUriToCamelCase ;
49
52
}
50
53
51
54
/**
@@ -67,15 +70,15 @@ public function read(string $class, string $defaultController = 'Home', string $
67
70
$ classShortname = $ reflection ->getShortName ();
68
71
69
72
$ output = [];
70
- $ classInUri = $ this ->getUriByClass ($ classname );
73
+ $ classInUri = $ this ->convertClassNameToUri ($ classname );
71
74
72
75
foreach ($ reflection ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ method ) {
73
76
$ methodName = $ method ->getName ();
74
77
75
78
foreach ($ this ->httpMethods as $ httpVerb ) {
76
79
if (strpos ($ methodName , strtolower ($ httpVerb )) === 0 ) {
77
80
// Remove HTTP verb prefix.
78
- $ methodInUri = $ this ->getUriByMethod ($ httpVerb , $ methodName );
81
+ $ methodInUri = $ this ->convertMethodNameToUri ($ httpVerb , $ methodName );
79
82
80
83
// Check if it is the default method.
81
84
if ($ methodInUri === $ defaultMethod ) {
@@ -164,7 +167,7 @@ private function getParameters(ReflectionMethod $method): array
164
167
*
165
168
* @return string URI path part from the folder(s) and controller
166
169
*/
167
- private function getUriByClass (string $ classname ): string
170
+ private function convertClassNameToUri (string $ classname ): string
168
171
{
169
172
// remove the namespace
170
173
$ pattern = '/ ' . preg_quote ($ this ->namespace , '/ ' ) . '/ ' ;
@@ -181,25 +184,33 @@ private function getUriByClass(string $classname): string
181
184
182
185
$ classUri = rtrim ($ classPath , '/ ' );
183
186
184
- if ($ this ->translateURIDashes ) {
185
- $ classUri = str_replace ('_ ' , '- ' , $ classUri );
186
- }
187
-
188
- return $ classUri ;
187
+ return $ this ->translateToUri ($ classUri );
189
188
}
190
189
191
190
/**
192
191
* @return string URI path part from the method
193
192
*/
194
- private function getUriByMethod (string $ httpVerb , string $ methodName ): string
193
+ private function convertMethodNameToUri (string $ httpVerb , string $ methodName ): string
195
194
{
196
195
$ methodUri = lcfirst (substr ($ methodName , strlen ($ httpVerb )));
197
196
198
- if ($ this ->translateURIDashes ) {
199
- $ methodUri = str_replace ('_ ' , '- ' , $ methodUri );
197
+ return $ this ->translateToUri ($ methodUri );
198
+ }
199
+
200
+ /**
201
+ * @param string $string classname or method name
202
+ */
203
+ private function translateToUri (string $ string ): string
204
+ {
205
+ if ($ this ->translateUriToCamelCase ) {
206
+ $ string = strtolower (
207
+ preg_replace ('/([a-z\d])([A-Z])/ ' , '$1-$2 ' , $ string )
208
+ );
209
+ } elseif ($ this ->translateURIDashes ) {
210
+ $ string = str_replace ('_ ' , '- ' , $ string );
200
211
}
201
212
202
- return $ methodUri ;
213
+ return $ string ;
203
214
}
204
215
205
216
/**
0 commit comments