5 * @link http://mnapoli.github.com/PHP-DI/
6 * @copyright Matthieu Napoli (http://mnapoli.fr/)
7 * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
10 namespace DI\Definition\Resolver;
12 use DI\Definition\DecoratorDefinition;
13 use DI\Definition\Exception\DefinitionException;
14 use DI\Definition\Definition;
15 use Interop\Container\ContainerInterface;
18 * Resolves a decorator definition to a value.
21 * @author Matthieu Napoli <matthieu@mnapoli.fr>
23 class DecoratorResolver implements DefinitionResolver
26 * @var ContainerInterface
31 * @var DefinitionResolver
33 private $definitionResolver;
36 * The resolver needs a container. This container will be passed to the factory as a parameter
37 * so that the factory can access other entries of the container.
39 * @param ContainerInterface $container
40 * @param DefinitionResolver $definitionResolver Used to resolve nested definitions.
42 public function __construct(ContainerInterface $container, DefinitionResolver $definitionResolver)
44 $this->container = $container;
45 $this->definitionResolver = $definitionResolver;
49 * Resolve a decorator definition to a value.
51 * This will call the callable of the definition and pass it the decorated entry.
53 * @param DecoratorDefinition $definition
57 public function resolve(Definition $definition, array $parameters = [])
59 $callable = $definition->getCallable();
61 if (! is_callable($callable)) {
62 throw new DefinitionException(sprintf(
63 'The decorator "%s" is not callable',
64 $definition->getName()
68 $decoratedDefinition = $definition->getDecoratedDefinition();
70 if (! $decoratedDefinition instanceof Definition) {
71 if (! $definition->getSubDefinitionName()) {
72 throw new DefinitionException('Decorators cannot be nested in another definition');
75 throw new DefinitionException(sprintf(
76 'Entry "%s" decorates nothing: no previous definition with the same name was found',
77 $definition->getName()
81 $decorated = $this->definitionResolver->resolve($decoratedDefinition);
83 return call_user_func($callable, $decorated, $this->container);
89 public function isResolvable(Definition $definition, array $parameters = [])