PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.0
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 / DefinitionArray.php

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

156 lines 4.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 WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source;
6
7 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ObjectDefinition;
9
10 /**
11 * Reads WPDeveloper\BetterDocs\Dependencies\DI definitions from a PHP array.
12 *
13 * @author Matthieu Napoli <matthieu@mnapoli.fr>
14 */
15 class DefinitionArray implements DefinitionSource, MutableDefinitionSource
16 {
17 const WILDCARD = '*';
18 /**
19 * Matches anything except "\".
20 */
21 const WILDCARD_PATTERN = '([^\\\\]+)';
22
23 /**
24 * WPDeveloper\BetterDocs\Dependencies\DI definitions in a PHP array.
25 * @var array
26 */
27 private $definitions = [];
28
29 /**
30 * Cache of wildcard definitions.
31 * @var array|null
32 */
33 private $wildcardDefinitions;
34
35 /**
36 * @var DefinitionNormalizer
37 */
38 private $normalizer;
39
40 /**
41 * @param array $definitions
42 */
43 public function __construct(array $definitions = [], ?Autowiring $autowiring = null)
44 {
45 if (isset($definitions[0])) {
46 throw new \Exception('The PHP-WPDeveloper\BetterDocs\Dependencies\DI definition is not indexed by an entry name in the definition array');
47 }
48
49 $this->definitions = $definitions;
50
51 $autowiring = $autowiring ?: new NoAutowiring;
52 $this->normalizer = new DefinitionNormalizer($autowiring);
53 }
54
55 /**
56 * @param array $definitions WPDeveloper\BetterDocs\Dependencies\DI definitions in a PHP array indexed by the definition name.
57 */
58 public function addDefinitions(array $definitions)
59 {
60 if (isset($definitions[0])) {
61 throw new \Exception('The PHP-WPDeveloper\BetterDocs\Dependencies\DI definition is not indexed by an entry name in the definition array');
62 }
63
64 // The newly added data prevails
65 // "for keys that exist in both arrays, the elements from the left-hand array will be used"
66 $this->definitions = $definitions + $this->definitions;
67
68 // Clear cache
69 $this->wildcardDefinitions = null;
70 }
71
72 /**
73 * {@inheritdoc}
74 */
75 public function addDefinition(Definition $definition)
76 {
77 $this->definitions[$definition->getName()] = $definition;
78
79 // Clear cache
80 $this->wildcardDefinitions = null;
81 }
82
83 public function getDefinition(string $name)
84 {
85 // Look for the definition by name
86 if (array_key_exists($name, $this->definitions)) {
87 $definition = $this->definitions[$name];
88 $definition = $this->normalizer->normalizeRootDefinition($definition, $name);
89
90 return $definition;
91 }
92
93 // Build the cache of wildcard definitions
94 if ($this->wildcardDefinitions === null) {
95 $this->wildcardDefinitions = [];
96 foreach ($this->definitions as $key => $definition) {
97 if (strpos($key, self::WILDCARD) !== false) {
98 $this->wildcardDefinitions[$key] = $definition;
99 }
100 }
101 }
102
103 // Look in wildcards definitions
104 foreach ($this->wildcardDefinitions as $key => $definition) {
105 // Turn the pattern into a regex
106 $key = preg_quote($key);
107 $key = '#' . str_replace('\\' . self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#';
108 if (preg_match($key, $name, $matches) === 1) {
109 $definition = $this->normalizer->normalizeRootDefinition($definition, $name);
110
111 // For a class definition, we replace * in the class name with the matches
112 // *Interface -> *Impl => FooInterface -> FooImpl
113 if ($definition instanceof ObjectDefinition) {
114 array_shift($matches);
115 $definition->setClassName(
116 $this->replaceWildcards($definition->getClassName(), $matches)
117 );
118 }
119
120 return $definition;
121 }
122 }
123
124 return null;
125 }
126
127 public function getDefinitions() : array
128 {
129 // Return all definitions except wildcard definitions
130 $definitions = [];
131 foreach ($this->definitions as $key => $definition) {
132 if (strpos($key, self::WILDCARD) === false) {
133 $definitions[$key] = $definition;
134 }
135 }
136
137 return $definitions;
138 }
139
140 /**
141 * Replaces all the wildcards in the string with the given replacements.
142 * @param string[] $replacements
143 */
144 private function replaceWildcards(string $string, array $replacements) : string
145 {
146 foreach ($replacements as $replacement) {
147 $pos = strpos($string, self::WILDCARD);
148 if ($pos !== false) {
149 $string = substr_replace($string, $replacement, $pos, 1);
150 }
151 }
152
153 return $string;
154 }
155 }
156