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 / Source / DefinitionNormalizer.php

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

121 lines 4.0 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\Source;
6
7 use WPMU_DEV\Defender\Vendor\DI\Definition\ArrayDefinition;
8 use WPMU_DEV\Defender\Vendor\DI\Definition\AutowireDefinition;
9 use WPMU_DEV\Defender\Vendor\DI\Definition\DecoratorDefinition;
10 use WPMU_DEV\Defender\Vendor\DI\Definition\Definition;
11 use WPMU_DEV\Defender\Vendor\DI\Definition\Exception\InvalidDefinition;
12 use WPMU_DEV\Defender\Vendor\DI\Definition\FactoryDefinition;
13 use WPMU_DEV\Defender\Vendor\DI\Definition\Helper\DefinitionHelper;
14 use WPMU_DEV\Defender\Vendor\DI\Definition\ObjectDefinition;
15 use WPMU_DEV\Defender\Vendor\DI\Definition\ValueDefinition;
16
17 /**
18 * Turns raw definitions/definition helpers into definitions ready
19 * to be resolved or compiled.
20 *
21 * @author Matthieu Napoli <[email protected]>
22 */
23 class DefinitionNormalizer
24 {
25 /**
26 * @var Autowiring
27 */
28 private $autowiring;
29
30 public function __construct(Autowiring $autowiring)
31 {
32 $this->autowiring = $autowiring;
33 }
34
35 /**
36 * Normalize a definition that is *not* nested in another one.
37 *
38 * This is usually a definition declared at the root of a definition array.
39 *
40 * @param mixed $definition
41 * @param string $name The definition name.
42 * @param string[] $wildcardsReplacements Replacements for wildcard definitions.
43 *
44 * @throws InvalidDefinition
45 */
46 public function normalizeRootDefinition($definition, string $name, array $wildcardsReplacements = null) : Definition
47 {
48 if ($definition instanceof DefinitionHelper) {
49 $definition = $definition->getDefinition($name);
50 } elseif (is_array($definition)) {
51 $definition = new ArrayDefinition($definition);
52 } elseif ($definition instanceof \Closure) {
53 $definition = new FactoryDefinition($name, $definition);
54 } elseif (! $definition instanceof Definition) {
55 $definition = new ValueDefinition($definition);
56 }
57
58 // For a class definition, we replace * in the class name with the matches
59 // *Interface -> *Impl => FooInterface -> FooImpl
60 if ($wildcardsReplacements && $definition instanceof ObjectDefinition) {
61 $definition->replaceWildcards($wildcardsReplacements);
62 }
63
64 if ($definition instanceof AutowireDefinition) {
65 $definition = $this->autowiring->autowire($name, $definition);
66 }
67
68 $definition->setName($name);
69
70 try {
71 $definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']);
72 } catch (InvalidDefinition $e) {
73 throw InvalidDefinition::create($definition, sprintf(
74 'Definition "%s" contains an error: %s',
75 $definition->getName(),
76 $e->getMessage()
77 ), $e);
78 }
79
80 return $definition;
81 }
82
83 /**
84 * Normalize a definition that is nested in another one.
85 *
86 * @param mixed $definition
87 * @return mixed
88 *
89 * @throws InvalidDefinition
90 */
91 public function normalizeNestedDefinition($definition)
92 {
93 $name = '<nested definition>';
94
95 if ($definition instanceof DefinitionHelper) {
96 $definition = $definition->getDefinition($name);
97 } elseif (is_array($definition)) {
98 $definition = new ArrayDefinition($definition);
99 } elseif ($definition instanceof \Closure) {
100 $definition = new FactoryDefinition($name, $definition);
101 }
102
103 if ($definition instanceof DecoratorDefinition) {
104 throw new InvalidDefinition('Decorators cannot be nested in another definition');
105 }
106
107 if ($definition instanceof AutowireDefinition) {
108 $definition = $this->autowiring->autowire($name, $definition);
109 }
110
111 if ($definition instanceof Definition) {
112 $definition->setName($name);
113
114 // Recursively traverse nested definitions
115 $definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']);
116 }
117
118 return $definition;
119 }
120 }
121