diff --git a/src/Illuminate/Routing/RouteDependencyResolverTrait.php b/src/Illuminate/Routing/RouteDependencyResolverTrait.php index 37fd8b579e03..eb49fbb77eaa 100644 --- a/src/Illuminate/Routing/RouteDependencyResolverTrait.php +++ b/src/Illuminate/Routing/RouteDependencyResolverTrait.php @@ -31,21 +31,30 @@ protected function resolveClassMethodDependencies(array $parameters, $instance, /** * Resolve the given method's type-hinted dependencies. * - * @param array $parameters + * @param array $originalParameters * @param \ReflectionFunctionAbstract $reflector * @return array */ - public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector) + public function resolveMethodDependencies(array $originalParameters, ReflectionFunctionAbstract $reflector) { - $originalParameters = $parameters; + $parameters = []; + + $values = array_values($originalParameters); + + $instancesCount = 0; foreach ($reflector->getParameters() as $key => $parameter) { $instance = $this->transformDependency( - $parameter, $parameters, $originalParameters + $parameter, $originalParameters ); if (! is_null($instance)) { - $this->spliceIntoParameters($parameters, $key, $instance); + $instancesCount++; + + $parameters[] = $instance; + } else { + $parameters[] = isset($values[$key - $instancesCount]) + ? $values[$key - $instancesCount] : $parameter->getDefaultValue(); } } @@ -57,10 +66,9 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA * * @param \ReflectionParameter $parameter * @param array $parameters - * @param array $originalParameters * @return mixed */ - protected function transformDependency(ReflectionParameter $parameter, $parameters, $originalParameters) + protected function transformDependency(ReflectionParameter $parameter, $parameters) { $class = $parameter->getClass(); @@ -85,19 +93,4 @@ protected function alreadyInParameters($class, array $parameters) return $value instanceof $class; })); } - - /** - * Splice the given value into the parameter list. - * - * @param array $parameters - * @param string $key - * @param mixed $instance - * @return void - */ - protected function spliceIntoParameters(array &$parameters, $key, $instance) - { - array_splice( - $parameters, $key, 0, [$instance] - ); - } } diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 80c5db8a9489..dfc948b389e9 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -309,6 +309,24 @@ public function testClassesCanBeInjectedIntoRoutes() unset($_SERVER['__test.route_inject']); } + public function testClassesAndVariablesCanBeInjectedIntoRoutes() + { + unset($_SERVER['__test.route_inject']); + $router = $this->getRouter(); + $router->get('foo/{var}/{bar?}/{baz?}', function (stdClass $foo, $var, $bar = 'test', Request $baz = null) { + $_SERVER['__test.route_inject'] = func_get_args(); + + return 'hello'; + }); + $this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent()); + $this->assertInstanceOf('stdClass', $_SERVER['__test.route_inject'][0]); + $this->assertEquals('bar', $_SERVER['__test.route_inject'][1]); + $this->assertEquals('test', $_SERVER['__test.route_inject'][2]); + $this->assertArrayHasKey(3, $_SERVER['__test.route_inject']); + $this->assertInstanceOf('Illuminate\Http\Request', $_SERVER['__test.route_inject'][3]); + unset($_SERVER['__test.route_inject']); + } + public function testOptionsResponsesAreGeneratedByDefault() { $router = $this->getRouter();