Skip to content

Commit 2e79e6b

Browse files
committed
Merge branch 'develop' into testing/commands
2 parents da3b9a0 + e336dfc commit 2e79e6b

File tree

3 files changed

+50
-24
lines changed

3 files changed

+50
-24
lines changed

system/CLI/CommandRunner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ protected function createCommandList()
150150
}
151151

152152
$class = new \ReflectionClass($className);
153+
153154
if ( ! $class->isInstantiable() || ! $class->isSubclassOf(BaseCommand::class))
154155
{
155156
continue;

system/Router/RouteCollection.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php namespace CodeIgniter\Router;
1+
<?php
2+
namespace CodeIgniter\Router;
23

34
/**
45
* CodeIgniter
@@ -132,16 +133,16 @@ class RouteCollection implements RouteCollectionInterface
132133
* @var array
133134
*/
134135
protected $routes = [
135-
'*' => [],
136-
'options' => [],
137-
'get' => [],
138-
'head' => [],
139-
'post' => [],
140-
'put' => [],
141-
'delete' => [],
142-
'trace' => [],
143-
'connect' => [],
144-
'cli' => [],
136+
'*' => [],
137+
'options' => [],
138+
'get' => [],
139+
'head' => [],
140+
'post' => [],
141+
'put' => [],
142+
'delete' => [],
143+
'trace' => [],
144+
'connect' => [],
145+
'cli' => [],
145146
];
146147

147148
/**
@@ -383,7 +384,8 @@ public function get404Override()
383384
*/
384385
protected function discoverRoutes()
385386
{
386-
if ($this->didDiscover) return;
387+
if ($this->didDiscover)
388+
return;
387389

388390
// We need this var in local scope
389391
// so route files can access it.
@@ -520,8 +522,11 @@ public function getRoutes($verb = null): array
520522
{
521523
// Keep current verb's routes at the beginning so they're matched
522524
// before any of the generic, "add" routes.
523-
$collection = array_merge($this->routes[$verb], $this->routes['*']);
524-
525+
if (isset($this->routes['*']))
526+
{
527+
$extraRules = array_diff_key($this->routes['*'], $this->routes[$verb]);
528+
$collection = array_merge($this->routes[$verb], $extraRules);
529+
}
525530
foreach ($collection as $r)
526531
{
527532
$key = key($r['route']);
@@ -806,13 +811,13 @@ public function resource(string $name, array $options = null): RouteCollectionIn
806811

807812
$methods = isset($options['only']) ? is_string($options['only']) ? explode(',', $options['only']) : $options['only'] : ['index', 'show', 'create', 'update', 'delete', 'new', 'edit'];
808813

809-
if(isset($options['except']))
814+
if (isset($options['except']))
810815
{
811816
$options['except'] = is_array($options['except']) ? $options['except'] : explode(',', $options['except']);
812817
$c = count($methods);
813-
for($i = 0; $i < $c; $i++)
818+
for ($i = 0; $i < $c; $i ++)
814819
{
815-
if(in_array($methods[$i], $options['except']))
820+
if (in_array($methods[$i], $options['except']))
816821
{
817822
unset($methods[$i]);
818823
}
@@ -822,16 +827,16 @@ public function resource(string $name, array $options = null): RouteCollectionIn
822827
if (in_array('index', $methods))
823828
$this->get($name, $new_name . '::index', $options);
824829
if (in_array('new', $methods))
825-
$this->get($name. '/new', $new_name . '::new', $options);
830+
$this->get($name . '/new', $new_name . '::new', $options);
826831
if (in_array('edit', $methods))
827-
$this->get($name . '/' . $id. '/edit', $new_name . '::edit/$1', $options);
832+
$this->get($name . '/' . $id . '/edit', $new_name . '::edit/$1', $options);
828833
if (in_array('show', $methods))
829834
$this->get($name . '/' . $id, $new_name . '::show/$1', $options);
830835
if (in_array('create', $methods))
831836
$this->post($name, $new_name . '::create', $options);
832837
if (in_array('update', $methods))
833838
$this->put($name . '/' . $id, $new_name . '::update/$1', $options);
834-
$this->patch($name . '/' . $id, $new_name . '::update/$1', $options);
839+
$this->patch($name . '/' . $id, $new_name . '::update/$1', $options);
835840
if (in_array('delete', $methods))
836841
$this->delete($name . '/' . $id, $new_name . '::delete/$1', $options);
837842

@@ -1137,7 +1142,7 @@ public function isFiltered(string $search): bool
11371142
*/
11381143
public function getFilterForRoute(string $search): string
11391144
{
1140-
if (! $this->isFiltered($search))
1145+
if ( ! $this->isFiltered($search))
11411146
{
11421147
return '';
11431148
}
@@ -1211,10 +1216,10 @@ protected function create(string $verb, string $from, $to, array $options = null
12111216
$from = trim($from, '/');
12121217
}
12131218

1214-
$options = array_merge((array)$this->currentOptions, (array)$options);
1219+
$options = array_merge((array) $this->currentOptions, (array) $options);
12151220

12161221
// Hostname limiting?
1217-
if (! empty($options['hostname']))
1222+
if ( ! empty($options['hostname']))
12181223
{
12191224
// @todo determine if there's a way to whitelist hosts?
12201225
if (strtolower($_SERVER['HTTP_HOST']) != strtolower($options['hostname']))
@@ -1245,7 +1250,8 @@ protected function create(string $verb, string $from, $to, array $options = null
12451250
for ($i = (int) $options['offset'] + 1; $i < (int) $options['offset'] + 7; $i ++ )
12461251
{
12471252
$to = preg_replace_callback(
1248-
'/\$X/', function ($m) use ($i) {
1253+
'/\$X/', function ($m) use ($i)
1254+
{
12491255
return '$' . $i;
12501256
}, $to, 1
12511257
);

tests/system/Router/RouterTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,23 @@ public function testMatchesCorrectlyWithMixedVerbs()
301301
$this->assertEquals('\Pages', $router->controllerName());
302302
$this->assertEquals('view', $router->methodName());
303303
}
304+
//--------------------------------------------------------------------
305+
306+
/**
307+
* @see https://github.com/bcit-ci/CodeIgniter4/issues/1354
308+
*/
309+
public function testRouteOrder()
310+
{
311+
$this->collection->setHTTPVerb('post');
312+
313+
$this->collection->post('auth', 'Main::auth_post');
314+
$this->collection->add('auth', 'Main::index');
315+
316+
$router = new Router($this->collection);
317+
318+
$router->handle('auth');
319+
$this->assertEquals('\Main', $router->controllerName());
320+
$this->assertEquals('auth_post', $router->methodName());
321+
322+
}
304323
}

0 commit comments

Comments
 (0)