1 | <?php |
||
15 | abstract class ClassDiscovery |
||
16 | { |
||
17 | /** |
||
18 | * A list of strategies to find classes. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | public static $strategies = [ |
||
23 | Strategy\Puli::class, |
||
24 | Strategy\HttpClients::class, |
||
25 | Strategy\GuzzleFactory::class, |
||
26 | Strategy\DiactorosFactory::class, |
||
27 | ]; |
||
28 | 10 | ||
29 | /** |
||
30 | 10 | * Discovery cache to make the second time we use discovery faster. |
|
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private static $cache = []; |
||
35 | |||
36 | /** |
||
37 | * Finds a class. |
||
38 | * |
||
39 | * @param string $type |
||
40 | * |
||
41 | * @return string |
||
42 | * |
||
43 | * @throws DiscoveryFailedException |
||
44 | 10 | */ |
|
45 | protected static function findOneByType($type) |
||
46 | { |
||
47 | // Look in the cache |
||
48 | if (null !== ($class = self::getFromCache($type))) { |
||
49 | return $class; |
||
50 | } |
||
51 | |||
52 | 21 | $exceptions = []; |
|
53 | foreach (self::$strategies as $strategy) { |
||
54 | 21 | try { |
|
55 | $candidates = call_user_func($strategy.'::getCandidates', $type); |
||
56 | } catch (StrategyUnavailableException $e) { |
||
57 | $exceptions[] = $e; |
||
58 | 21 | continue; |
|
59 | 21 | } |
|
60 | 21 | ||
61 | foreach ($candidates as $candidate) { |
||
62 | if (isset($candidate['condition'])) { |
||
63 | if (!self::evaluateCondition($candidate['condition'])) { |
||
64 | continue; |
||
65 | 21 | } |
|
66 | } |
||
67 | 21 | ||
68 | 21 | // save the result for later use |
|
69 | 21 | self::storeInCache($type, $candidate); |
|
70 | |||
71 | return $candidate['class']; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | throw new DiscoveryFailedException('Could not find resource using any discovery strategy', $exceptions); |
||
76 | 9 | } |
|
77 | |||
78 | 9 | /** |
|
79 | 9 | * Get a value from cache. |
|
80 | 9 | * |
|
81 | * @param string $type |
||
82 | 9 | * |
|
83 | 9 | * @return string|null |
|
84 | */ |
||
85 | 9 | private static function getFromCache($type) |
|
86 | { |
||
87 | if (!isset(self::$cache[$type])) { |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | $candidate = self::$cache[$type]; |
||
92 | if (!self::evaluateCondition($candidate['condition'])) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | return $candidate['class']; |
||
97 | 8 | } |
|
98 | |||
99 | 8 | /** |
|
100 | * Store a value in cache. |
||
101 | 8 | * |
|
102 | 7 | * @param string $type |
|
103 | 1 | * @param string $class |
|
104 | */ |
||
105 | 1 | private static function storeInCache($type, $class) |
|
109 | |||
110 | /** |
||
111 | 7 | * Clear the cache. |
|
112 | 1 | */ |
|
113 | public static function clearCache() |
||
117 | |||
118 | /** |
||
119 | * Evaulates conditions to boolean. |
||
120 | * |
||
121 | * @param mixed $condition |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | protected static function evaluateCondition($condition) |
||
147 | } |
||
148 |