Completed
Push — master ( f3acb1...ea145d )
by Matthieu
02:49
created

DefinitionArray::getDefinition()   C

↳ Parent: DefinitionArray

Complexity

Conditions 8
Paths 11

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 40
Code Lines 19

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 8
eloc 19
c 2
b 1
f 0
nc 11
nop 1
dl 0
loc 40
rs 5.3846
1
<?php
2
3
namespace DI\Definition\Source;
4
5
use DI\Definition\ArrayDefinition;
6
use DI\Definition\Definition;
7
use DI\Definition\FactoryDefinition;
8
use DI\Definition\Helper\DefinitionHelper;
9
use DI\Definition\ObjectDefinition;
10
use DI\Definition\ValueDefinition;
11
12
/**
13
 * Reads DI definitions from a PHP array.
14
 *
15
 * @author Matthieu Napoli <[email protected]>
16
 */
17
class DefinitionArray implements DefinitionSource, MutableDefinitionSource
18
{
19
    const WILDCARD = '*';
20
    /**
21
     * Matches anything except "\".
22
     */
23
    const WILDCARD_PATTERN = '([^\\\\]+)';
24
25
    /**
26
     * DI definitions in a PHP array.
27
     * @var array
28
     */
29
    private $definitions = [];
30
31
    /**
32
     * Cache of wildcard definitions.
33
     * @var array
34
     */
35
    private $wildcardDefinitions;
36
37
    /**
38
     * @param array $definitions
39
     */
40
    public function __construct(array $definitions = [])
41
    {
42
        $this->definitions = $definitions;
43
    }
44
45
    /**
46
     * @param array $definitions DI definitions in a PHP array indexed by the definition name.
47
     */
48
    public function addDefinitions(array $definitions)
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
54
        // Clear cache
55
        $this->wildcardDefinitions = null;
0 ignored issues
show
Documentation Bug introduced by Matthieu Napoli
It seems like null of type null is incompatible with the declared type array of property $wildcardDefinitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
    }
57
58
    /**
59
     * [email protected]}
60
     */
61
    public function addDefinition(Definition $definition)
62
    {
63
        $this->definitions[$definition->getName()] = $definition;
64
65
        // Clear cache
66
        $this->wildcardDefinitions = null;
0 ignored issues
show
Documentation Bug introduced by Matthieu Napoli
It seems like null of type null is incompatible with the declared type array of property $wildcardDefinitions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67
    }
68
69
    /**
70
     * [email protected]}
71
     */
72
    public function getDefinition($name)
73
    {
74
        // Look for the definition by name
75
        if (array_key_exists($name, $this->definitions)) {
76
            return $this->castDefinition($this->definitions[$name], $name);
77
        }
78
79
        // Build the cache of wildcard definitions
80
        if ($this->wildcardDefinitions === null) {
81
            $this->wildcardDefinitions = [];
82
            foreach ($this->definitions as $key => $definition) {
83
                if (strpos($key, self::WILDCARD) !== false) {
84
                    $this->wildcardDefinitions[$key] = $definition;
85
                }
86
            }
87
        }
88
89
        // Look in wildcards definitions
90
        foreach ($this->wildcardDefinitions as $key => $definition) {
91
            // Turn the pattern into a regex
92
            $key = preg_quote($key);
93
            $key = '#' . str_replace('\\' . self::WILDCARD, self::WILDCARD_PATTERN, $key) . '#';
94
            if (preg_match($key, $name, $matches) === 1) {
95
                $definition = $this->castDefinition($definition, $name);
96
97
                // For a class definition, we replace * in the class name with the matches
98
                // *Interface -> *Impl => FooInterface -> FooImpl
99
                if ($definition instanceof ObjectDefinition) {
100
                    array_shift($matches);
101
                    $definition->setClassName(
102
                        $this->replaceWildcards($definition->getClassName(), $matches)
103
                    );
104
                }
105
106
                return $definition;
107
            }
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * @param mixed  $definition
115
     * @param string $name
116
     * @return Definition
117
     */
118
    private function castDefinition($definition, $name)
119
    {
120
        if ($definition instanceof DefinitionHelper) {
121
            $definition = $definition->getDefinition($name);
122
        } elseif (is_array($definition)) {
123
            $definition = new ArrayDefinition($name, $definition);
124
        } elseif ($definition instanceof \Closure) {
125
            $definition = new FactoryDefinition($name, $definition);
126
        } elseif (! $definition instanceof Definition) {
127
            $definition = new ValueDefinition($name, $definition);
128
        }
129
130
        return $definition;
131
    }
132
133
    /**
134
     * Replaces all the wildcards in the string with the given replacements.
135
     * @param string   $string
136
     * @param string[] $replacements
137
     * @return string
138
     */
139
    private function replaceWildcards($string, array $replacements)
140
    {
141
        foreach ($replacements as $replacement) {
142
            $pos = strpos($string, self::WILDCARD);
143
            if ($pos !== false) {
144
                $string = substr_replace($string, $replacement, $pos, 1);
145
            }
146
        }
147
148
        return $string;
149
    }
150
}
151