PluginProbe
WPIDE – File Manager & Code Editor / 3.5.10
WPIDE – File Manager & Code Editor v3.5.10
3.5.10 3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 All 56 releases
wpide / vendor / php-di / php-di / src / Definition / Source / DefinitionNormalizer.php

DefinitionNormalizer.php in WPIDE – File Manager & Code Editor 3.5.10, at vendor/php-di/php-di/src/Definition/Source/DefinitionNormalizer.php

121 lines 3.8 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 DI\Definition\Source;
6
7 use DI\Definition\ArrayDefinition;
8 use DI\Definition\AutowireDefinition;
9 use DI\Definition\DecoratorDefinition;
10 use DI\Definition\Definition;
11 use DI\Definition\Exception\InvalidDefinition;
12 use DI\Definition\FactoryDefinition;
13 use DI\Definition\Helper\DefinitionHelper;
14 use DI\Definition\ObjectDefinition;
15 use 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