Exception
3 years ago
Strategy
6 months ago
ClassDiscovery.php
3 years ago
Exception.php
3 years ago
HttpAsyncClientDiscovery.php
2 years ago
HttpClientDiscovery.php
2 years ago
MessageFactoryDiscovery.php
2 years ago
NotFoundException.php
11 months ago
Psr17FactoryDiscovery.php
6 months ago
Psr18ClientDiscovery.php
11 months ago
StreamFactoryDiscovery.php
2 years ago
UriFactoryDiscovery.php
2 years ago
ClassDiscovery.php
237 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaHttp\Discovery; |
| 4 | |
| 5 | use AmeliaHttp\Discovery\Exception\ClassInstantiationFailedException; |
| 6 | use AmeliaHttp\Discovery\Exception\DiscoveryFailedException; |
| 7 | use AmeliaHttp\Discovery\Exception\NoCandidateFoundException; |
| 8 | use AmeliaHttp\Discovery\Exception\StrategyUnavailableException; |
| 9 | |
| 10 | /** |
| 11 | * Registry that based find results on class existence. |
| 12 | * |
| 13 | * @author David de Boer <david@ddeboer.nl> |
| 14 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 15 | * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 16 | */ |
| 17 | abstract class ClassDiscovery |
| 18 | { |
| 19 | /** |
| 20 | * A list of strategies to find classes. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private static $strategies = [ |
| 25 | Strategy\PuliBetaStrategy::class, |
| 26 | Strategy\CommonClassesStrategy::class, |
| 27 | Strategy\CommonPsr17ClassesStrategy::class, |
| 28 | ]; |
| 29 | |
| 30 | /** |
| 31 | * Discovery cache to make the second time we use discovery faster. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private static $cache = []; |
| 36 | |
| 37 | /** |
| 38 | * Finds a class. |
| 39 | * |
| 40 | * @param string $type |
| 41 | * |
| 42 | * @return string|\Closure |
| 43 | * |
| 44 | * @throws DiscoveryFailedException |
| 45 | */ |
| 46 | protected static function findOneByType($type) |
| 47 | { |
| 48 | // Look in the cache |
| 49 | if (null !== ($class = self::getFromCache($type))) { |
| 50 | return $class; |
| 51 | } |
| 52 | |
| 53 | $exceptions = []; |
| 54 | foreach (self::$strategies as $strategy) { |
| 55 | try { |
| 56 | $candidates = call_user_func($strategy.'::getCandidates', $type); |
| 57 | } catch (StrategyUnavailableException $e) { |
| 58 | $exceptions[] = $e; |
| 59 | |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | foreach ($candidates as $candidate) { |
| 64 | if (isset($candidate['condition'])) { |
| 65 | if (!self::evaluateCondition($candidate['condition'])) { |
| 66 | continue; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // save the result for later use |
| 71 | self::storeInCache($type, $candidate); |
| 72 | |
| 73 | return $candidate['class']; |
| 74 | } |
| 75 | |
| 76 | $exceptions[] = new NoCandidateFoundException($strategy, $candidates); |
| 77 | } |
| 78 | |
| 79 | throw DiscoveryFailedException::create($exceptions); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get a value from cache. |
| 84 | * |
| 85 | * @param string $type |
| 86 | * |
| 87 | * @return string|null |
| 88 | */ |
| 89 | private static function getFromCache($type) |
| 90 | { |
| 91 | if (!isset(self::$cache[$type])) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $candidate = self::$cache[$type]; |
| 96 | if (isset($candidate['condition'])) { |
| 97 | if (!self::evaluateCondition($candidate['condition'])) { |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $candidate['class']; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Store a value in cache. |
| 107 | * |
| 108 | * @param string $type |
| 109 | * @param string $class |
| 110 | */ |
| 111 | private static function storeInCache($type, $class) |
| 112 | { |
| 113 | self::$cache[$type] = $class; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set new strategies and clear the cache. |
| 118 | * |
| 119 | * @param array $strategies string array of fully qualified class name to a DiscoveryStrategy |
| 120 | */ |
| 121 | public static function setStrategies(array $strategies) |
| 122 | { |
| 123 | self::$strategies = $strategies; |
| 124 | self::clearCache(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Append a strategy at the end of the strategy queue. |
| 129 | * |
| 130 | * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
| 131 | */ |
| 132 | public static function appendStrategy($strategy) |
| 133 | { |
| 134 | self::$strategies[] = $strategy; |
| 135 | self::clearCache(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Prepend a strategy at the beginning of the strategy queue. |
| 140 | * |
| 141 | * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
| 142 | */ |
| 143 | public static function prependStrategy($strategy) |
| 144 | { |
| 145 | array_unshift(self::$strategies, $strategy); |
| 146 | self::clearCache(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Clear the cache. |
| 151 | */ |
| 152 | public static function clearCache() |
| 153 | { |
| 154 | self::$cache = []; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Evaluates conditions to boolean. |
| 159 | * |
| 160 | * @param mixed $condition |
| 161 | * |
| 162 | * @return bool |
| 163 | */ |
| 164 | protected static function evaluateCondition($condition) |
| 165 | { |
| 166 | if (is_string($condition)) { |
| 167 | // Should be extended for functions, extensions??? |
| 168 | return self::safeClassExists($condition); |
| 169 | } |
| 170 | if (is_callable($condition)) { |
| 171 | return (bool) $condition(); |
| 172 | } |
| 173 | if (is_bool($condition)) { |
| 174 | return $condition; |
| 175 | } |
| 176 | if (is_array($condition)) { |
| 177 | foreach ($condition as $c) { |
| 178 | if (false === static::evaluateCondition($c)) { |
| 179 | // Immediately stop execution if the condition is false |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get an instance of the $class. |
| 192 | * |
| 193 | * @param string|\Closure $class A FQCN of a class or a closure that instantiate the class. |
| 194 | * |
| 195 | * @return object |
| 196 | * |
| 197 | * @throws ClassInstantiationFailedException |
| 198 | */ |
| 199 | protected static function instantiateClass($class) |
| 200 | { |
| 201 | try { |
| 202 | if (is_string($class)) { |
| 203 | return new $class(); |
| 204 | } |
| 205 | |
| 206 | if (is_callable($class)) { |
| 207 | return $class(); |
| 208 | } |
| 209 | } catch (\Exception $e) { |
| 210 | throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e); |
| 211 | } |
| 212 | |
| 213 | throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string'); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * We want to do a "safe" version of PHP's "class_exists" because Magento has a bug |
| 218 | * (or they call it a "feature"). Magento is throwing an exception if you do class_exists() |
| 219 | * on a class that ends with "Factory" and if that file does not exits. |
| 220 | * |
| 221 | * This function will catch all potential exceptions and make sure it returns a boolean. |
| 222 | * |
| 223 | * @param string $class |
| 224 | * @param bool $autoload |
| 225 | * |
| 226 | * @return bool |
| 227 | */ |
| 228 | public static function safeClassExists($class) |
| 229 | { |
| 230 | try { |
| 231 | return class_exists($class); |
| 232 | } catch (\Exception $e) { |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 |