Skip to content

Commit aaecc7e

Browse files
committed
[12.x] fix method dependencies order
1 parent 8737a65 commit aaecc7e

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/Illuminate/Routing/ResolvesRouteDependencies.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@ protected function resolveClassMethodDependencies(array $parameters, $instance,
4141
*/
4242
public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
4343
{
44-
$instanceCount = 0;
45-
44+
$keys = array_keys($parameters);
4645
$values = array_values($parameters);
4746

4847
$skippableValue = new stdClass;
4948

5049
foreach ($reflector->getParameters() as $key => $parameter) {
51-
$instance = $this->transformDependency($parameter, $parameters, $skippableValue);
52-
53-
if ($instance !== $skippableValue) {
54-
$instanceCount++;
5550

51+
if (false !== ($position = array_search($parameter->name, $keys, true))) {
52+
$instance = $parameters[$parameter->name];
53+
unset($keys[$position], $values[$position], $parameters[$parameter->name]);
54+
$this->spliceIntoParameters($parameters, $key, $instance, $parameter->name);
55+
} elseif ($skippableValue !== ($instance = $this->transformDependency($parameter, $values, $skippableValue))) {
5656
$this->spliceIntoParameters($parameters, $key, $instance);
57-
} elseif (! isset($values[$key - $instanceCount]) &&
58-
$parameter->isDefaultValueAvailable()) {
57+
} elseif (empty($values) && $parameter->isDefaultValueAvailable()) {
5958
$this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue());
59+
} else {
60+
array_shift($keys);
61+
array_shift($values);
6062
}
6163

6264
$this->container->fireAfterResolvingAttributeCallbacks($parameter->getAttributes(), $instance);
@@ -113,12 +115,17 @@ protected function alreadyInParameters($class, array $parameters)
113115
* @param array $parameters
114116
* @param string $offset
115117
* @param mixed $value
118+
* @param string|null $key
116119
* @return void
117120
*/
118-
protected function spliceIntoParameters(array &$parameters, $offset, $value)
121+
protected function spliceIntoParameters(array &$parameters, $offset, $value, ?string $key = null)
119122
{
120-
array_splice(
121-
$parameters, $offset, 0, [$value]
122-
);
123+
if (null === $key) {
124+
array_splice($parameters, $offset, 0, [$value]);
125+
} else {
126+
$parameters = array_slice($parameters, 0, $offset, true)
127+
+ [$key => $value]
128+
+ array_slice($parameters, $offset, null, true);
129+
}
123130
}
124131
}

tests/Routing/RoutingRouteTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,31 +684,31 @@ public function testControllerCallActionMethodParameters()
684684
unset($_SERVER['__test.controller_callAction_parameters']);
685685
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@oneArgument');
686686
$router->dispatch(Request::create($str.'/one/two', 'GET'));
687-
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
687+
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
688688

689689
// Has two arguments and receives two
690690
unset($_SERVER['__test.controller_callAction_parameters']);
691691
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@twoArguments');
692692
$router->dispatch(Request::create($str.'/one/two', 'GET'));
693-
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
693+
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
694694

695695
// Has two arguments but with different names from the ones passed from the route
696696
unset($_SERVER['__test.controller_callAction_parameters']);
697697
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@differentArgumentNames');
698698
$router->dispatch(Request::create($str.'/one/two', 'GET'));
699-
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
699+
$this->assertSame(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
700700

701701
// Has two arguments with same name but argument order is reversed
702702
unset($_SERVER['__test.controller_callAction_parameters']);
703703
$router->get(($str = Str::random()).'/{one}/{two}', RouteTestAnotherControllerWithParameterStub::class.'@reversedArguments');
704704
$router->dispatch(Request::create($str.'/one/two', 'GET'));
705-
$this->assertEquals(['one' => 'one', 'two' => 'two'], $_SERVER['__test.controller_callAction_parameters']);
705+
$this->assertSame(['two' => 'two', 'one' => 'one'], $_SERVER['__test.controller_callAction_parameters']);
706706

707707
// No route parameters while method has parameters
708708
unset($_SERVER['__test.controller_callAction_parameters']);
709709
$router->get(($str = Str::random()).'', RouteTestAnotherControllerWithParameterStub::class.'@oneArgument');
710710
$router->dispatch(Request::create($str, 'GET'));
711-
$this->assertEquals([], $_SERVER['__test.controller_callAction_parameters']);
711+
$this->assertSame([], $_SERVER['__test.controller_callAction_parameters']);
712712

713713
// With model bindings
714714
unset($_SERVER['__test.controller_callAction_parameters']);

0 commit comments

Comments
 (0)