Skip to content

Commit

Permalink
make ->call also inject dependencies for calls with the @ sign based …
Browse files Browse the repository at this point in the history
…classnames
  • Loading branch information
mvalim committed Sep 30, 2014
1 parent b78b1ea commit 97c1880
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,25 @@ public function call($callback, array $parameters = array(), $defaultMethod = nu
return $this->callClass($callback, $parameters, $defaultMethod);
}

$dependencies = [];
$dependencies = $this->getMethodDependencies($callback,$parameters);

return call_user_func_array($callback, $dependencies);
}

/**
* Get all dependencies for a given method
* @param \Closure|array $callback
* @param array $parameters
* @return array
*/
protected function getMethodDependencies($callback,$parameters = array()) {
$dependencies = [];
foreach ($this->getCallReflector($callback)->getParameters() as $key => $parameter)
{
$dependencies[] = $this->getDependencyForCallParameter($parameter, $parameters);
}

return call_user_func_array($callback, $dependencies);
return array_merge($dependencies,$parameters);
}

/**
Expand Down Expand Up @@ -601,7 +612,9 @@ protected function callClass($target, array $parameters = array(), $defaultMetho
// received in this method into this listener class instance's methods.
$callable = array($this->make($segments[0]), $method);

return call_user_func_array($callable, $parameters);
$dependencies = $this->getMethodDependencies($callable,$parameters);

return call_user_func_array($callable, $dependencies);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ public function testCallWithAtSignBasedClassReferences()
$result = $container->call('ContainerTestCallStub@work', ['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $result);

$container = new Container;
$result = $container->call('ContainerTestCallStub@inject');
$this->assertInstanceOf('ContainerConcreteStub',$result[0]);
$this->assertEquals('taylor',$result[1]);

$container = new Container;
$result = $container->call('ContainerTestCallStub@inject',['default'=>'foo']);
$this->assertInstanceOf('ContainerConcreteStub',$result[0]);
$this->assertEquals('foo',$result[1]);

$container = new Container;
$result = $container->call('ContainerTestCallStub', ['foo', 'bar'], 'work');
$this->assertEquals(['foo', 'bar'], $result);
Expand Down Expand Up @@ -500,6 +510,9 @@ class ContainerTestCallStub {
public function work() {
return func_get_args();
}
public function inject(ContainerConcreteStub $stub,$default = 'taylor') {
return func_get_args();
}
}

class ContainerTestContextInjectOne {
Expand Down

0 comments on commit 97c1880

Please sign in to comment.