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