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 |