-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new resolver ParameterNameContainerResolver
Resolves parameters from a container by name
- Loading branch information
Showing
4 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/ParameterResolver/Container/ParameterNameContainerResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Invoker\ParameterResolver\Container; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Invoker\ParameterResolver\ParameterResolver; | ||
use ReflectionFunctionAbstract; | ||
|
||
/** | ||
* Inject entries from a DI container using the parameter names. | ||
* | ||
* @author Matthieu Napoli <[email protected]> | ||
*/ | ||
class ParameterNameContainerResolver implements ParameterResolver | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @param ContainerInterface $container The container to get entries from. | ||
*/ | ||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function getParameters( | ||
ReflectionFunctionAbstract $reflection, | ||
array $providedParameters, | ||
array $resolvedParameters | ||
) { | ||
foreach ($reflection->getParameters() as $index => $parameter) { | ||
if (array_key_exists($index, $resolvedParameters)) { | ||
// Skip already resolved parameters | ||
continue; | ||
} | ||
|
||
$name = $parameter->getName(); | ||
|
||
if ($name && $this->container->has($name)) { | ||
$resolvedParameters[$index] = $this->container->get($name); | ||
} | ||
} | ||
|
||
return $resolvedParameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/ParameterResolver/Container/ParameterNameContainerResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Invoker\Test\ParameterResolver\Container; | ||
|
||
use Invoker\ParameterResolver\Container\ParameterNameContainerResolver; | ||
use Invoker\Test\Mock\ArrayContainer; | ||
|
||
class ParameterNameContainerResolverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var ParameterNameContainerResolver | ||
*/ | ||
private $resolver; | ||
|
||
/** | ||
* @var ArrayContainer | ||
*/ | ||
private $container; | ||
|
||
public function setUp() | ||
{ | ||
$this->container = new ArrayContainer; | ||
$this->resolver = new ParameterNameContainerResolver($this->container); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function should_resolve_parameter_with_parameter_name_from_container() | ||
{ | ||
$callable = function ($foo) {}; | ||
$reflection = new \ReflectionFunction($callable); | ||
|
||
$this->container->set('foo', 'bar'); | ||
|
||
$parameters = $this->resolver->getParameters($reflection, array(), array()); | ||
|
||
$this->assertCount(1, $parameters); | ||
$this->assertSame('bar', $parameters[0]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function should_skip_parameter_if_container_cannot_provide_parameter() | ||
{ | ||
$callable = function ($foo) {}; | ||
$reflection = new \ReflectionFunction($callable); | ||
|
||
$parameters = $this->resolver->getParameters($reflection, array(), array()); | ||
|
||
$this->assertCount(0, $parameters); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function should_skip_parameter_if_already_resolved() | ||
{ | ||
$callable = function ($foo) {}; | ||
$reflection = new \ReflectionFunction($callable); | ||
|
||
$this->container->set('foo', 'bar'); | ||
|
||
$resolvedParameters = array('first param value'); | ||
$parameters = $this->resolver->getParameters($reflection, array(), $resolvedParameters); | ||
|
||
$this->assertSame($resolvedParameters, $parameters); | ||
} | ||
} |