PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 3.10.1
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v3.10.1
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / addons / inc / Dependencies / DI / Definition / Source / DefinitionArray.php
cookiebot / addons / inc / Dependencies / DI / Definition / Source Last commit date
AnnotationReader.php 5 years ago Autowiring.php 5 years ago CachedDefinitionSource.php 5 years ago DefinitionArray.php 5 years ago DefinitionFile.php 5 years ago DefinitionSource.php 5 years ago MutableDefinitionSource.php 5 years ago SourceChain.php 5 years ago
DefinitionArray.php
143 lines
1 <?php
2 /**
3 * PHP-DI
4 *
5 * @link http://php-di.org/
6 * @copyright Matthieu Napoli (http://mnapoli.fr/)
7 * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
8 */
9
10 namespace Cybot\Dependencies\DI\Definition\Source;
11
12 use Cybot\Dependencies\DI\Definition\ArrayDefinition;
13 use Cybot\Dependencies\DI\Definition\ObjectDefinition;
14 use Cybot\Dependencies\DI\Definition\Definition;
15 use Cybot\Dependencies\DI\Definition\FactoryDefinition;
16 use Cybot\Dependencies\DI\Definition\ValueDefinition;
17 use Cybot\Dependencies\DI\Definition\Helper\DefinitionHelper;
18
19 /**
20 * Reads DI definitions from a PHP array.
21 *
22 * @author Matthieu Napoli <matthieu@mnapoli.fr>
23 */
24 class DefinitionArray implements DefinitionSource, MutableDefinitionSource
25 {
26 const WILDCARD = '*';
27 /**
28 * Matches anything except "\"
29 */
30 const WILDCARD_PATTERN = '([^\\\\]+)';
31
32 /**
33 * DI definitions in a PHP array
34 * @var array
35 */
36 private $definitions = [];
37
38 /**
39 * @param array $definitions
40 */
41 public function __construct(array $definitions = [])
42 {
43 $this->definitions = $definitions;
44 }
45
46 /**
47 * @param array $definitions DI definitions in a PHP array indexed by the definition name.
48 */
49 public function addDefinitions(array $definitions)
50 {
51 // The newly added data prevails
52 // "for keys that exist in both arrays, the elements from the left-hand array will be used"
53 $this->definitions = $definitions + $this->definitions;
54 }
55
56 /**
57 * {@inheritdoc}
58 */
59 public function addDefinition(Definition $definition)
60 {
61 $this->definitions[$definition->getName()] = $definition;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function getDefinition($name)
68 {
69 // Look for the definition by name
70 if (array_key_exists($name, $this->definitions)) {
71 return $this->castDefinition($this->definitions[$name], $name);
72 }
73
74 // Look if there are wildcards definitions
75 foreach ($this->definitions as $key => $definition) {
76 if (strpos($key, self::WILDCARD) === false) {
77 continue;
78 }
79
80 // Turn the pattern into a regex
81 $key = addslashes($key);
82 $key = '#' . str_replace(self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#';
83 if (preg_match($key, $name, $matches) === 1) {
84 $definition = $this->castDefinition($definition, $name);
85
86 // For a class definition, we replace * in the class name with the matches
87 // *Interface -> *Impl => FooInterface -> FooImpl
88 if ($definition instanceof ObjectDefinition) {
89 array_shift($matches);
90 $definition->setClassName(
91 $this->replaceWildcards($definition->getClassName(), $matches)
92 );
93 }
94
95 return $definition;
96 }
97 }
98
99 return null;
100 }
101
102 /**
103 * @param mixed $definition
104 * @param string $name
105 * @return Definition
106 */
107 private function castDefinition($definition, $name)
108 {
109 if ($definition instanceof DefinitionHelper) {
110 $definition = $definition->getDefinition($name);
111 }
112 if (! $definition instanceof Definition && is_array($definition)) {
113 $definition = new ArrayDefinition($name, $definition);
114 }
115 if ($definition instanceof \Closure) {
116 $definition = new FactoryDefinition($name, $definition);
117 }
118 if (! $definition instanceof Definition) {
119 $definition = new ValueDefinition($name, $definition);
120 }
121
122 return $definition;
123 }
124
125 /**
126 * Replaces all the wildcards in the string with the given replacements.
127 * @param string $string
128 * @param string[] $replacements
129 * @return string
130 */
131 private function replaceWildcards($string, array $replacements)
132 {
133 foreach ($replacements as $replacement) {
134 $pos = strpos($string, self::WILDCARD);
135 if ($pos !== false) {
136 $string = substr_replace($string, $replacement, $pos, 1);
137 }
138 }
139
140 return $string;
141 }
142 }
143