Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: HTTP verbs in Router #8317

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions system/Router/AutoRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ final class AutoRouter implements AutoRouterInterface
*/
private bool $translateURIDashes;

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

/**
* Default namespace for controllers.
*/
Expand All @@ -65,13 +60,11 @@ public function __construct(
string $defaultNamespace,
string $defaultController,
string $defaultMethod,
bool $translateURIDashes,
string $httpVerb
bool $translateURIDashes
) {
$this->cliRoutes = $cliRoutes;
$this->defaultNamespace = $defaultNamespace;
$this->translateURIDashes = $translateURIDashes;
$this->httpVerb = $httpVerb;

$this->controller = $defaultController;
$this->method = $defaultMethod;
Expand All @@ -81,6 +74,8 @@ public function __construct(
* Attempts to match a URI path against Controllers and directories
* found in APPPATH/Controllers, to find a matching route.
*
* @param string $httpVerb HTTP verb like `GET`,`POST`
*
* @return array [directory_name, controller_name, controller_method, params]
*/
public function getRoute(string $uri, string $httpVerb): array
Expand Down Expand Up @@ -122,7 +117,7 @@ public function getRoute(string $uri, string $httpVerb): array
}

// Ensure routes registered via $routes->cli() are not accessible via web.
if ($this->httpVerb !== 'CLI') {
if ($httpVerb !== 'CLI') {
$controller = '\\' . $this->defaultNamespace;

$controller .= $this->directory ? str_replace('/', '\\', $this->directory) : '';
Expand Down
9 changes: 4 additions & 5 deletions system/Router/AutoRouterImproved.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ final class AutoRouterImproved implements AutoRouterInterface
/**
* @param class-string[] $protectedControllers
* @param string $defaultController Short classname
*
* @deprecated $httpVerb is deprecated. No longer used.
*/
public function __construct(// @phpstan-ignore-line
public function __construct(
array $protectedControllers,
string $namespace,
string $defaultController,
string $defaultMethod,
bool $translateURIDashes,
string $httpVerb
bool $translateURIDashes
) {
$this->protectedControllers = $protectedControllers;
$this->namespace = rtrim($namespace, '\\');
Expand Down Expand Up @@ -243,6 +240,8 @@ private function searchLastDefaultController(): bool
/**
* Finds controller, method and params from the URI.
*
* @param string $httpVerb HTTP verb like `GET`,`POST`
*
* @return array [directory_name, controller_name, controller_method, params]
*/
public function getRoute(string $uri, string $httpVerb): array
Expand Down
6 changes: 2 additions & 4 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,15 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
$this->collection->getDefaultNamespace(),
$this->collection->getDefaultController(),
$this->collection->getDefaultMethod(),
$this->translateURIDashes,
$this->collection->getHTTPVerb()
$this->translateURIDashes
);
} else {
$this->autoRouter = new AutoRouter(
$this->collection->getRoutes('CLI', false), // @phpstan-ignore-line
$this->collection->getDefaultNamespace(),
$this->collection->getDefaultController(),
$this->collection->getDefaultMethod(),
$this->translateURIDashes,
$this->collection->getHTTPVerb()
$this->translateURIDashes
);
}
}
Expand Down
68 changes: 34 additions & 34 deletions tests/system/Router/AutoRouterImprovedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Method;
use CodeIgniter\Router\Controllers\Dash_folder\Dash_controller;
use CodeIgniter\Router\Controllers\Dash_folder\Home;
use CodeIgniter\Router\Controllers\Index;
Expand All @@ -42,15 +43,14 @@ protected function setUp(): void
$this->collection = new RouteCollection(Services::locator(), $moduleConfig, new Routing());
}

private function createNewAutoRouter(string $httpVerb = 'get', $namespace = 'CodeIgniter\Router\Controllers'): AutoRouterImproved
private function createNewAutoRouter($namespace = 'CodeIgniter\Router\Controllers'): AutoRouterImproved
{
return new AutoRouterImproved(
[],
$namespace,
$this->collection->getDefaultController(),
$this->collection->getDefaultMethod(),
true,
$httpVerb
true
);
}

Expand All @@ -61,7 +61,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodGet(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('/', 'get');
= $router->getRoute('/', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Index::class, $controller);
Expand All @@ -84,10 +84,10 @@ public function testAutoRouteFindsModuleDefaultControllerAndMethodGet()

$this->collection->setDefaultController('Index');

$router = $this->createNewAutoRouter('get', 'App/Controllers');
$router = $this->createNewAutoRouter('App/Controllers');

[$directory, $controller, $method, $params]
= $router->getRoute('test', 'get');
= $router->getRoute('test', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Index::class, $controller);
Expand All @@ -99,10 +99,10 @@ public function testAutoRouteFindsDefaultControllerAndMethodPost(): void
{
$this->collection->setDefaultController('Index');

$router = $this->createNewAutoRouter('post');
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('/', 'post');
= $router->getRoute('/', Method::POST);

$this->assertNull($directory);
$this->assertSame('\\' . Index::class, $controller);
Expand All @@ -115,7 +115,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('mycontroller/somemethod', 'get');
= $router->getRoute('mycontroller/somemethod', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Mycontroller::class, $controller);
Expand All @@ -133,7 +133,7 @@ public function testFindsControllerAndMethodAndParam(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('mycontroller/somemethod/a', 'get');
= $router->getRoute('mycontroller/somemethod/a', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Mycontroller::class, $controller);
Expand All @@ -155,15 +155,15 @@ public function testUriParamCountIsGreaterThanMethodParams(): void

$router = $this->createNewAutoRouter();

$router->getRoute('mycontroller/somemethod/a/b', 'get');
$router->getRoute('mycontroller/somemethod/a/b', Method::GET);
}

public function testAutoRouteFindsControllerWithFile(): void
{
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('mycontroller', 'get');
= $router->getRoute('mycontroller', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Mycontroller::class, $controller);
Expand All @@ -176,7 +176,7 @@ public function testAutoRouteFindsControllerWithSubfolder(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder/mycontroller/somemethod', 'get');
= $router->getRoute('subfolder/mycontroller/somemethod', Method::GET);

$this->assertSame('Subfolder/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Mycontroller::class, $controller);
Expand All @@ -194,7 +194,7 @@ public function testAutoRouteFindsControllerWithSubSubfolder()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder/sub/mycontroller/somemethod', 'get');
= $router->getRoute('subfolder/sub/mycontroller/somemethod', Method::GET);

$this->assertSame('Subfolder/Sub/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Sub\Mycontroller::class, $controller);
Expand All @@ -207,7 +207,7 @@ public function testAutoRouteFindsDashedSubfolder(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('dash-folder/mycontroller/somemethod', 'get');
= $router->getRoute('dash-folder/mycontroller/somemethod', Method::GET);

$this->assertSame('Dash_folder/', $directory);
$this->assertSame(
Expand All @@ -223,7 +223,7 @@ public function testAutoRouteFindsDashedController(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('dash-folder/dash-controller/somemethod', 'get');
= $router->getRoute('dash-folder/dash-controller/somemethod', Method::GET);

$this->assertSame('Dash_folder/', $directory);
$this->assertSame('\\' . Dash_controller::class, $controller);
Expand All @@ -236,7 +236,7 @@ public function testAutoRouteFindsDashedMethod(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('dash-folder/dash-controller/dash-method', 'get');
= $router->getRoute('dash-folder/dash-controller/dash-method', Method::GET);

$this->assertSame('Dash_folder/', $directory);
$this->assertSame('\\' . Dash_controller::class, $controller);
Expand All @@ -249,7 +249,7 @@ public function testAutoRouteFindsDefaultDashFolder(): void
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('dash-folder', 'get');
= $router->getRoute('dash-folder', Method::GET);

$this->assertSame('Dash_folder/', $directory);
$this->assertSame('\\' . Home::class, $controller);
Expand All @@ -262,7 +262,7 @@ public function testAutoRouteFallbackToDefaultMethod()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('index/15', 'get');
= $router->getRoute('index/15', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Index::class, $controller);
Expand All @@ -280,7 +280,7 @@ public function testAutoRouteFallbackToDefaultControllerOneParam()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder/15', 'get');
= $router->getRoute('subfolder/15', Method::GET);

$this->assertSame('Subfolder/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
Expand All @@ -298,7 +298,7 @@ public function testAutoRouteFallbackToDefaultControllerTwoParams()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder/15/20', 'get');
= $router->getRoute('subfolder/15/20', Method::GET);

$this->assertSame('Subfolder/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
Expand All @@ -316,7 +316,7 @@ public function testAutoRouteFallbackToDefaultControllerNoParams()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('subfolder', 'get');
= $router->getRoute('subfolder', Method::GET);

$this->assertSame('Subfolder/', $directory);
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Home::class, $controller);
Expand All @@ -335,7 +335,7 @@ public function testAutoRouteRejectsSingleDot(): void

$router = $this->createNewAutoRouter();

$router->getRoute('.', 'get');
$router->getRoute('.', Method::GET);
}

public function testAutoRouteRejectsDoubleDot(): void
Expand All @@ -344,7 +344,7 @@ public function testAutoRouteRejectsDoubleDot(): void

$router = $this->createNewAutoRouter();

$router->getRoute('..', 'get');
$router->getRoute('..', Method::GET);
}

public function testAutoRouteRejectsMidDot(): void
Expand All @@ -353,7 +353,7 @@ public function testAutoRouteRejectsMidDot(): void

$router = $this->createNewAutoRouter();

$router->getRoute('foo.bar', 'get');
$router->getRoute('foo.bar', Method::GET);
}

public function testRejectsDefaultControllerPath(): void
Expand All @@ -362,7 +362,7 @@ public function testRejectsDefaultControllerPath(): void

$router = $this->createNewAutoRouter();

$router->getRoute('home', 'get');
$router->getRoute('home', Method::GET);
}

public function testRejectsDefaultControllerAndDefaultMethodPath(): void
Expand All @@ -371,7 +371,7 @@ public function testRejectsDefaultControllerAndDefaultMethodPath(): void

$router = $this->createNewAutoRouter();

$router->getRoute('home/index', 'get');
$router->getRoute('home/index', Method::GET);
}

public function testRejectsDefaultMethodPath(): void
Expand All @@ -380,7 +380,7 @@ public function testRejectsDefaultMethodPath(): void

$router = $this->createNewAutoRouter();

$router->getRoute('mycontroller/index', 'get');
$router->getRoute('mycontroller/index', Method::GET);
}

public function testRejectsControllerWithRemapMethod(): void
Expand All @@ -392,7 +392,7 @@ public function testRejectsControllerWithRemapMethod(): void

$router = $this->createNewAutoRouter();

$router->getRoute('remap/test', 'get');
$router->getRoute('remap/test', Method::GET);
}

public function testRejectsURIWithUnderscoreFolder()
Expand All @@ -404,7 +404,7 @@ public function testRejectsURIWithUnderscoreFolder()

$router = $this->createNewAutoRouter();

$router->getRoute('dash_folder', 'get');
$router->getRoute('dash_folder', Method::GET);
}

public function testRejectsURIWithUnderscoreController()
Expand All @@ -416,7 +416,7 @@ public function testRejectsURIWithUnderscoreController()

$router = $this->createNewAutoRouter();

$router->getRoute('dash-folder/dash_controller/dash-method', 'get');
$router->getRoute('dash-folder/dash_controller/dash-method', Method::GET);
}

public function testRejectsURIWithUnderscoreMethod()
Expand All @@ -428,15 +428,15 @@ public function testRejectsURIWithUnderscoreMethod()

$router = $this->createNewAutoRouter();

$router->getRoute('dash-folder/dash-controller/dash_method', 'get');
$router->getRoute('dash-folder/dash-controller/dash_method', Method::GET);
}

public function testPermitsURIWithUnderscoreParam()
{
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('mycontroller/somemethod/a_b', 'get');
= $router->getRoute('mycontroller/somemethod/a_b', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Mycontroller::class, $controller);
Expand All @@ -449,7 +449,7 @@ public function testDoesNotTranslateDashInParam()
$router = $this->createNewAutoRouter();

[$directory, $controller, $method, $params]
= $router->getRoute('mycontroller/somemethod/a-b', 'get');
= $router->getRoute('mycontroller/somemethod/a-b', Method::GET);

$this->assertNull($directory);
$this->assertSame('\\' . Mycontroller::class, $controller);
Expand Down
Loading