PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / vendor / php-di / php-di / src / Definition / Source / SourceChain.php

SourceChain.php in The Innovative Form Builder – IvyForms 0.6.1-backup, at backend/vendor/php-di/php-di/src/Definition/Source/SourceChain.php

111 lines 2.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 DI\Definition\Source;
6
7 use DI\Definition\Definition;
8 use DI\Definition\ExtendsPreviousDefinition;
9
10 /**
11 * Manages a chain of other definition sources.
12 *
13 * @author Matthieu Napoli <[email protected]>
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