Skip to content

Commit

Permalink
Merge pull request #8321 from kenjis/feat-translateUriToCamelCase
Browse files Browse the repository at this point in the history
feat: [Auto Routing Improved] add option to translate uri to camel case
  • Loading branch information
kenjis authored Dec 14, 2023
2 parents eef7990 + 4f419cd commit 0efc561
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 39 deletions.
22 changes: 20 additions & 2 deletions app/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Routing extends BaseRouting
{
/**
* For Defined Routes.
* An array of files that contain route definitions.
* Route files are read in order, with the first match
* found taking precedence.
Expand All @@ -30,6 +31,7 @@ class Routing extends BaseRouting
];

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

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

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

/**
* Whether to translate dashes in URIs to underscores.
* For Auto Routing.
* Whether to translate dashes in URIs for controller/method to underscores.
* Primarily useful when using the auto-routing.
*
* Default: false
Expand Down Expand Up @@ -91,6 +96,7 @@ class Routing extends BaseRouting
public bool $autoRoute = false;

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

/**
* Map of URI segments and namespaces. For Auto Routing (Improved).
* For Auto Routing (Improved).
* Map of URI segments and namespaces.
*
* The key is the first URI segment. The value is the controller namespace.
* E.g.,
Expand All @@ -110,4 +117,15 @@ class Routing extends BaseRouting
* @var array [ uri_segment => namespace ]
*/
public array $moduleRoutes = [];

/**
* For Auto Routing (Improved).
* Whether to translate dashes in URIs for controller/method to CamelCase.
* E.g., blog-controller -> BlogController
*
* If you enable this, $translateURIDashes is ignored.
*
* Default: false
*/
public bool $translateUriToCamelCase = false;
}
5 changes: 0 additions & 5 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2606,11 +2606,6 @@
'count' => 1,
'path' => __DIR__ . '/system/Router/AutoRouter.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in &&, Config\\\\Routing given on the right side\\.$#',
'count' => 1,
'path' => __DIR__ . '/system/Router/AutoRouterImproved.php',
];
$ignoreErrors[] = [
'message' => '#^PHPDoc type int of property CodeIgniter\\\\Router\\\\Exceptions\\\\RedirectException\\:\\:\\$code is not the same as PHPDoc type mixed of overridden property Exception\\:\\:\\$code\\.$#',
'count' => 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class ControllerMethodReader
private array $httpMethods;

private bool $translateURIDashes;
private bool $translateUriToCamelCase;

/**
* @param string $namespace the default namespace
Expand All @@ -44,8 +45,9 @@ public function __construct(string $namespace, array $httpMethods)
$this->namespace = $namespace;
$this->httpMethods = $httpMethods;

$config = config(Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
$config = config(Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
$this->translateUriToCamelCase = $config->translateUriToCamelCase;
}

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

$output = [];
$classInUri = $this->getUriByClass($classname);
$classInUri = $this->convertClassNameToUri($classname);

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

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

// Check if it is the default method.
if ($methodInUri === $defaultMethod) {
Expand Down Expand Up @@ -164,7 +166,7 @@ private function getParameters(ReflectionMethod $method): array
*
* @return string URI path part from the folder(s) and controller
*/
private function getUriByClass(string $classname): string
private function convertClassNameToUri(string $classname): string
{
// remove the namespace
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
Expand All @@ -181,25 +183,33 @@ private function getUriByClass(string $classname): string

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

if ($this->translateURIDashes) {
$classUri = str_replace('_', '-', $classUri);
}

return $classUri;
return $this->translateToUri($classUri);
}

/**
* @return string URI path part from the method
*/
private function getUriByMethod(string $httpVerb, string $methodName): string
private function convertMethodNameToUri(string $httpVerb, string $methodName): string
{
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));

if ($this->translateURIDashes) {
$methodUri = str_replace('_', '-', $methodUri);
return $this->translateToUri($methodUri);
}

/**
* @param string $string classname or method name
*/
private function translateToUri(string $string): string
{
if ($this->translateUriToCamelCase) {
$string = strtolower(
preg_replace('/([a-z\d])([A-Z])/', '$1-$2', $string)
);
} elseif ($this->translateURIDashes) {
$string = str_replace('_', '-', $string);
}

return $methodUri;
return $string;
}

/**
Expand Down
22 changes: 20 additions & 2 deletions system/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Routing extends BaseConfig
{
/**
* For Defined Routes.
* An array of files that contain route definitions.
* Route files are read in order, with the first match
* found taking precedence.
Expand All @@ -30,6 +31,7 @@ class Routing extends BaseConfig
];

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

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

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

/**
* Whether to translate dashes in URIs to underscores.
* For Auto Routing.
* Whether to translate dashes in URIs for controller/method to underscores.
* Primarily useful when using the auto-routing.
*
* Default: false
Expand Down Expand Up @@ -91,6 +96,7 @@ class Routing extends BaseConfig
public bool $autoRoute = false;

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

/**
* Map of URI segments and namespaces. For Auto Routing (Improved).
* For Auto Routing (Improved).
* Map of URI segments and namespaces.
*
* The key is the first URI segment. The value is the controller namespace.
* E.g.,
Expand All @@ -110,4 +117,15 @@ class Routing extends BaseConfig
* @var array [ uri_segment => namespace ]
*/
public array $moduleRoutes = [];

/**
* For Auto Routing (Improved).
* Whether to translate dashes in URIs for controller/method to CamelCase.
* E.g., blog-controller -> BlogController
*
* If you enable this, $translateURIDashes is ignored.
*
* Default: false
*/
public bool $translateUriToCamelCase = false;
}
Loading

0 comments on commit 0efc561

Please sign in to comment.