@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * Example: the target host name can not be resolved or the connection failed. |
13 | 13 | */ |
14 | -interface NetworkExceptionInterface extends ClientExceptionInterface |
|
15 | -{ |
|
14 | +interface NetworkExceptionInterface extends ClientExceptionInterface { |
|
16 | 15 | /** |
17 | 16 | * Returns the request. |
18 | 17 | * |
@@ -12,12 +12,12 @@ |
||
12 | 12 | */ |
13 | 13 | interface NetworkExceptionInterface extends ClientExceptionInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * Returns the request. |
|
17 | - * |
|
18 | - * The request object MAY be a different object from the one passed to ClientInterface::sendRequest() |
|
19 | - * |
|
20 | - * @return RequestInterface |
|
21 | - */ |
|
22 | - public function getRequest() : RequestInterface; |
|
15 | + /** |
|
16 | + * Returns the request. |
|
17 | + * |
|
18 | + * The request object MAY be a different object from the one passed to ClientInterface::sendRequest() |
|
19 | + * |
|
20 | + * @return RequestInterface |
|
21 | + */ |
|
22 | + public function getRequest() : RequestInterface; |
|
23 | 23 | } |
@@ -12,8 +12,7 @@ |
||
12 | 12 | * |
13 | 13 | * @deprecated This will be removed in 2.0. Consider using Psr17FactoryDiscovery. |
14 | 14 | */ |
15 | -final class UriFactoryDiscovery extends ClassDiscovery |
|
16 | -{ |
|
15 | +final class UriFactoryDiscovery extends ClassDiscovery { |
|
17 | 16 | /** |
18 | 17 | * Finds a URI Factory. |
19 | 18 | * |
@@ -13,20 +13,20 @@ |
||
13 | 13 | */ |
14 | 14 | final class UriFactoryDiscovery extends ClassDiscovery |
15 | 15 | { |
16 | - /** |
|
17 | - * Finds a URI Factory. |
|
18 | - * |
|
19 | - * @return UriFactory |
|
20 | - * |
|
21 | - * @throws Exception\NotFoundException |
|
22 | - */ |
|
23 | - public static function find() |
|
24 | - { |
|
25 | - try { |
|
26 | - $uriFactory = static::findOneByType(UriFactory::class); |
|
27 | - } catch (DiscoveryFailedException $e) { |
|
28 | - throw new NotFoundException('No uri factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation.', 0, $e); |
|
29 | - } |
|
30 | - return static::instantiateClass($uriFactory); |
|
31 | - } |
|
16 | + /** |
|
17 | + * Finds a URI Factory. |
|
18 | + * |
|
19 | + * @return UriFactory |
|
20 | + * |
|
21 | + * @throws Exception\NotFoundException |
|
22 | + */ |
|
23 | + public static function find() |
|
24 | + { |
|
25 | + try { |
|
26 | + $uriFactory = static::findOneByType(UriFactory::class); |
|
27 | + } catch (DiscoveryFailedException $e) { |
|
28 | + throw new NotFoundException('No uri factories found. To use Guzzle, Diactoros or Slim Framework factories install php-http/message and the chosen message implementation.', 0, $e); |
|
29 | + } |
|
30 | + return static::instantiateClass($uriFactory); |
|
31 | + } |
|
32 | 32 | } |
@@ -10,8 +10,7 @@ |
||
10 | 10 | * |
11 | 11 | * @author Joel Wurtz <[email protected]> |
12 | 12 | */ |
13 | -final class HttpAsyncClientDiscovery extends ClassDiscovery |
|
14 | -{ |
|
13 | +final class HttpAsyncClientDiscovery extends ClassDiscovery { |
|
15 | 14 | /** |
16 | 15 | * Finds an HTTP Async Client. |
17 | 16 | * |
@@ -11,20 +11,20 @@ |
||
11 | 11 | */ |
12 | 12 | final class HttpAsyncClientDiscovery extends ClassDiscovery |
13 | 13 | { |
14 | - /** |
|
15 | - * Finds an HTTP Async Client. |
|
16 | - * |
|
17 | - * @return HttpAsyncClient |
|
18 | - * |
|
19 | - * @throws Exception\NotFoundException |
|
20 | - */ |
|
21 | - public static function find() |
|
22 | - { |
|
23 | - try { |
|
24 | - $asyncClient = static::findOneByType(HttpAsyncClient::class); |
|
25 | - } catch (DiscoveryFailedException $e) { |
|
26 | - throw new NotFoundException('No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".', 0, $e); |
|
27 | - } |
|
28 | - return static::instantiateClass($asyncClient); |
|
29 | - } |
|
14 | + /** |
|
15 | + * Finds an HTTP Async Client. |
|
16 | + * |
|
17 | + * @return HttpAsyncClient |
|
18 | + * |
|
19 | + * @throws Exception\NotFoundException |
|
20 | + */ |
|
21 | + public static function find() |
|
22 | + { |
|
23 | + try { |
|
24 | + $asyncClient = static::findOneByType(HttpAsyncClient::class); |
|
25 | + } catch (DiscoveryFailedException $e) { |
|
26 | + throw new NotFoundException('No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".', 0, $e); |
|
27 | + } |
|
28 | + return static::instantiateClass($asyncClient); |
|
29 | + } |
|
30 | 30 | } |
@@ -15,8 +15,7 @@ |
||
15 | 15 | * @author Márk Sági-Kazár <[email protected]> |
16 | 16 | * @author Tobias Nyholm <[email protected]> |
17 | 17 | */ |
18 | -abstract class ClassDiscovery |
|
19 | -{ |
|
18 | +abstract class ClassDiscovery { |
|
20 | 19 | /** |
21 | 20 | * A list of strategies to find classes. |
22 | 21 | * |
@@ -16,204 +16,204 @@ |
||
16 | 16 | */ |
17 | 17 | abstract class ClassDiscovery |
18 | 18 | { |
19 | - /** |
|
20 | - * A list of strategies to find classes. |
|
21 | - * |
|
22 | - * @var DiscoveryStrategy[] |
|
23 | - */ |
|
24 | - private static $strategies = [Strategy\GeneratedDiscoveryStrategy::class, Strategy\CommonClassesStrategy::class, Strategy\CommonPsr17ClassesStrategy::class, Strategy\PuliBetaStrategy::class]; |
|
25 | - private static $deprecatedStrategies = [Strategy\PuliBetaStrategy::class => \true]; |
|
26 | - /** |
|
27 | - * Discovery cache to make the second time we use discovery faster. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - private static $cache = []; |
|
32 | - /** |
|
33 | - * Finds a class. |
|
34 | - * |
|
35 | - * @param string $type |
|
36 | - * |
|
37 | - * @return string|\Closure |
|
38 | - * |
|
39 | - * @throws DiscoveryFailedException |
|
40 | - */ |
|
41 | - protected static function findOneByType($type) |
|
42 | - { |
|
43 | - // Look in the cache |
|
44 | - if (null !== ($class = self::getFromCache($type))) { |
|
45 | - return $class; |
|
46 | - } |
|
47 | - static $skipStrategy; |
|
48 | - $skipStrategy ?? ($skipStrategy = self::safeClassExists(Strategy\GeneratedDiscoveryStrategy::class) ? \false : Strategy\GeneratedDiscoveryStrategy::class); |
|
49 | - $exceptions = []; |
|
50 | - foreach (self::$strategies as $strategy) { |
|
51 | - if ($skipStrategy === $strategy) { |
|
52 | - continue; |
|
53 | - } |
|
54 | - try { |
|
55 | - $candidates = $strategy::getCandidates($type); |
|
56 | - } catch (StrategyUnavailableException $e) { |
|
57 | - if (!isset(self::$deprecatedStrategies[$strategy])) { |
|
58 | - $exceptions[] = $e; |
|
59 | - } |
|
60 | - continue; |
|
61 | - } |
|
62 | - foreach ($candidates as $candidate) { |
|
63 | - if (isset($candidate['condition'])) { |
|
64 | - if (!self::evaluateCondition($candidate['condition'])) { |
|
65 | - continue; |
|
66 | - } |
|
67 | - } |
|
68 | - // save the result for later use |
|
69 | - self::storeInCache($type, $candidate); |
|
70 | - return $candidate['class']; |
|
71 | - } |
|
72 | - $exceptions[] = new NoCandidateFoundException($strategy, $candidates); |
|
73 | - } |
|
74 | - throw DiscoveryFailedException::create($exceptions); |
|
75 | - } |
|
76 | - /** |
|
77 | - * Get a value from cache. |
|
78 | - * |
|
79 | - * @param string $type |
|
80 | - * |
|
81 | - * @return string|null |
|
82 | - */ |
|
83 | - private static function getFromCache($type) |
|
84 | - { |
|
85 | - if (!isset(self::$cache[$type])) { |
|
86 | - return; |
|
87 | - } |
|
88 | - $candidate = self::$cache[$type]; |
|
89 | - if (isset($candidate['condition'])) { |
|
90 | - if (!self::evaluateCondition($candidate['condition'])) { |
|
91 | - return; |
|
92 | - } |
|
93 | - } |
|
94 | - return $candidate['class']; |
|
95 | - } |
|
96 | - /** |
|
97 | - * Store a value in cache. |
|
98 | - * |
|
99 | - * @param string $type |
|
100 | - * @param string $class |
|
101 | - */ |
|
102 | - private static function storeInCache($type, $class) |
|
103 | - { |
|
104 | - self::$cache[$type] = $class; |
|
105 | - } |
|
106 | - /** |
|
107 | - * Set new strategies and clear the cache. |
|
108 | - * |
|
109 | - * @param string[] $strategies list of fully qualified class names that implement DiscoveryStrategy |
|
110 | - */ |
|
111 | - public static function setStrategies(array $strategies) |
|
112 | - { |
|
113 | - self::$strategies = $strategies; |
|
114 | - self::clearCache(); |
|
115 | - } |
|
116 | - /** |
|
117 | - * Returns the currently configured discovery strategies as fully qualified class names. |
|
118 | - * |
|
119 | - * @return string[] |
|
120 | - */ |
|
121 | - public static function getStrategies() : iterable |
|
122 | - { |
|
123 | - return self::$strategies; |
|
124 | - } |
|
125 | - /** |
|
126 | - * Append a strategy at the end of the strategy queue. |
|
127 | - * |
|
128 | - * @param string $strategy Fully qualified class name of a DiscoveryStrategy |
|
129 | - */ |
|
130 | - public static function appendStrategy($strategy) |
|
131 | - { |
|
132 | - self::$strategies[] = $strategy; |
|
133 | - self::clearCache(); |
|
134 | - } |
|
135 | - /** |
|
136 | - * Prepend a strategy at the beginning of the strategy queue. |
|
137 | - * |
|
138 | - * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
|
139 | - */ |
|
140 | - public static function prependStrategy($strategy) |
|
141 | - { |
|
142 | - \array_unshift(self::$strategies, $strategy); |
|
143 | - self::clearCache(); |
|
144 | - } |
|
145 | - public static function clearCache() |
|
146 | - { |
|
147 | - self::$cache = []; |
|
148 | - } |
|
149 | - /** |
|
150 | - * Evaluates conditions to boolean. |
|
151 | - * |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - protected static function evaluateCondition($condition) |
|
155 | - { |
|
156 | - if (\is_string($condition)) { |
|
157 | - // Should be extended for functions, extensions??? |
|
158 | - return self::safeClassExists($condition); |
|
159 | - } |
|
160 | - if (\is_callable($condition)) { |
|
161 | - return (bool) $condition(); |
|
162 | - } |
|
163 | - if (\is_bool($condition)) { |
|
164 | - return $condition; |
|
165 | - } |
|
166 | - if (\is_array($condition)) { |
|
167 | - foreach ($condition as $c) { |
|
168 | - if (\false === static::evaluateCondition($c)) { |
|
169 | - // Immediately stop execution if the condition is false |
|
170 | - return \false; |
|
171 | - } |
|
172 | - } |
|
173 | - return \true; |
|
174 | - } |
|
175 | - return \false; |
|
176 | - } |
|
177 | - /** |
|
178 | - * Get an instance of the $class. |
|
179 | - * |
|
180 | - * @param string|\Closure $class a FQCN of a class or a closure that instantiate the class |
|
181 | - * |
|
182 | - * @return object |
|
183 | - * |
|
184 | - * @throws ClassInstantiationFailedException |
|
185 | - */ |
|
186 | - protected static function instantiateClass($class) |
|
187 | - { |
|
188 | - try { |
|
189 | - if (\is_string($class)) { |
|
190 | - return new $class(); |
|
191 | - } |
|
192 | - if (\is_callable($class)) { |
|
193 | - return $class(); |
|
194 | - } |
|
195 | - } catch (\Exception $e) { |
|
196 | - throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e); |
|
197 | - } |
|
198 | - throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string'); |
|
199 | - } |
|
200 | - /** |
|
201 | - * We need a "safe" version of PHP's "class_exists" because Magento has a bug |
|
202 | - * (or they call it a "feature"). Magento is throwing an exception if you do class_exists() |
|
203 | - * on a class that ends with "Factory" and if that file does not exits. |
|
204 | - * |
|
205 | - * This function catches all potential exceptions and makes sure to always return a boolean. |
|
206 | - * |
|
207 | - * @param string $class |
|
208 | - * |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - public static function safeClassExists($class) |
|
212 | - { |
|
213 | - try { |
|
214 | - return \class_exists($class) || \interface_exists($class); |
|
215 | - } catch (\Exception $e) { |
|
216 | - return \false; |
|
217 | - } |
|
218 | - } |
|
19 | + /** |
|
20 | + * A list of strategies to find classes. |
|
21 | + * |
|
22 | + * @var DiscoveryStrategy[] |
|
23 | + */ |
|
24 | + private static $strategies = [Strategy\GeneratedDiscoveryStrategy::class, Strategy\CommonClassesStrategy::class, Strategy\CommonPsr17ClassesStrategy::class, Strategy\PuliBetaStrategy::class]; |
|
25 | + private static $deprecatedStrategies = [Strategy\PuliBetaStrategy::class => \true]; |
|
26 | + /** |
|
27 | + * Discovery cache to make the second time we use discovery faster. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + private static $cache = []; |
|
32 | + /** |
|
33 | + * Finds a class. |
|
34 | + * |
|
35 | + * @param string $type |
|
36 | + * |
|
37 | + * @return string|\Closure |
|
38 | + * |
|
39 | + * @throws DiscoveryFailedException |
|
40 | + */ |
|
41 | + protected static function findOneByType($type) |
|
42 | + { |
|
43 | + // Look in the cache |
|
44 | + if (null !== ($class = self::getFromCache($type))) { |
|
45 | + return $class; |
|
46 | + } |
|
47 | + static $skipStrategy; |
|
48 | + $skipStrategy ?? ($skipStrategy = self::safeClassExists(Strategy\GeneratedDiscoveryStrategy::class) ? \false : Strategy\GeneratedDiscoveryStrategy::class); |
|
49 | + $exceptions = []; |
|
50 | + foreach (self::$strategies as $strategy) { |
|
51 | + if ($skipStrategy === $strategy) { |
|
52 | + continue; |
|
53 | + } |
|
54 | + try { |
|
55 | + $candidates = $strategy::getCandidates($type); |
|
56 | + } catch (StrategyUnavailableException $e) { |
|
57 | + if (!isset(self::$deprecatedStrategies[$strategy])) { |
|
58 | + $exceptions[] = $e; |
|
59 | + } |
|
60 | + continue; |
|
61 | + } |
|
62 | + foreach ($candidates as $candidate) { |
|
63 | + if (isset($candidate['condition'])) { |
|
64 | + if (!self::evaluateCondition($candidate['condition'])) { |
|
65 | + continue; |
|
66 | + } |
|
67 | + } |
|
68 | + // save the result for later use |
|
69 | + self::storeInCache($type, $candidate); |
|
70 | + return $candidate['class']; |
|
71 | + } |
|
72 | + $exceptions[] = new NoCandidateFoundException($strategy, $candidates); |
|
73 | + } |
|
74 | + throw DiscoveryFailedException::create($exceptions); |
|
75 | + } |
|
76 | + /** |
|
77 | + * Get a value from cache. |
|
78 | + * |
|
79 | + * @param string $type |
|
80 | + * |
|
81 | + * @return string|null |
|
82 | + */ |
|
83 | + private static function getFromCache($type) |
|
84 | + { |
|
85 | + if (!isset(self::$cache[$type])) { |
|
86 | + return; |
|
87 | + } |
|
88 | + $candidate = self::$cache[$type]; |
|
89 | + if (isset($candidate['condition'])) { |
|
90 | + if (!self::evaluateCondition($candidate['condition'])) { |
|
91 | + return; |
|
92 | + } |
|
93 | + } |
|
94 | + return $candidate['class']; |
|
95 | + } |
|
96 | + /** |
|
97 | + * Store a value in cache. |
|
98 | + * |
|
99 | + * @param string $type |
|
100 | + * @param string $class |
|
101 | + */ |
|
102 | + private static function storeInCache($type, $class) |
|
103 | + { |
|
104 | + self::$cache[$type] = $class; |
|
105 | + } |
|
106 | + /** |
|
107 | + * Set new strategies and clear the cache. |
|
108 | + * |
|
109 | + * @param string[] $strategies list of fully qualified class names that implement DiscoveryStrategy |
|
110 | + */ |
|
111 | + public static function setStrategies(array $strategies) |
|
112 | + { |
|
113 | + self::$strategies = $strategies; |
|
114 | + self::clearCache(); |
|
115 | + } |
|
116 | + /** |
|
117 | + * Returns the currently configured discovery strategies as fully qualified class names. |
|
118 | + * |
|
119 | + * @return string[] |
|
120 | + */ |
|
121 | + public static function getStrategies() : iterable |
|
122 | + { |
|
123 | + return self::$strategies; |
|
124 | + } |
|
125 | + /** |
|
126 | + * Append a strategy at the end of the strategy queue. |
|
127 | + * |
|
128 | + * @param string $strategy Fully qualified class name of a DiscoveryStrategy |
|
129 | + */ |
|
130 | + public static function appendStrategy($strategy) |
|
131 | + { |
|
132 | + self::$strategies[] = $strategy; |
|
133 | + self::clearCache(); |
|
134 | + } |
|
135 | + /** |
|
136 | + * Prepend a strategy at the beginning of the strategy queue. |
|
137 | + * |
|
138 | + * @param string $strategy Fully qualified class name to a DiscoveryStrategy |
|
139 | + */ |
|
140 | + public static function prependStrategy($strategy) |
|
141 | + { |
|
142 | + \array_unshift(self::$strategies, $strategy); |
|
143 | + self::clearCache(); |
|
144 | + } |
|
145 | + public static function clearCache() |
|
146 | + { |
|
147 | + self::$cache = []; |
|
148 | + } |
|
149 | + /** |
|
150 | + * Evaluates conditions to boolean. |
|
151 | + * |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + protected static function evaluateCondition($condition) |
|
155 | + { |
|
156 | + if (\is_string($condition)) { |
|
157 | + // Should be extended for functions, extensions??? |
|
158 | + return self::safeClassExists($condition); |
|
159 | + } |
|
160 | + if (\is_callable($condition)) { |
|
161 | + return (bool) $condition(); |
|
162 | + } |
|
163 | + if (\is_bool($condition)) { |
|
164 | + return $condition; |
|
165 | + } |
|
166 | + if (\is_array($condition)) { |
|
167 | + foreach ($condition as $c) { |
|
168 | + if (\false === static::evaluateCondition($c)) { |
|
169 | + // Immediately stop execution if the condition is false |
|
170 | + return \false; |
|
171 | + } |
|
172 | + } |
|
173 | + return \true; |
|
174 | + } |
|
175 | + return \false; |
|
176 | + } |
|
177 | + /** |
|
178 | + * Get an instance of the $class. |
|
179 | + * |
|
180 | + * @param string|\Closure $class a FQCN of a class or a closure that instantiate the class |
|
181 | + * |
|
182 | + * @return object |
|
183 | + * |
|
184 | + * @throws ClassInstantiationFailedException |
|
185 | + */ |
|
186 | + protected static function instantiateClass($class) |
|
187 | + { |
|
188 | + try { |
|
189 | + if (\is_string($class)) { |
|
190 | + return new $class(); |
|
191 | + } |
|
192 | + if (\is_callable($class)) { |
|
193 | + return $class(); |
|
194 | + } |
|
195 | + } catch (\Exception $e) { |
|
196 | + throw new ClassInstantiationFailedException('Unexpected exception when instantiating class.', 0, $e); |
|
197 | + } |
|
198 | + throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string'); |
|
199 | + } |
|
200 | + /** |
|
201 | + * We need a "safe" version of PHP's "class_exists" because Magento has a bug |
|
202 | + * (or they call it a "feature"). Magento is throwing an exception if you do class_exists() |
|
203 | + * on a class that ends with "Factory" and if that file does not exits. |
|
204 | + * |
|
205 | + * This function catches all potential exceptions and makes sure to always return a boolean. |
|
206 | + * |
|
207 | + * @param string $class |
|
208 | + * |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + public static function safeClassExists($class) |
|
212 | + { |
|
213 | + try { |
|
214 | + return \class_exists($class) || \interface_exists($class); |
|
215 | + } catch (\Exception $e) { |
|
216 | + return \false; |
|
217 | + } |
|
218 | + } |
|
219 | 219 | } |
@@ -158,7 +158,7 @@ |
||
158 | 158 | return self::safeClassExists($condition); |
159 | 159 | } |
160 | 160 | if (\is_callable($condition)) { |
161 | - return (bool) $condition(); |
|
161 | + return (bool)$condition(); |
|
162 | 162 | } |
163 | 163 | if (\is_bool($condition)) { |
164 | 164 | return $condition; |
@@ -12,8 +12,7 @@ |
||
12 | 12 | * |
13 | 13 | * @deprecated This will be removed in 2.0. Consider using Psr18ClientDiscovery. |
14 | 14 | */ |
15 | -final class HttpClientDiscovery extends ClassDiscovery |
|
16 | -{ |
|
15 | +final class HttpClientDiscovery extends ClassDiscovery { |
|
17 | 16 | /** |
18 | 17 | * Finds an HTTP Client. |
19 | 18 | * |
@@ -13,20 +13,20 @@ |
||
13 | 13 | */ |
14 | 14 | final class HttpClientDiscovery extends ClassDiscovery |
15 | 15 | { |
16 | - /** |
|
17 | - * Finds an HTTP Client. |
|
18 | - * |
|
19 | - * @return HttpClient |
|
20 | - * |
|
21 | - * @throws Exception\NotFoundException |
|
22 | - */ |
|
23 | - public static function find() |
|
24 | - { |
|
25 | - try { |
|
26 | - $client = static::findOneByType(HttpClient::class); |
|
27 | - } catch (DiscoveryFailedException $e) { |
|
28 | - throw new NotFoundException('No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".', 0, $e); |
|
29 | - } |
|
30 | - return static::instantiateClass($client); |
|
31 | - } |
|
16 | + /** |
|
17 | + * Finds an HTTP Client. |
|
18 | + * |
|
19 | + * @return HttpClient |
|
20 | + * |
|
21 | + * @throws Exception\NotFoundException |
|
22 | + */ |
|
23 | + public static function find() |
|
24 | + { |
|
25 | + try { |
|
26 | + $client = static::findOneByType(HttpClient::class); |
|
27 | + } catch (DiscoveryFailedException $e) { |
|
28 | + throw new NotFoundException('No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".', 0, $e); |
|
29 | + } |
|
30 | + return static::instantiateClass($client); |
|
31 | + } |
|
32 | 32 | } |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @author Tobias Nyholm <[email protected]> |
13 | 13 | */ |
14 | -final class Psr18ClientDiscovery extends ClassDiscovery |
|
15 | -{ |
|
14 | +final class Psr18ClientDiscovery extends ClassDiscovery { |
|
16 | 15 | /** |
17 | 16 | * Finds a PSR-18 HTTP Client. |
18 | 17 | * |
@@ -12,20 +12,20 @@ |
||
12 | 12 | */ |
13 | 13 | final class Psr18ClientDiscovery extends ClassDiscovery |
14 | 14 | { |
15 | - /** |
|
16 | - * Finds a PSR-18 HTTP Client. |
|
17 | - * |
|
18 | - * @return ClientInterface |
|
19 | - * |
|
20 | - * @throws RealNotFoundException |
|
21 | - */ |
|
22 | - public static function find() |
|
23 | - { |
|
24 | - try { |
|
25 | - $client = static::findOneByType(ClientInterface::class); |
|
26 | - } catch (DiscoveryFailedException $e) { |
|
27 | - throw new RealNotFoundException('No PSR-18 clients found. Make sure to install a package providing "psr/http-client-implementation". Example: "php-http/guzzle7-adapter".', 0, $e); |
|
28 | - } |
|
29 | - return static::instantiateClass($client); |
|
30 | - } |
|
15 | + /** |
|
16 | + * Finds a PSR-18 HTTP Client. |
|
17 | + * |
|
18 | + * @return ClientInterface |
|
19 | + * |
|
20 | + * @throws RealNotFoundException |
|
21 | + */ |
|
22 | + public static function find() |
|
23 | + { |
|
24 | + try { |
|
25 | + $client = static::findOneByType(ClientInterface::class); |
|
26 | + } catch (DiscoveryFailedException $e) { |
|
27 | + throw new RealNotFoundException('No PSR-18 clients found. Make sure to install a package providing "psr/http-client-implementation". Example: "php-http/guzzle7-adapter".', 0, $e); |
|
28 | + } |
|
29 | + return static::instantiateClass($client); |
|
30 | + } |
|
31 | 31 | } |
@@ -7,6 +7,5 @@ |
||
7 | 7 | * |
8 | 8 | * @author Tobias Nyholm <[email protected]> |
9 | 9 | */ |
10 | -final class PuliUnavailableException extends StrategyUnavailableException |
|
11 | -{ |
|
10 | +final class PuliUnavailableException extends StrategyUnavailableException { |
|
12 | 11 | } |
@@ -46,8 +46,7 @@ |
||
46 | 46 | * |
47 | 47 | * Don't miss updating src/Composer/Plugin.php when adding a new supported class. |
48 | 48 | */ |
49 | -final class CommonClassesStrategy implements DiscoveryStrategy |
|
50 | -{ |
|
49 | +final class CommonClassesStrategy implements DiscoveryStrategy { |
|
51 | 50 | /** |
52 | 51 | * @var array |
53 | 52 | */ |
@@ -47,70 +47,70 @@ |
||
47 | 47 | */ |
48 | 48 | final class CommonClassesStrategy implements DiscoveryStrategy |
49 | 49 | { |
50 | - /** |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - private static $classes = [MessageFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleMessageFactory::class, 'condition' => [GuzzleRequest::class, GuzzleMessageFactory::class]], ['class' => DiactorosMessageFactory::class, 'condition' => [DiactorosRequest::class, DiactorosMessageFactory::class]], ['class' => SlimMessageFactory::class, 'condition' => [SlimRequest::class, SlimMessageFactory::class]]], StreamFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleStreamFactory::class, 'condition' => [GuzzleRequest::class, GuzzleStreamFactory::class]], ['class' => DiactorosStreamFactory::class, 'condition' => [DiactorosRequest::class, DiactorosStreamFactory::class]], ['class' => SlimStreamFactory::class, 'condition' => [SlimRequest::class, SlimStreamFactory::class]]], UriFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleUriFactory::class, 'condition' => [GuzzleRequest::class, GuzzleUriFactory::class]], ['class' => DiactorosUriFactory::class, 'condition' => [DiactorosRequest::class, DiactorosUriFactory::class]], ['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]]], HttpAsyncClient::class => [['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, [self::class, 'isPsr17FactoryInstalled']]], ['class' => Guzzle7::class, 'condition' => Guzzle7::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Curl::class, 'condition' => Curl::class], ['class' => React::class, 'condition' => React::class]], HttpClient::class => [['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, [self::class, 'isPsr17FactoryInstalled'], [self::class, 'isSymfonyImplementingHttpClient']]], ['class' => Guzzle7::class, 'condition' => Guzzle7::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Guzzle5::class, 'condition' => Guzzle5::class], ['class' => Curl::class, 'condition' => Curl::class], ['class' => Socket::class, 'condition' => Socket::class], ['class' => Buzz::class, 'condition' => Buzz::class], ['class' => React::class, 'condition' => React::class], ['class' => Cake::class, 'condition' => Cake::class], ['class' => Artax::class, 'condition' => Artax::class], ['class' => [self::class, 'buzzInstantiate'], 'condition' => [\OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents::class, \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Message\ResponseBuilder::class]]], Psr18Client::class => [['class' => [self::class, 'symfonyPsr18Instantiate'], 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class]], ['class' => GuzzleHttp::class, 'condition' => [self::class, 'isGuzzleImplementingPsr18']], ['class' => [self::class, 'buzzInstantiate'], 'condition' => [\OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents::class, \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Message\ResponseBuilder::class]]]]; |
|
54 | - public static function getCandidates($type) |
|
55 | - { |
|
56 | - if (Psr18Client::class === $type) { |
|
57 | - return self::getPsr18Candidates(); |
|
58 | - } |
|
59 | - return self::$classes[$type] ?? []; |
|
60 | - } |
|
61 | - /** |
|
62 | - * @return array The return value is always an array with zero or more elements. Each |
|
63 | - * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
64 | - */ |
|
65 | - private static function getPsr18Candidates() |
|
66 | - { |
|
67 | - $candidates = self::$classes[Psr18Client::class]; |
|
68 | - // HTTPlug 2.0 clients implements PSR18Client too. |
|
69 | - foreach (self::$classes[HttpClient::class] as $c) { |
|
70 | - if (!\is_string($c['class'])) { |
|
71 | - continue; |
|
72 | - } |
|
73 | - try { |
|
74 | - if (ClassDiscovery::safeClassExists($c['class']) && \is_subclass_of($c['class'], Psr18Client::class)) { |
|
75 | - $candidates[] = $c; |
|
76 | - } |
|
77 | - } catch (\Throwable $e) { |
|
78 | - \trigger_error(\sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', \get_class($e), $e->getMessage()), \E_USER_WARNING); |
|
79 | - } |
|
80 | - } |
|
81 | - return $candidates; |
|
82 | - } |
|
83 | - public static function buzzInstantiate() |
|
84 | - { |
|
85 | - return new \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory()); |
|
86 | - } |
|
87 | - public static function symfonyPsr18Instantiate() |
|
88 | - { |
|
89 | - return new SymfonyPsr18(null, Psr17FactoryDiscovery::findResponseFactory(), Psr17FactoryDiscovery::findStreamFactory()); |
|
90 | - } |
|
91 | - public static function isGuzzleImplementingPsr18() |
|
92 | - { |
|
93 | - return \defined('GuzzleHttp\\ClientInterface::MAJOR_VERSION'); |
|
94 | - } |
|
95 | - public static function isSymfonyImplementingHttpClient() |
|
96 | - { |
|
97 | - return \is_subclass_of(SymfonyHttplug::class, HttpClient::class); |
|
98 | - } |
|
99 | - /** |
|
100 | - * Can be used as a condition. |
|
101 | - * |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public static function isPsr17FactoryInstalled() |
|
105 | - { |
|
106 | - try { |
|
107 | - Psr17FactoryDiscovery::findResponseFactory(); |
|
108 | - } catch (NotFoundException $e) { |
|
109 | - return \false; |
|
110 | - } catch (\Throwable $e) { |
|
111 | - \trigger_error(\sprintf('Got exception "%s (%s)" while checking if a PSR-17 ResponseFactory is available', \get_class($e), $e->getMessage()), \E_USER_WARNING); |
|
112 | - return \false; |
|
113 | - } |
|
114 | - return \true; |
|
115 | - } |
|
50 | + /** |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + private static $classes = [MessageFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleMessageFactory::class, 'condition' => [GuzzleRequest::class, GuzzleMessageFactory::class]], ['class' => DiactorosMessageFactory::class, 'condition' => [DiactorosRequest::class, DiactorosMessageFactory::class]], ['class' => SlimMessageFactory::class, 'condition' => [SlimRequest::class, SlimMessageFactory::class]]], StreamFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleStreamFactory::class, 'condition' => [GuzzleRequest::class, GuzzleStreamFactory::class]], ['class' => DiactorosStreamFactory::class, 'condition' => [DiactorosRequest::class, DiactorosStreamFactory::class]], ['class' => SlimStreamFactory::class, 'condition' => [SlimRequest::class, SlimStreamFactory::class]]], UriFactory::class => [['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], ['class' => GuzzleUriFactory::class, 'condition' => [GuzzleRequest::class, GuzzleUriFactory::class]], ['class' => DiactorosUriFactory::class, 'condition' => [DiactorosRequest::class, DiactorosUriFactory::class]], ['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]]], HttpAsyncClient::class => [['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, [self::class, 'isPsr17FactoryInstalled']]], ['class' => Guzzle7::class, 'condition' => Guzzle7::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Curl::class, 'condition' => Curl::class], ['class' => React::class, 'condition' => React::class]], HttpClient::class => [['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, [self::class, 'isPsr17FactoryInstalled'], [self::class, 'isSymfonyImplementingHttpClient']]], ['class' => Guzzle7::class, 'condition' => Guzzle7::class], ['class' => Guzzle6::class, 'condition' => Guzzle6::class], ['class' => Guzzle5::class, 'condition' => Guzzle5::class], ['class' => Curl::class, 'condition' => Curl::class], ['class' => Socket::class, 'condition' => Socket::class], ['class' => Buzz::class, 'condition' => Buzz::class], ['class' => React::class, 'condition' => React::class], ['class' => Cake::class, 'condition' => Cake::class], ['class' => Artax::class, 'condition' => Artax::class], ['class' => [self::class, 'buzzInstantiate'], 'condition' => [\OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents::class, \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Message\ResponseBuilder::class]]], Psr18Client::class => [['class' => [self::class, 'symfonyPsr18Instantiate'], 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class]], ['class' => GuzzleHttp::class, 'condition' => [self::class, 'isGuzzleImplementingPsr18']], ['class' => [self::class, 'buzzInstantiate'], 'condition' => [\OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents::class, \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Message\ResponseBuilder::class]]]]; |
|
54 | + public static function getCandidates($type) |
|
55 | + { |
|
56 | + if (Psr18Client::class === $type) { |
|
57 | + return self::getPsr18Candidates(); |
|
58 | + } |
|
59 | + return self::$classes[$type] ?? []; |
|
60 | + } |
|
61 | + /** |
|
62 | + * @return array The return value is always an array with zero or more elements. Each |
|
63 | + * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
64 | + */ |
|
65 | + private static function getPsr18Candidates() |
|
66 | + { |
|
67 | + $candidates = self::$classes[Psr18Client::class]; |
|
68 | + // HTTPlug 2.0 clients implements PSR18Client too. |
|
69 | + foreach (self::$classes[HttpClient::class] as $c) { |
|
70 | + if (!\is_string($c['class'])) { |
|
71 | + continue; |
|
72 | + } |
|
73 | + try { |
|
74 | + if (ClassDiscovery::safeClassExists($c['class']) && \is_subclass_of($c['class'], Psr18Client::class)) { |
|
75 | + $candidates[] = $c; |
|
76 | + } |
|
77 | + } catch (\Throwable $e) { |
|
78 | + \trigger_error(\sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', \get_class($e), $e->getMessage()), \E_USER_WARNING); |
|
79 | + } |
|
80 | + } |
|
81 | + return $candidates; |
|
82 | + } |
|
83 | + public static function buzzInstantiate() |
|
84 | + { |
|
85 | + return new \OCA\FullTextSearch_Elasticsearch\Vendor\Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory()); |
|
86 | + } |
|
87 | + public static function symfonyPsr18Instantiate() |
|
88 | + { |
|
89 | + return new SymfonyPsr18(null, Psr17FactoryDiscovery::findResponseFactory(), Psr17FactoryDiscovery::findStreamFactory()); |
|
90 | + } |
|
91 | + public static function isGuzzleImplementingPsr18() |
|
92 | + { |
|
93 | + return \defined('GuzzleHttp\\ClientInterface::MAJOR_VERSION'); |
|
94 | + } |
|
95 | + public static function isSymfonyImplementingHttpClient() |
|
96 | + { |
|
97 | + return \is_subclass_of(SymfonyHttplug::class, HttpClient::class); |
|
98 | + } |
|
99 | + /** |
|
100 | + * Can be used as a condition. |
|
101 | + * |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public static function isPsr17FactoryInstalled() |
|
105 | + { |
|
106 | + try { |
|
107 | + Psr17FactoryDiscovery::findResponseFactory(); |
|
108 | + } catch (NotFoundException $e) { |
|
109 | + return \false; |
|
110 | + } catch (\Throwable $e) { |
|
111 | + \trigger_error(\sprintf('Got exception "%s (%s)" while checking if a PSR-17 ResponseFactory is available', \get_class($e), $e->getMessage()), \E_USER_WARNING); |
|
112 | + return \false; |
|
113 | + } |
|
114 | + return \true; |
|
115 | + } |
|
116 | 116 | } |
@@ -17,8 +17,7 @@ |
||
17 | 17 | * @author David de Boer <[email protected]> |
18 | 18 | * @author Márk Sági-Kazár <[email protected]> |
19 | 19 | */ |
20 | -class PuliBetaStrategy implements DiscoveryStrategy |
|
21 | -{ |
|
20 | +class PuliBetaStrategy implements DiscoveryStrategy { |
|
22 | 21 | /** |
23 | 22 | * @var GeneratedPuliFactory |
24 | 23 | */ |
@@ -18,60 +18,60 @@ |
||
18 | 18 | */ |
19 | 19 | class PuliBetaStrategy implements DiscoveryStrategy |
20 | 20 | { |
21 | - /** |
|
22 | - * @var GeneratedPuliFactory |
|
23 | - */ |
|
24 | - protected static $puliFactory; |
|
25 | - /** |
|
26 | - * @var Discovery |
|
27 | - */ |
|
28 | - protected static $puliDiscovery; |
|
29 | - /** |
|
30 | - * @return GeneratedPuliFactory |
|
31 | - * |
|
32 | - * @throws PuliUnavailableException |
|
33 | - */ |
|
34 | - private static function getPuliFactory() |
|
35 | - { |
|
36 | - if (null === self::$puliFactory) { |
|
37 | - if (!\defined('OCA\\FullTextSearch_Elasticsearch\\Vendor\\PULI_FACTORY_CLASS')) { |
|
38 | - throw new PuliUnavailableException('Puli Factory is not available'); |
|
39 | - } |
|
40 | - $puliFactoryClass = PULI_FACTORY_CLASS; |
|
41 | - if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
|
42 | - throw new PuliUnavailableException('Puli Factory class does not exist'); |
|
43 | - } |
|
44 | - self::$puliFactory = new $puliFactoryClass(); |
|
45 | - } |
|
46 | - return self::$puliFactory; |
|
47 | - } |
|
48 | - /** |
|
49 | - * Returns the Puli discovery layer. |
|
50 | - * |
|
51 | - * @return Discovery |
|
52 | - * |
|
53 | - * @throws PuliUnavailableException |
|
54 | - */ |
|
55 | - private static function getPuliDiscovery() |
|
56 | - { |
|
57 | - if (!isset(self::$puliDiscovery)) { |
|
58 | - $factory = self::getPuliFactory(); |
|
59 | - $repository = $factory->createRepository(); |
|
60 | - self::$puliDiscovery = $factory->createDiscovery($repository); |
|
61 | - } |
|
62 | - return self::$puliDiscovery; |
|
63 | - } |
|
64 | - public static function getCandidates($type) |
|
65 | - { |
|
66 | - $returnData = []; |
|
67 | - $bindings = self::getPuliDiscovery()->findBindings($type); |
|
68 | - foreach ($bindings as $binding) { |
|
69 | - $condition = \true; |
|
70 | - if ($binding->hasParameterValue('depends')) { |
|
71 | - $condition = $binding->getParameterValue('depends'); |
|
72 | - } |
|
73 | - $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
|
74 | - } |
|
75 | - return $returnData; |
|
76 | - } |
|
21 | + /** |
|
22 | + * @var GeneratedPuliFactory |
|
23 | + */ |
|
24 | + protected static $puliFactory; |
|
25 | + /** |
|
26 | + * @var Discovery |
|
27 | + */ |
|
28 | + protected static $puliDiscovery; |
|
29 | + /** |
|
30 | + * @return GeneratedPuliFactory |
|
31 | + * |
|
32 | + * @throws PuliUnavailableException |
|
33 | + */ |
|
34 | + private static function getPuliFactory() |
|
35 | + { |
|
36 | + if (null === self::$puliFactory) { |
|
37 | + if (!\defined('OCA\\FullTextSearch_Elasticsearch\\Vendor\\PULI_FACTORY_CLASS')) { |
|
38 | + throw new PuliUnavailableException('Puli Factory is not available'); |
|
39 | + } |
|
40 | + $puliFactoryClass = PULI_FACTORY_CLASS; |
|
41 | + if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
|
42 | + throw new PuliUnavailableException('Puli Factory class does not exist'); |
|
43 | + } |
|
44 | + self::$puliFactory = new $puliFactoryClass(); |
|
45 | + } |
|
46 | + return self::$puliFactory; |
|
47 | + } |
|
48 | + /** |
|
49 | + * Returns the Puli discovery layer. |
|
50 | + * |
|
51 | + * @return Discovery |
|
52 | + * |
|
53 | + * @throws PuliUnavailableException |
|
54 | + */ |
|
55 | + private static function getPuliDiscovery() |
|
56 | + { |
|
57 | + if (!isset(self::$puliDiscovery)) { |
|
58 | + $factory = self::getPuliFactory(); |
|
59 | + $repository = $factory->createRepository(); |
|
60 | + self::$puliDiscovery = $factory->createDiscovery($repository); |
|
61 | + } |
|
62 | + return self::$puliDiscovery; |
|
63 | + } |
|
64 | + public static function getCandidates($type) |
|
65 | + { |
|
66 | + $returnData = []; |
|
67 | + $bindings = self::getPuliDiscovery()->findBindings($type); |
|
68 | + foreach ($bindings as $binding) { |
|
69 | + $condition = \true; |
|
70 | + if ($binding->hasParameterValue('depends')) { |
|
71 | + $condition = $binding->getParameterValue('depends'); |
|
72 | + } |
|
73 | + $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
|
74 | + } |
|
75 | + return $returnData; |
|
76 | + } |
|
77 | 77 | } |