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

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

111 lines 3.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 WPDeveloper\BetterDocs\Dependencies\DI\Definition\Source;
6
7 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\Definition;
8 use WPDeveloper\BetterDocs\Dependencies\DI\Definition\ExtendsPreviousDefinition;
9
10 /**
11 * Manages a chain of other definition sources.
12 *
13 * @author Matthieu Napoli <matthieu@mnapoli.fr>
14 */
15 class SourceChain implements DefinitionSource, MutableDefinitionSource
16 {
17 /**
18 * @var DefinitionSource[]
19 */
20 private $sources;
21
22 /**
23 * @var DefinitionSource
24 */
25 private $rootSource;
26
27 /**
28 * @var MutableDefinitionSource|null
29 */
30 private $mutableSource;
31
32 /**
33 * @param DefinitionSource[] $sources
34 */
35 public function __construct(array $sources)
36 {
37 // We want a numerically indexed array to ease the traversal later
38 $this->sources = array_values($sources);
39 $this->rootSource = $this;
40 }
41
42 /**
43 * {@inheritdoc}
44 *
45 * @param int $startIndex Use this parameter to start looking from a specific
46 * point in the source chain.
47 */
48 public function getDefinition(string $name, int $startIndex = 0)
49 {
50 $count = count($this->sources);
51 for ($i = $startIndex; $i < $count; ++$i) {
52 $source = $this->sources[$i];
53
54 $definition = $source->getDefinition($name);
55
56 if ($definition) {
57 if ($definition instanceof ExtendsPreviousDefinition) {
58 $this->resolveExtendedDefinition($definition, $i);
59 }
60
61 return $definition;
62 }
63 }
64
65 return null;
66 }
67
68 public function getDefinitions() : array
69 {
70 $names = [];
71 foreach ($this->sources as $source) {
72 $names = array_merge($names, $source->getDefinitions());
73 }
74 $names = array_keys($names);
75
76 $definitions = array_combine($names, array_map(function (string $name) {
77 return $this->getDefinition($name);
78 }, $names));
79
80 return $definitions;
81 }
82
83 public function addDefinition(Definition $definition)
84 {
85 if (! $this->mutableSource) {
86 throw new \LogicException("The container's definition source has not been initialized correctly");
87 }
88
89 $this->mutableSource->addDefinition($definition);
90 }
91
92 private function resolveExtendedDefinition(ExtendsPreviousDefinition $definition, int $currentIndex)
93 {
94 // Look in the next sources only (else infinite recursion, and we can only extend
95 // entries defined in the previous definition files - a previous == next here because
96 // the array was reversed ;) )
97 $subDefinition = $this->getDefinition($definition->getName(), $currentIndex + 1);
98
99 if ($subDefinition) {
100 $definition->setExtendedDefinition($subDefinition);
101 }
102 }
103
104 public function setMutableDefinitionSource(MutableDefinitionSource $mutableSource)
105 {
106 $this->mutableSource = $mutableSource;
107
108 array_unshift($this->sources, $mutableSource);
109 }
110 }
111