Skip to content

Commit eac3eda

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.4
Conflicts: rector.php system/Router/AutoRouterImproved.php
2 parents 4563aae + c87169d commit eac3eda

File tree

10 files changed

+127
-37
lines changed

10 files changed

+127
-37
lines changed

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
RemoveUnusedConstructorParamRector::class => [
9393
// there are deprecated parameters
9494
__DIR__ . '/system/Debug/Exceptions.php',
95+
// @TODO remove if deprecated $httpVerb is removed
96+
__DIR__ . '/system/Router/AutoRouterImproved.php',
9597
],
9698

9799
// call on purpose for nothing happen check

system/Router/AutoRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(
8080
*
8181
* @return array [directory_name, controller_name, controller_method, params]
8282
*/
83-
public function getRoute(string $uri): array
83+
public function getRoute(string $uri, string $httpVerb): array
8484
{
8585
$segments = explode('/', $uri);
8686

system/Router/AutoRouterImproved.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ final class AutoRouterImproved implements AutoRouterInterface
5555
*/
5656
private bool $translateURIDashes;
5757

58-
/**
59-
* HTTP verb for the request.
60-
*/
61-
private string $httpVerb;
62-
6358
/**
6459
* The namespace for controllers.
6560
*/
@@ -71,15 +66,17 @@ final class AutoRouterImproved implements AutoRouterInterface
7166
private string $defaultController;
7267

7368
/**
74-
* The name of the default method
69+
* The name of the default method without HTTP verb prefix.
7570
*/
7671
private string $defaultMethod;
7772

7873
/**
7974
* @param class-string[] $protectedControllers
8075
* @param string $defaultController Short classname
76+
*
77+
* @deprecated $httpVerb is deprecated. No longer used.
8178
*/
82-
public function __construct(
79+
public function __construct(// @phpstan-ignore-line
8380
array $protectedControllers,
8481
string $namespace,
8582
string $defaultController,
@@ -90,13 +87,11 @@ public function __construct(
9087
$this->protectedControllers = $protectedControllers;
9188
$this->namespace = rtrim($namespace, '\\');
9289
$this->translateURIDashes = $translateURIDashes;
93-
$this->httpVerb = $httpVerb;
9490
$this->defaultController = $defaultController;
95-
$this->defaultMethod = $httpVerb . ucfirst($defaultMethod);
91+
$this->defaultMethod = $defaultMethod;
9692

9793
// Set the default values
9894
$this->controller = $this->defaultController;
99-
$this->method = $this->defaultMethod;
10095
}
10196

10297
private function createSegments(string $uri)
@@ -195,8 +190,11 @@ private function searchLastDefaultController(array $segments): bool
195190
*
196191
* @return array [directory_name, controller_name, controller_method, params]
197192
*/
198-
public function getRoute(string $uri): array
193+
public function getRoute(string $uri, string $httpVerb): array
199194
{
195+
$defaultMethod = strtolower($httpVerb) . ucfirst($this->defaultMethod);
196+
$this->method = $defaultMethod;
197+
200198
$segments = $this->createSegments($uri);
201199

202200
// Check for Module Routes.
@@ -235,7 +233,7 @@ public function getRoute(string $uri): array
235233

236234
$method = '';
237235
if ($methodParam !== null) {
238-
$method = $this->httpVerb . ucfirst($this->translateURIDashes($methodParam));
236+
$method = $httpVerb . ucfirst($this->translateURIDashes($methodParam));
239237
}
240238

241239
if ($methodParam !== null && method_exists($this->controller, $method)) {
@@ -251,7 +249,7 @@ public function getRoute(string $uri): array
251249
}
252250

253251
// Prevent access to default method path
254-
if (strtolower($this->method) === strtolower($this->defaultMethod)) {
252+
if (strtolower($this->method) === strtolower($defaultMethod)) {
255253
throw new PageNotFoundException(
256254
'Cannot access the default method "' . $this->method . '" with the method name URI path.'
257255
);

system/Router/AutoRouterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface AutoRouterInterface
2121
*
2222
* @return array [directory_name, controller_name, controller_method, params]
2323
*/
24-
public function getRoute(string $uri): array;
24+
public function getRoute(string $uri, string $httpVerb): array;
2525
}

system/Router/RouteCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class RouteCollection implements RouteCollectionInterface
156156
/**
157157
* The current method that the script is being called by.
158158
*
159-
* @var string
159+
* @var string HTTP verb (lower case) like `get`,`post` or `*`
160160
*/
161161
protected $HTTPVerb = '*';
162162

@@ -584,11 +584,13 @@ public function getHTTPVerb(): string
584584
* Sets the current HTTP verb.
585585
* Used primarily for testing.
586586
*
587+
* @param string $verb HTTP verb
588+
*
587589
* @return $this
588590
*/
589591
public function setHTTPVerb(string $verb)
590592
{
591-
$this->HTTPVerb = $verb;
593+
$this->HTTPVerb = strtolower($verb);
592594

593595
return $this;
594596
}

system/Router/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function __construct(RouteCollectionInterface $routes, ?Request $request
126126
$this->controller = $this->collection->getDefaultController();
127127
$this->method = $this->collection->getDefaultMethod();
128128

129-
$this->collection->setHTTPVerb(strtolower($request->getMethod() ?? $_SERVER['REQUEST_METHOD']));
129+
$this->collection->setHTTPVerb($request->getMethod() ?? $_SERVER['REQUEST_METHOD']);
130130

131131
$this->translateURIDashes = $this->collection->shouldTranslateURIDashes();
132132

@@ -504,7 +504,7 @@ protected function checkRoutes(string $uri): bool
504504
public function autoRoute(string $uri)
505505
{
506506
[$this->directory, $this->controller, $this->method, $this->params]
507-
= $this->autoRouter->getRoute($uri);
507+
= $this->autoRouter->getRoute($uri, $this->collection->getHTTPVerb());
508508
}
509509

510510
/**

tests/system/Router/AutoRouterImprovedTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodGet()
5959
$router = $this->createNewAutoRouter();
6060

6161
[$directory, $controller, $method, $params]
62-
= $router->getRoute('/');
62+
= $router->getRoute('/', 'get');
6363

6464
$this->assertNull($directory);
6565
$this->assertSame('\\' . Index::class, $controller);
@@ -95,7 +95,7 @@ public function testAutoRouteFindsDefaultControllerAndMethodPost()
9595
$router = $this->createNewAutoRouter('post');
9696

9797
[$directory, $controller, $method, $params]
98-
= $router->getRoute('/');
98+
= $router->getRoute('/', 'post');
9999

100100
$this->assertNull($directory);
101101
$this->assertSame('\\' . Index::class, $controller);
@@ -108,7 +108,7 @@ public function testAutoRouteFindsControllerWithFileAndMethod()
108108
$router = $this->createNewAutoRouter();
109109

110110
[$directory, $controller, $method, $params]
111-
= $router->getRoute('mycontroller/somemethod');
111+
= $router->getRoute('mycontroller/somemethod', 'get');
112112

113113
$this->assertNull($directory);
114114
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -121,7 +121,7 @@ public function testFindsControllerAndMethodAndParam()
121121
$router = $this->createNewAutoRouter();
122122

123123
[$directory, $controller, $method, $params]
124-
= $router->getRoute('mycontroller/somemethod/a');
124+
= $router->getRoute('mycontroller/somemethod/a', 'get');
125125

126126
$this->assertNull($directory);
127127
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -138,15 +138,15 @@ public function testUriParamCountIsGreaterThanMethodParams()
138138

139139
$router = $this->createNewAutoRouter();
140140

141-
$router->getRoute('mycontroller/somemethod/a/b');
141+
$router->getRoute('mycontroller/somemethod/a/b', 'get');
142142
}
143143

144144
public function testAutoRouteFindsControllerWithFile()
145145
{
146146
$router = $this->createNewAutoRouter();
147147

148148
[$directory, $controller, $method, $params]
149-
= $router->getRoute('mycontroller');
149+
= $router->getRoute('mycontroller', 'get');
150150

151151
$this->assertNull($directory);
152152
$this->assertSame('\\' . Mycontroller::class, $controller);
@@ -159,7 +159,7 @@ public function testAutoRouteFindsControllerWithSubfolder()
159159
$router = $this->createNewAutoRouter();
160160

161161
[$directory, $controller, $method, $params]
162-
= $router->getRoute('subfolder/mycontroller/somemethod');
162+
= $router->getRoute('subfolder/mycontroller/somemethod', 'get');
163163

164164
$this->assertSame('Subfolder/', $directory);
165165
$this->assertSame('\\' . \CodeIgniter\Router\Controllers\Subfolder\Mycontroller::class, $controller);
@@ -185,7 +185,7 @@ public function testAutoRouteFindsDashedSubfolder()
185185
$router = $this->createNewAutoRouter();
186186

187187
[$directory, $controller, $method, $params]
188-
= $router->getRoute('dash-folder/mycontroller/somemethod');
188+
= $router->getRoute('dash-folder/mycontroller/somemethod', 'get');
189189

190190
$this->assertSame('Dash_folder/', $directory);
191191
$this->assertSame(
@@ -201,7 +201,7 @@ public function testAutoRouteFindsDashedController()
201201
$router = $this->createNewAutoRouter();
202202

203203
[$directory, $controller, $method, $params]
204-
= $router->getRoute('dash-folder/dash-controller/somemethod');
204+
= $router->getRoute('dash-folder/dash-controller/somemethod', 'get');
205205

206206
$this->assertSame('Dash_folder/', $directory);
207207
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -214,7 +214,7 @@ public function testAutoRouteFindsDashedMethod()
214214
$router = $this->createNewAutoRouter();
215215

216216
[$directory, $controller, $method, $params]
217-
= $router->getRoute('dash-folder/dash-controller/dash-method');
217+
= $router->getRoute('dash-folder/dash-controller/dash-method', 'get');
218218

219219
$this->assertSame('Dash_folder/', $directory);
220220
$this->assertSame('\\' . Dash_controller::class, $controller);
@@ -227,7 +227,7 @@ public function testAutoRouteFindsDefaultDashFolder()
227227
$router = $this->createNewAutoRouter();
228228

229229
[$directory, $controller, $method, $params]
230-
= $router->getRoute('dash-folder');
230+
= $router->getRoute('dash-folder', 'get');
231231

232232
$this->assertSame('Dash_folder/', $directory);
233233
$this->assertSame('\\' . Home::class, $controller);
@@ -293,7 +293,7 @@ public function testAutoRouteRejectsSingleDot()
293293

294294
$router = $this->createNewAutoRouter();
295295

296-
$router->getRoute('.');
296+
$router->getRoute('.', 'get');
297297
}
298298

299299
public function testAutoRouteRejectsDoubleDot()
@@ -302,7 +302,7 @@ public function testAutoRouteRejectsDoubleDot()
302302

303303
$router = $this->createNewAutoRouter();
304304

305-
$router->getRoute('..');
305+
$router->getRoute('..', 'get');
306306
}
307307

308308
public function testAutoRouteRejectsMidDot()
@@ -311,7 +311,7 @@ public function testAutoRouteRejectsMidDot()
311311

312312
$router = $this->createNewAutoRouter();
313313

314-
$router->getRoute('foo.bar');
314+
$router->getRoute('foo.bar', 'get');
315315
}
316316

317317
public function testRejectsDefaultControllerPath()
@@ -320,7 +320,7 @@ public function testRejectsDefaultControllerPath()
320320

321321
$router = $this->createNewAutoRouter();
322322

323-
$router->getRoute('home');
323+
$router->getRoute('home', 'get');
324324
}
325325

326326
public function testRejectsDefaultControllerAndDefaultMethodPath()
@@ -329,7 +329,7 @@ public function testRejectsDefaultControllerAndDefaultMethodPath()
329329

330330
$router = $this->createNewAutoRouter();
331331

332-
$router->getRoute('home/index');
332+
$router->getRoute('home/index', 'get');
333333
}
334334

335335
public function testRejectsDefaultMethodPath()
@@ -338,7 +338,7 @@ public function testRejectsDefaultMethodPath()
338338

339339
$router = $this->createNewAutoRouter();
340340

341-
$router->getRoute('mycontroller/index');
341+
$router->getRoute('mycontroller/index', 'get');
342342
}
343343

344344
public function testRejectsControllerWithRemapMethod()
@@ -350,6 +350,6 @@ public function testRejectsControllerWithRemapMethod()
350350

351351
$router = $this->createNewAutoRouter();
352352

353-
$router->getRoute('remap/test');
353+
$router->getRoute('remap/test', 'get');
354354
}
355355
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Test;
13+
14+
use CodeIgniter\Events\Events;
15+
use Config\Feature;
16+
use Config\Services;
17+
18+
/**
19+
* @group Others
20+
*
21+
* @internal
22+
*/
23+
final class FeatureTestAutoRoutingImprovedTest extends CIUnitTestCase
24+
{
25+
use FeatureTestTrait;
26+
27+
public static function setUpBeforeClass(): void
28+
{
29+
parent::setUpBeforeClass();
30+
31+
Events::simulate(true);
32+
33+
self::initializeRouter();
34+
}
35+
36+
public static function tearDownAfterClass(): void
37+
{
38+
parent::tearDownAfterClass();
39+
40+
Events::simulate(false);
41+
42+
Services::reset();
43+
}
44+
45+
private static function initializeRouter(): void
46+
{
47+
$routes = Services::routes();
48+
$routes->resetRoutes();
49+
$routes->loadRoutes();
50+
51+
$routes->setAutoRoute(true);
52+
config(Feature::class)->autoRoutesImproved = true;
53+
54+
$namespace = 'Tests\Support\Controllers';
55+
$routes->setDefaultNamespace($namespace);
56+
57+
$router = Services::router($routes);
58+
59+
Services::injectMock('router', $router);
60+
}
61+
62+
public function testCallGet()
63+
{
64+
$response = $this->get('newautorouting');
65+
66+
$response->assertSee('Hello');
67+
}
68+
69+
public function testCallPost()
70+
{
71+
$response = $this->post('newautorouting/save/1/a/b');
72+
73+
$response->assertSee('Saved');
74+
}
75+
}

0 commit comments

Comments
 (0)