Skip to content

Commit

Permalink
Merge pull request #5889 from kenjis/feat-new-auto-router
Browse files Browse the repository at this point in the history
feat: new improved auto router
  • Loading branch information
kenjis authored May 3, 2022
2 parents cdf427f + 2fe5107 commit ab63c8d
Show file tree
Hide file tree
Showing 23 changed files with 1,233 additions and 76 deletions.
7 changes: 6 additions & 1 deletion app/Config/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Feature extends BaseConfig
{
/**
* Enable multiple filters for a route or not
* Enable multiple filters for a route or not.
*
* If you enable this:
* - CodeIgniter\CodeIgniter::handleRequest() uses:
Expand All @@ -24,4 +24,9 @@ class Feature extends BaseConfig
* @var bool
*/
public $multipleFilters = false;

/**
* Use improved new auto routing instead of the default legacy version.
*/
public bool $autoRoutesImproved = false;
}
6 changes: 3 additions & 3 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ parameters:

-
message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getDefaultNamespace\\(\\)\\.$#"
count: 2
count: 3
path: system/Router/Router.php

-
Expand Down Expand Up @@ -706,8 +706,8 @@ parameters:
path: system/Router/Router.php

-
message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRegisteredControllers\\(\\)\\.$#"
count: 1
message: "#^Call to an undefined method CodeIgniter\\\\Router\\\\RouteCollectionInterface\\:\\:getRegisteredControllers\\(.*\\)\\.$#"
count: 2
path: system/Router/Router.php

-
Expand Down
26 changes: 17 additions & 9 deletions system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,46 @@
/**
* Router for Auto-Routing
*/
class AutoRouter
final class AutoRouter implements AutoRouterInterface
{
/**
* List of controllers registered for the CLI verb that should not be accessed in the web.
*
* @var class-string[]
*/
protected array $protectedControllers;
private array $protectedControllers;

/**
* Sub-directory that contains the requested controller class.
* Primarily used by 'autoRoute'.
*/
protected ?string $directory = null;
private ?string $directory = null;

/**
* The name of the controller class.
*/
protected string $controller;
private string $controller;

/**
* The name of the method to use.
*/
protected string $method;
private string $method;

/**
* Whether dashes in URI's should be converted
* to underscores when determining method names.
*/
protected bool $translateURIDashes;
private bool $translateURIDashes;

/**
* HTTP verb for the request.
*/
protected string $httpVerb;
private string $httpVerb;

/**
* Default namespace for controllers.
*/
protected string $defaultNamespace;
private string $defaultNamespace;

public function __construct(
array $protectedControllers,
Expand Down Expand Up @@ -164,6 +166,8 @@ public function getRoute(string $uri): array
/**
* Tells the system whether we should translate URI dashes or not
* in the URI from a dash to an underscore.
*
* @deprecated This method should be removed.
*/
public function setTranslateURIDashes(bool $val = false): self
{
Expand All @@ -179,7 +183,7 @@ public function setTranslateURIDashes(bool $val = false): self
*
* @return array returns an array of remaining uri segments that don't map onto a directory
*/
protected function scanControllers(array $segments): array
private function scanControllers(array $segments): array
{
$segments = array_filter($segments, static fn ($segment) => $segment !== '');
// numerically reindex the array, removing gaps
Expand Down Expand Up @@ -234,6 +238,8 @@ private function isValidSegment(string $segment): bool
* Sets the sub-directory that the controller is in.
*
* @param bool $validate if true, checks to make sure $dir consists of only PSR4 compliant segments
*
* @deprecated This method should be removed.
*/
public function setDirectory(?string $dir = null, bool $append = false, bool $validate = true)
{
Expand Down Expand Up @@ -263,6 +269,8 @@ public function setDirectory(?string $dir = null, bool $append = false, bool $va
/**
* Returns the name of the sub-directory the controller is in,
* if any. Relative to APPPATH.'Controllers'.
*
* @deprecated This method should be removed.
*/
public function directory(): string
{
Expand Down
Loading

0 comments on commit ab63c8d

Please sign in to comment.