PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / DI / Definition / Resolver / DecoratorResolver.php

DecoratorResolver.php in Defender Security – Malware Scanner, Login Security & Firewall 5.5.1, at lib/packages/DI/Definition/Resolver/DecoratorResolver.php

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