1 | <?php |
||
16 | abstract class ClassDiscovery |
||
17 | { |
||
18 | /** |
||
19 | * A list of strategies to find classes. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $strategies = [ |
||
24 | Strategy\PuliBetaStrategy::class, |
||
25 | Strategy\CommonClassesStrategy::class, |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * Discovery cache to make the second time we use discovery faster. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private static $cache = []; |
||
34 | |||
35 | /** |
||
36 | * Finds a class. |
||
37 | * |
||
38 | * @param string $type |
||
39 | * |
||
40 | * @return string|\Closure |
||
41 | * |
||
42 | * @throws DiscoveryFailedException |
||
43 | */ |
||
44 | 15 | protected static function findOneByType($type) |
|
77 | |||
78 | /** |
||
79 | * Get a value from cache. |
||
80 | * |
||
81 | * @param string $type |
||
82 | * |
||
83 | * @return string|null |
||
84 | */ |
||
85 | 15 | private static function getFromCache($type) |
|
100 | |||
101 | /** |
||
102 | * Store a value in cache. |
||
103 | * |
||
104 | * @param string $type |
||
105 | * @param string $class |
||
106 | */ |
||
107 | 9 | private static function storeInCache($type, $class) |
|
111 | |||
112 | /** |
||
113 | * Set new strategies and clear the cache. |
||
114 | * |
||
115 | * @param array $strategies string array of fully qualified class name to a DiscoveryStrategy |
||
116 | */ |
||
117 | 27 | public static function setStrategies(array $strategies) |
|
122 | |||
123 | /** |
||
124 | * Append a strategy at the end of the strategy queue. |
||
125 | * |
||
126 | * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
||
127 | */ |
||
128 | 1 | public static function appendStrategy($strategy) |
|
133 | |||
134 | /** |
||
135 | * Prepend a strategy at the beginning of the strategy queue. |
||
136 | * |
||
137 | * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
||
138 | */ |
||
139 | 1 | public static function prependStrategy($strategy) |
|
144 | |||
145 | /** |
||
146 | * Clear the cache. |
||
147 | */ |
||
148 | 27 | public static function clearCache() |
|
152 | |||
153 | /** |
||
154 | * Evaluates conditions to boolean. |
||
155 | * |
||
156 | * @param mixed $condition |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 7 | protected static function evaluateCondition($condition) |
|
161 | { |
||
162 | 7 | if (is_string($condition)) { |
|
163 | // Should be extended for functions, extensions??? |
||
164 | return self::safeClassExists($condition); |
||
165 | 7 | } elseif (is_callable($condition)) { |
|
166 | return $condition(); |
||
167 | 7 | } elseif (is_bool($condition)) { |
|
168 | 7 | return $condition; |
|
169 | } elseif (is_array($condition)) { |
||
170 | // Immediately stop execution if the condition is false |
||
171 | for ($i = 0; $i < count($condition); ++$i) { |
||
|
|||
172 | if (false === static::evaluateCondition($condition[$i])) { |
||
173 | return false; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return true; |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get an instance of the $class. |
||
185 | * |
||
186 | * @param string|\Closure $class A FQCN of a class or a closure that instantiate the class. |
||
187 | * |
||
188 | * @return object |
||
189 | * |
||
190 | * @throws ClassInstantiationFailedException |
||
191 | */ |
||
192 | 5 | protected static function instantiateClass($class) |
|
208 | |||
209 | /** |
||
210 | * We want to do a "safe" version of PHP's "class_exists" because Magento has a bug |
||
211 | * (or they call it a "feature"). Magento is throwing an exception if you do class_exists() |
||
212 | * on a class that ends with "Factory" and if that file does not exits. |
||
213 | * |
||
214 | * This function will catch all potential exceptions and make sure it returns a boolean. |
||
215 | * |
||
216 | * @param string $class |
||
217 | * @param bool $autoload |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public static function safeClassExists($class) |
||
229 | } |
||
230 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: