| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           4 | 
              | 
        
        
            | 
            | 
           5 | 
           namespace DI\Definition\Resolver;
  | 
        
        
            | 
            | 
           6 | 
              | 
        
        
            | 
            | 
           7 | 
           use DI\Definition\DecoratorDefinition;
  | 
        
        
            | 
            | 
           8 | 
           use DI\Definition\Definition;
  | 
        
        
            | 
            | 
           9 | 
           use DI\Definition\Exception\InvalidDefinition;
  | 
        
        
            | 
            | 
           10 | 
           use Psr\Container\ContainerInterface;
  | 
        
        
            | 
            | 
           11 | 
              | 
        
        
            | 
            | 
           12 | 
           /**
  | 
        
        
            | 
            | 
           13 | 
            * Resolves a decorator definition to a value.
  | 
        
        
            | 
            | 
           14 | 
            *
  | 
        
        
            | 
            | 
           15 | 
            * @template-implements DefinitionResolver<DecoratorDefinition>
  | 
        
        
            | 
            | 
           16 | 
            *
  | 
        
        
            | 
            | 
           17 | 
            * @since 5.0
  | 
        
        
            | 
            | 
           18 | 
            * @author Matthieu Napoli <matthieu@mnapoli.fr>
  | 
        
        
            | 
            | 
           19 | 
            */
  | 
        
        
            | 
            | 
           20 | 
           class DecoratorResolver implements DefinitionResolver
  | 
        
        
            | 
            | 
           21 | 
           {
  | 
        
        
            | 
            | 
           22 | 
               /**
  | 
        
        
            | 
            | 
           23 | 
                * The resolver needs a container. This container will be passed to the factory as a parameter
  | 
        
        
            | 
            | 
           24 | 
                * so that the factory can access other entries of the container.
  | 
        
        
            | 
            | 
           25 | 
                *
  | 
        
        
            | 
            | 
           26 | 
                * @param DefinitionResolver $definitionResolver Used to resolve nested definitions.
  | 
        
        
            | 
            | 
           27 | 
                */
  | 
        
        
            | 
            | 
           28 | 
               public function __construct(
  | 
        
        
            | 
            | 
           29 | 
                   private ContainerInterface $container,
  | 
        
        
            | 
            | 
           30 | 
                   private DefinitionResolver $definitionResolver
  | 
        
        
            | 
            | 
           31 | 
               ) {
  | 
        
        
            | 
            | 
           32 | 
               }
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
               /**
  | 
        
        
            | 
            | 
           35 | 
                * Resolve a decorator definition to a value.
  | 
        
        
            | 
            | 
           36 | 
                *
  | 
        
        
            | 
            | 
           37 | 
                * This will call the callable of the definition and pass it the decorated entry.
  | 
        
        
            | 
            | 
           38 | 
                *
  | 
        
        
            | 
            | 
           39 | 
                * @param DecoratorDefinition $definition
  | 
        
        
            | 
            | 
           40 | 
                */
  | 
        
        
            | 
            | 
           41 | 
               public function resolve(Definition $definition, array $parameters = []) : mixed
  | 
        
        
            | 
            | 
           42 | 
               {
  | 
        
        
            | 
            | 
           43 | 
                   $callable = $definition->getCallable();
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
                   if (! is_callable($callable)) {
  | 
        
        
            | 
            | 
           46 | 
                       throw new InvalidDefinition(sprintf(
  | 
        
        
            | 
            | 
           47 | 
                           'The decorator "%s" is not callable',
  | 
        
        
            | 
            | 
           48 | 
                           $definition->getName()
  | 
        
        
            | 
            | 
           49 | 
                       ));
  | 
        
        
            | 
            | 
           50 | 
                   }
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
                   $decoratedDefinition = $definition->getDecoratedDefinition();
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
                   if (! $decoratedDefinition instanceof Definition) {
  | 
        
        
            | 
            | 
           55 | 
                       if (! $definition->getName()) {
  | 
        
        
            | 
            | 
           56 | 
                           throw new InvalidDefinition('Decorators cannot be nested in another definition');
  | 
        
        
            | 
            | 
           57 | 
                       }
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                       throw new InvalidDefinition(sprintf(
  | 
        
        
            | 
            | 
           60 | 
                           'Entry "%s" decorates nothing: no previous definition with the same name was found',
  | 
        
        
            | 
            | 
           61 | 
                           $definition->getName()
  | 
        
        
            | 
            | 
           62 | 
                       ));
  | 
        
        
            | 
            | 
           63 | 
                   }
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   $decorated = $this->definitionResolver->resolve($decoratedDefinition, $parameters);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   return $callable($decorated, $this->container);
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               public function isResolvable(Definition $definition, array $parameters = []) : bool
  | 
        
        
            | 
            | 
           71 | 
               {
  | 
        
        
            | 
            | 
           72 | 
                   return true;
  | 
        
        
            | 
            | 
           73 | 
               }
  | 
        
        
            | 
            | 
           74 | 
           }
  |