PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.0.0
Defender Security – Malware Scanner, Login Security & Firewall v6.0.0
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 6.0.0, at lib/packages/DI/Definition/Source/DefinitionNormalizer.php

113 lines 3.9 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 public function __construct(
26 private Autowiring $autowiring,
27 ) {
28 }
29
30 /**
31 * Normalize a definition that is *not* nested in another one.
32 *
33 * This is usually a definition declared at the root of a definition array.
34 *
35 * @param string $name The definition name.
36 * @param string[] $wildcardsReplacements Replacements for wildcard definitions.
37 *
38 * @throws InvalidDefinition
39 */
40 public function normalizeRootDefinition(mixed $definition, string $name, ?array $wildcardsReplacements = null) : Definition
41 {
42 if ($definition instanceof DefinitionHelper) {
43 $definition = $definition->getDefinition($name);
44 } elseif (is_array($definition)) {
45 $definition = new ArrayDefinition($definition);
46 } elseif ($definition instanceof \Closure) {
47 $definition = new FactoryDefinition($name, $definition);
48 } elseif (! $definition instanceof Definition) {
49 $definition = new ValueDefinition($definition);
50 }
51
52 // For a class definition, we replace * in the class name with the matches
53 // *Interface -> *Impl => FooInterface -> FooImpl
54 if ($wildcardsReplacements && $definition instanceof ObjectDefinition) {
55 $definition->replaceWildcards($wildcardsReplacements);
56 }
57
58 if ($definition instanceof AutowireDefinition) {
59 /** @var AutowireDefinition $definition */
60 $definition = $this->autowiring->autowire($name, $definition);
61 }
62
63 $definition->setName($name);
64
65 try {
66 $definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']);
67 } catch (InvalidDefinition $e) {
68 throw InvalidDefinition::create($definition, sprintf(
69 'Definition "%s" contains an error: %s',
70 $definition->getName(),
71 $e->getMessage()
72 ), $e);
73 }
74
75 return $definition;
76 }
77
78 /**
79 * Normalize a definition that is nested in another one.
80 *
81 * @throws InvalidDefinition
82 */
83 public function normalizeNestedDefinition(mixed $definition) : mixed
84 {
85 $name = '<nested definition>';
86
87 if ($definition instanceof DefinitionHelper) {
88 $definition = $definition->getDefinition($name);
89 } elseif (is_array($definition)) {
90 $definition = new ArrayDefinition($definition);
91 } elseif ($definition instanceof \Closure) {
92 $definition = new FactoryDefinition($name, $definition);
93 }
94
95 if ($definition instanceof DecoratorDefinition) {
96 throw new InvalidDefinition('Decorators cannot be nested in another definition');
97 }
98
99 if ($definition instanceof AutowireDefinition) {
100 $definition = $this->autowiring->autowire($name, $definition);
101 }
102
103 if ($definition instanceof Definition) {
104 $definition->setName($name);
105
106 // Recursively traverse nested definitions
107 $definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']);
108 }
109
110 return $definition;
111 }
112 }
113