PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.4.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.4.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / DI / Definition / Source / DefinitionNormalizer.php

DefinitionNormalizer.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.4.2, at includes/Dependencies/DI/Definition/Source/DefinitionNormalizer.php

113 lines 3.6 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 WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source;
6
7 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ArrayDefinition;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\AutowireDefinition;
9 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\DecoratorDefinition;
10 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
11 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Exception\InvalidDefinition;
12 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\FactoryDefinition;
13 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Helper\DefinitionHelper;
14 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ValueDefinition;
15
16 /**
17 * Turns raw definitions/definition helpers into definitions ready
18 * to be resolved or compiled.
19 *
20 * @author Matthieu Napoli <matthieu@mnapoli.fr>
21 */
22 class DefinitionNormalizer
23 {
24 /**
25 * @var Autowiring
26 */
27 private $autowiring;
28
29 public function __construct(Autowiring $autowiring)
30 {
31 $this->autowiring = $autowiring;
32 }
33
34 /**
35 * Normalize a definition that is *not* nested in another one.
36 *
37 * This is usually a definition declared at the root of a definition array.
38 *
39 * @param mixed $definition
40 * @param string $name The definition name.
41 *
42 * @throws InvalidDefinition
43 */
44 public function normalizeRootDefinition($definition, string $name) : Definition
45 {
46 if ($definition instanceof DefinitionHelper) {
47 $definition = $definition->getDefinition($name);
48 } elseif (is_array($definition)) {
49 $definition = new ArrayDefinition($definition);
50 } elseif ($definition instanceof \Closure) {
51 $definition = new FactoryDefinition($name, $definition);
52 } elseif (! $definition instanceof Definition) {
53 $definition = new ValueDefinition($definition);
54 }
55
56 if ($definition instanceof AutowireDefinition) {
57 $definition = $this->autowiring->autowire($name, $definition);
58 }
59
60 $definition->setName($name);
61
62 try {
63 $definition->replaceNestedDefinitions([$this, 'normalizeNestedDefinition']);
64 } catch (InvalidDefinition $e) {
65 throw InvalidDefinition::create($definition, sprintf(
66 'Definition "%s" contains an error: %s',
67 $definition->getName(),
68 $e->getMessage()
69 ), $e);
70 }
71
72 return $definition;
73 }
74
75 /**
76 * Normalize a definition that is nested in another one.
77 *
78 * @param mixed $definition
79 * @return mixed
80 *
81 * @throws InvalidDefinition
82 */
83 public function normalizeNestedDefinition($definition)
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