PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.8.1
Defender Security – Malware Scanner, Login Security & Firewall v5.8.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.8.1, at lib/packages/DI/Definition/Resolver/DecoratorResolver.php

75 lines 2.3 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 * @template-implements DefinitionResolver<DecoratorDefinition>
16 *
17 * @since 5.0
18 * @author Matthieu Napoli <[email protected]>
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 }
75