1 | <?php |
||
20 | class ClassInterfaceCache |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
||
25 | * |
||
26 | * @var string[][] $interfaces |
||
27 | */ |
||
28 | private $interfaces = array(); |
||
29 | |||
30 | /** |
||
31 | * @type string[][] $aliases |
||
32 | */ |
||
33 | protected $aliases = array(); |
||
34 | |||
35 | |||
36 | /** |
||
37 | * @param string $fqn |
||
38 | * @return string |
||
39 | */ |
||
40 | public function getFqn($fqn) |
||
44 | |||
45 | |||
46 | /** |
||
47 | * @param string $fqn |
||
48 | * @return array |
||
49 | */ |
||
50 | public function getInterfaces($fqn) |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param string $fqn |
||
69 | * @param string $interface |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function hasInterface($fqn, $interface) |
||
78 | |||
79 | |||
80 | /** |
||
81 | * adds an alias for a classname |
||
82 | * |
||
83 | * @param string $fqn the class name that should be used (concrete class to replace interface) |
||
84 | * @param string $alias the class name that would be type hinted for (abstract parent or interface) |
||
85 | * @param string $for_class the class that has the dependency (is type hinting for the interface) |
||
86 | */ |
||
87 | public function addAlias($fqn, $alias, $for_class = '') |
||
88 | { |
||
89 | $fqn = $this->getFqn($fqn); |
||
90 | $alias = $this->getFqn($alias); |
||
91 | // are we adding an alias for a specific class? |
||
92 | if ($for_class !== '') { |
||
93 | // make sure it's set up as an array |
||
94 | if (! isset($this->aliases[ $for_class ])) { |
||
95 | $this->aliases[ $for_class ] = array(); |
||
96 | } |
||
97 | $this->aliases[ $for_class ][ $alias ] = $fqn; |
||
98 | return; |
||
99 | } |
||
100 | $this->aliases[ $alias ] = $fqn; |
||
101 | } |
||
102 | |||
103 | |||
104 | /** |
||
105 | * returns TRUE if the provided FQN is an alias |
||
106 | * |
||
107 | * @param string $fqn |
||
108 | * @param string $for_class |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function isAlias($fqn = '', $for_class = '') |
||
122 | |||
123 | |||
124 | /** |
||
125 | * returns TRUE if the provided FQN is an alias |
||
126 | * |
||
127 | * @param string $fqn |
||
128 | * @return bool |
||
129 | */ |
||
130 | protected function isDirectAlias($fqn = '') |
||
131 | { |
||
132 | return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]); |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * returns TRUE if the provided FQN is an alias for the specified class |
||
138 | * |
||
139 | * @param string $fqn |
||
140 | * @param string $for_class |
||
141 | * @return bool |
||
142 | */ |
||
143 | protected function isAliasForClass($fqn = '', $for_class = '') |
||
150 | |||
151 | |||
152 | /** |
||
153 | * returns FQN for provided alias if one exists, otherwise returns the original FQN |
||
154 | * functions recursively, so that multiple aliases can be used to drill down to a FQN |
||
155 | * for example: |
||
156 | * if the following two entries were added to the aliases array: |
||
157 | * array( |
||
158 | * 'interface_alias' => 'some\namespace\interface' |
||
159 | * 'some\namespace\interface' => 'some\namespace\classname' |
||
160 | * ) |
||
161 | * then one could use Loader::getNew( 'interface_alias' ) |
||
162 | * to load an instance of 'some\namespace\classname' |
||
163 | * |
||
164 | * @param string $alias |
||
165 | * @param string $for_class |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getFqnForAlias($alias = '', $for_class = '') |
||
179 | } |
||
180 |