| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace ElementorDeps\DI\Definition\Source; |
| 5 |
|
| 6 |
use ElementorDeps\DI\Definition\Definition; |
| 7 |
/** |
| 8 |
* Reads DI definitions from a PHP array. |
| 9 |
* |
| 10 |
* @author Matthieu Napoli <matthieu@mnapoli.fr> |
| 11 |
*/ |
| 12 |
class DefinitionArray implements DefinitionSource, MutableDefinitionSource |
| 13 |
{ |
| 14 |
const WILDCARD = '*'; |
| 15 |
/** |
| 16 |
* Matches anything except "\". |
| 17 |
*/ |
| 18 |
const WILDCARD_PATTERN = '([^\\\\]+)'; |
| 19 |
/** |
| 20 |
* DI definitions in a PHP array. |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private $definitions = []; |
| 24 |
/** |
| 25 |
* Cache of wildcard definitions. |
| 26 |
* @var array|null |
| 27 |
*/ |
| 28 |
private $wildcardDefinitions; |
| 29 |
/** |
| 30 |
* @var DefinitionNormalizer |
| 31 |
*/ |
| 32 |
private $normalizer; |
| 33 |
public function __construct(array $definitions = [], Autowiring $autowiring = null) |
| 34 |
{ |
| 35 |
if (isset($definitions[0])) { |
| 36 |
throw new \Exception('The PHP-DI definition is not indexed by an entry name in the definition array'); |
| 37 |
} |
| 38 |
$this->definitions = $definitions; |
| 39 |
$autowiring = $autowiring ?: new NoAutowiring(); |
| 40 |
$this->normalizer = new DefinitionNormalizer($autowiring); |
| 41 |
} |
| 42 |
/** |
| 43 |
* @param array $definitions DI definitions in a PHP array indexed by the definition name. |
| 44 |
*/ |
| 45 |
public function addDefinitions(array $definitions) |
| 46 |
{ |
| 47 |
if (isset($definitions[0])) { |
| 48 |
throw new \Exception('The PHP-DI definition is not indexed by an entry name in the definition array'); |
| 49 |
} |
| 50 |
// The newly added data prevails |
| 51 |
// "for keys that exist in both arrays, the elements from the left-hand array will be used" |
| 52 |
$this->definitions = $definitions + $this->definitions; |
| 53 |
// Clear cache |
| 54 |
$this->wildcardDefinitions = null; |
| 55 |
} |
| 56 |
/** |
| 57 |
* {@inheritdoc} |
| 58 |
*/ |
| 59 |
public function addDefinition(Definition $definition) |
| 60 |
{ |
| 61 |
$this->definitions[$definition->getName()] = $definition; |
| 62 |
// Clear cache |
| 63 |
$this->wildcardDefinitions = null; |
| 64 |
} |
| 65 |
public function getDefinition(string $name) |
| 66 |
{ |
| 67 |
// Look for the definition by name |
| 68 |
if (\array_key_exists($name, $this->definitions)) { |
| 69 |
$definition = $this->definitions[$name]; |
| 70 |
$definition = $this->normalizer->normalizeRootDefinition($definition, $name); |
| 71 |
return $definition; |
| 72 |
} |
| 73 |
// Build the cache of wildcard definitions |
| 74 |
if ($this->wildcardDefinitions === null) { |
| 75 |
$this->wildcardDefinitions = []; |
| 76 |
foreach ($this->definitions as $key => $definition) { |
| 77 |
if (\strpos($key, self::WILDCARD) !== \false) { |
| 78 |
$this->wildcardDefinitions[$key] = $definition; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
// Look in wildcards definitions |
| 83 |
foreach ($this->wildcardDefinitions as $key => $definition) { |
| 84 |
// Turn the pattern into a regex |
| 85 |
$key = \preg_quote($key); |
| 86 |
$key = '#' . \str_replace('\\' . self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#'; |
| 87 |
if (\preg_match($key, $name, $matches) === 1) { |
| 88 |
\array_shift($matches); |
| 89 |
$definition = $this->normalizer->normalizeRootDefinition($definition, $name, $matches); |
| 90 |
return $definition; |
| 91 |
} |
| 92 |
} |
| 93 |
return null; |
| 94 |
} |
| 95 |
public function getDefinitions() : array |
| 96 |
{ |
| 97 |
// Return all definitions except wildcard definitions |
| 98 |
$definitions = []; |
| 99 |
foreach ($this->definitions as $key => $definition) { |
| 100 |
if (\strpos($key, self::WILDCARD) === \false) { |
| 101 |
$definitions[$key] = $definition; |
| 102 |
} |
| 103 |
} |
| 104 |
return $definitions; |
| 105 |
} |
| 106 |
} |
| 107 |
|