Skip to content

Commit

Permalink
Refactor injection implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Aug 29, 2018
1 parent 6e44615 commit 0a59dee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/Resolver/AbstractInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function trigger_error;

/**
* @codeCoverageIgnore Deprecated
* @deprecated Since 3.1.0
* @see InjectionInterface
*/
Expand Down
12 changes: 4 additions & 8 deletions src/Resolver/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ private function isCallableType(string $type): bool
* If the candidate is usable, its injection representation is returned
*
* @param mixed $value
* @param string|null $requiredType
* @return null|TypeInjection|ValueInjection
* @param null|string $requiredType
* @return null|InjectionInterface
*/
private function prepareInjection($value, ?string $requiredType) : ?AbstractInjection
private function prepareInjection($value, ?string $requiredType) : ?InjectionInterface
{
if (($value instanceof ValueInjection) || ($value instanceof TypeInjection)) {
return $value;
Expand Down Expand Up @@ -266,7 +266,7 @@ private function prepareInjection($value, ?string $requiredType) : ?AbstractInje
* @param array $callTimeParameters
* @throws Exception\UnexpectedValueException
* @throws Exception\MissingPropertyException
* @return AbstractInjection[]
* @return InjectionInterface[]
*/
public function resolveParameters(string $requestedType, array $callTimeParameters = []) : array
{
Expand Down Expand Up @@ -337,10 +337,6 @@ public function resolveParameters(string $requestedType, array $callTimeParamete
$result[$name] = new ValueInjection($paramInfo->getDefault());
}

foreach ($result as $name => $injection) {
$injection->setParameterName($name);
}

return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resolver/DependencyResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function resolvePreference(string $type, ?string $context = null) : ?stri
*
* @param string $class The class name to resolve the parameters for
* @param array $parameters Parameters to use as provided.
* @return array Returns the injection parameters as positional array. This
* @return InjectionInterface[] Returns the injection parameters as indexed array. This
* array contains either TypeInjection or ValueInjection instances
* @throws \Zend\Di\Exception\MissingPropertyException When a parameter
* could not be resolved
Expand Down
48 changes: 27 additions & 21 deletions src/Resolver/TypeInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

namespace Zend\Di\Resolver;

use const E_USER_DEPRECATED;
use Psr\Container\ContainerInterface;
use function trigger_error;

/**
* Wrapper for types that should be looked up for injection
*/
final class TypeInjection extends AbstractInjection
final class TypeInjection implements InjectionInterface
{
/**
* Holds the type name to look up
Expand All @@ -29,42 +33,44 @@ public function __construct(string $type)
$this->type = $type;
}

/**
* Get the type name to look up for injection
*
* @return string
*/
public function getType() : string
{
return $this->type;
}

/**
* {@inheritDoc}
* @see AbstractInjection::export()
*/
public function export() : string
{
return var_export($this->type, true);
}

/**
* {@inheritDoc}
* @see AbstractInjection::isExportable()
*/
public function isExportable() : bool
{
return true;
}

public function toValue(ContainerInterface $container)
{
return $container->get($this->type);
}

/**
* Simply converts to the type name string
* Reflects the type name
*
* @codeCoverageIgnore Too trivial to require a test
* @return string
*/
public function __toString() : string
{
return $this->type;
}

/**
* Get the type name to look up for injection
*
* @codeCoverageIgnore
* @deprecated Since 3.1.0
* @see toValue()
* @see export()
* @see __toString()
* @return string
*/
public function getType() : string
{
trigger_error(__METHOD__ . ' is deprecated. Please migrate to __toString()', E_USER_DEPRECATED);
return $this->type;
}
}
33 changes: 20 additions & 13 deletions src/Resolver/ValueInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

namespace Zend\Di\Resolver;

use Psr\Container\ContainerInterface;
use ReflectionObject;
use Zend\Di\Exception\RuntimeException;

/**
* Wrapper for values that should be directly injected
*/
class ValueInjection extends AbstractInjection
class ValueInjection implements InjectionInterface
{
/**
* Holds the value to inject
Expand All @@ -31,23 +32,13 @@ public function __construct($value)
}

/**
* @param string $state
* @param array $state
*/
public static function __set_state($state) : self
public static function __set_state(array $state) : self
{
return new self($state['value']);
}

/**
* Get the value to inject
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}

/**
* Exports the encapsulated value to php code
*
Expand Down Expand Up @@ -83,4 +74,20 @@ public function isExportable() : bool

return false;
}

public function toValue(ContainerInterface $container)
{
return $this->value;
}

/**
* Get the value to inject
*
* @deprecated Since 3.1.0
* @see toValue()
*/
public function getValue()
{
return $this->value;
}
}

0 comments on commit 0a59dee

Please sign in to comment.