@@ -41,263 +41,263 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface |
| 43 | 43 | { |
| 44 | - private $requestFactory; |
|
| 45 | - private $responseFactory; |
|
| 46 | - private $serverRequestFactory; |
|
| 47 | - private $streamFactory; |
|
| 48 | - private $uploadedFileFactory; |
|
| 49 | - private $uriFactory; |
|
| 50 | - |
|
| 51 | - public function __construct( |
|
| 52 | - ?RequestFactoryInterface $requestFactory = null, |
|
| 53 | - ?ResponseFactoryInterface $responseFactory = null, |
|
| 54 | - ?ServerRequestFactoryInterface $serverRequestFactory = null, |
|
| 55 | - ?StreamFactoryInterface $streamFactory = null, |
|
| 56 | - ?UploadedFileFactoryInterface $uploadedFileFactory = null, |
|
| 57 | - ?UriFactoryInterface $uriFactory = null |
|
| 58 | - ) { |
|
| 59 | - $this->requestFactory = $requestFactory; |
|
| 60 | - $this->responseFactory = $responseFactory; |
|
| 61 | - $this->serverRequestFactory = $serverRequestFactory; |
|
| 62 | - $this->streamFactory = $streamFactory; |
|
| 63 | - $this->uploadedFileFactory = $uploadedFileFactory; |
|
| 64 | - $this->uriFactory = $uriFactory; |
|
| 65 | - |
|
| 66 | - $this->setFactory($requestFactory); |
|
| 67 | - $this->setFactory($responseFactory); |
|
| 68 | - $this->setFactory($serverRequestFactory); |
|
| 69 | - $this->setFactory($streamFactory); |
|
| 70 | - $this->setFactory($uploadedFileFactory); |
|
| 71 | - $this->setFactory($uriFactory); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param UriInterface|string $uri |
|
| 76 | - */ |
|
| 77 | - public function createRequest(string $method, $uri): RequestInterface |
|
| 78 | - { |
|
| 79 | - $factory = $this->requestFactory ?? $this->setFactory(Psr17FactoryDiscovery::findRequestFactory()); |
|
| 80 | - |
|
| 81 | - return $factory->createRequest(...\func_get_args()); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
| 85 | - { |
|
| 86 | - $factory = $this->responseFactory ?? $this->setFactory(Psr17FactoryDiscovery::findResponseFactory()); |
|
| 87 | - |
|
| 88 | - return $factory->createResponse(...\func_get_args()); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param UriInterface|string $uri |
|
| 93 | - */ |
|
| 94 | - public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
| 95 | - { |
|
| 96 | - $factory = $this->serverRequestFactory ?? $this->setFactory(Psr17FactoryDiscovery::findServerRequestFactory()); |
|
| 97 | - |
|
| 98 | - return $factory->createServerRequest(...\func_get_args()); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function createServerRequestFromGlobals(?array $server = null, ?array $get = null, ?array $post = null, ?array $cookie = null, ?array $files = null, ?StreamInterface $body = null): ServerRequestInterface |
|
| 102 | - { |
|
| 103 | - $server = $server ?? $_SERVER; |
|
| 104 | - $request = $this->createServerRequest($server['REQUEST_METHOD'] ?? 'GET', $this->createUriFromGlobals($server), $server); |
|
| 105 | - |
|
| 106 | - return $this->buildServerRequestFromGlobals($request, $server, $files ?? $_FILES) |
|
| 107 | - ->withQueryParams($get ?? $_GET) |
|
| 108 | - ->withParsedBody($post ?? $_POST) |
|
| 109 | - ->withCookieParams($cookie ?? $_COOKIE) |
|
| 110 | - ->withBody($body ?? $this->createStreamFromFile('php://input', 'r+')); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function createStream(string $content = ''): StreamInterface |
|
| 114 | - { |
|
| 115 | - $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 116 | - |
|
| 117 | - return $factory->createStream($content); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
| 121 | - { |
|
| 122 | - $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 123 | - |
|
| 124 | - return $factory->createStreamFromFile($filename, $mode); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param resource $resource |
|
| 129 | - */ |
|
| 130 | - public function createStreamFromResource($resource): StreamInterface |
|
| 131 | - { |
|
| 132 | - $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 133 | - |
|
| 134 | - return $factory->createStreamFromResource($resource); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function createUploadedFile(StreamInterface $stream, ?int $size = null, int $error = \UPLOAD_ERR_OK, ?string $clientFilename = null, ?string $clientMediaType = null): UploadedFileInterface |
|
| 138 | - { |
|
| 139 | - $factory = $this->uploadedFileFactory ?? $this->setFactory(Psr17FactoryDiscovery::findUploadedFileFactory()); |
|
| 140 | - |
|
| 141 | - return $factory->createUploadedFile(...\func_get_args()); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - public function createUri(string $uri = ''): UriInterface |
|
| 145 | - { |
|
| 146 | - $factory = $this->uriFactory ?? $this->setFactory(Psr17FactoryDiscovery::findUriFactory()); |
|
| 147 | - |
|
| 148 | - return $factory->createUri(...\func_get_args()); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - public function createUriFromGlobals(?array $server = null): UriInterface |
|
| 152 | - { |
|
| 153 | - return $this->buildUriFromGlobals($this->createUri(''), $server ?? $_SERVER); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - private function setFactory($factory) |
|
| 157 | - { |
|
| 158 | - if (!$this->requestFactory && $factory instanceof RequestFactoryInterface) { |
|
| 159 | - $this->requestFactory = $factory; |
|
| 160 | - } |
|
| 161 | - if (!$this->responseFactory && $factory instanceof ResponseFactoryInterface) { |
|
| 162 | - $this->responseFactory = $factory; |
|
| 163 | - } |
|
| 164 | - if (!$this->serverRequestFactory && $factory instanceof ServerRequestFactoryInterface) { |
|
| 165 | - $this->serverRequestFactory = $factory; |
|
| 166 | - } |
|
| 167 | - if (!$this->streamFactory && $factory instanceof StreamFactoryInterface) { |
|
| 168 | - $this->streamFactory = $factory; |
|
| 169 | - } |
|
| 170 | - if (!$this->uploadedFileFactory && $factory instanceof UploadedFileFactoryInterface) { |
|
| 171 | - $this->uploadedFileFactory = $factory; |
|
| 172 | - } |
|
| 173 | - if (!$this->uriFactory && $factory instanceof UriFactoryInterface) { |
|
| 174 | - $this->uriFactory = $factory; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $factory; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - private function buildServerRequestFromGlobals(ServerRequestInterface $request, array $server, array $files): ServerRequestInterface |
|
| 181 | - { |
|
| 182 | - $request = $request |
|
| 183 | - ->withProtocolVersion(isset($server['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1') |
|
| 184 | - ->withUploadedFiles($this->normalizeFiles($files)); |
|
| 185 | - |
|
| 186 | - $headers = []; |
|
| 187 | - foreach ($server as $k => $v) { |
|
| 188 | - if (0 === strpos($k, 'HTTP_')) { |
|
| 189 | - $k = substr($k, 5); |
|
| 190 | - } elseif (!\in_array($k, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) { |
|
| 191 | - continue; |
|
| 192 | - } |
|
| 193 | - $k = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $k)))); |
|
| 194 | - |
|
| 195 | - $headers[$k] = $v; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - if (!isset($headers['Authorization'])) { |
|
| 199 | - if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { |
|
| 200 | - $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; |
|
| 201 | - } elseif (isset($_SERVER['PHP_AUTH_USER'])) { |
|
| 202 | - $headers['Authorization'] = 'Basic '.base64_encode($_SERVER['PHP_AUTH_USER'].':'.($_SERVER['PHP_AUTH_PW'] ?? '')); |
|
| 203 | - } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) { |
|
| 204 | - $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST']; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - foreach ($headers as $k => $v) { |
|
| 209 | - try { |
|
| 210 | - $request = $request->withHeader($k, $v); |
|
| 211 | - } catch (\InvalidArgumentException $e) { |
|
| 212 | - // ignore invalid headers |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - return $request; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - private function buildUriFromGlobals(UriInterface $uri, array $server): UriInterface |
|
| 220 | - { |
|
| 221 | - $uri = $uri->withScheme(!empty($server['HTTPS']) && 'off' !== strtolower($server['HTTPS']) ? 'https' : 'http'); |
|
| 222 | - |
|
| 223 | - $hasPort = false; |
|
| 224 | - if (isset($server['HTTP_HOST'])) { |
|
| 225 | - $parts = parse_url('http://'.$server['HTTP_HOST']); |
|
| 226 | - |
|
| 227 | - $uri = $uri->withHost($parts['host'] ?? 'localhost'); |
|
| 228 | - |
|
| 229 | - if ($parts['port'] ?? false) { |
|
| 230 | - $hasPort = true; |
|
| 231 | - $uri = $uri->withPort($parts['port']); |
|
| 232 | - } |
|
| 233 | - } else { |
|
| 234 | - $uri = $uri->withHost($server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? 'localhost'); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if (!$hasPort && isset($server['SERVER_PORT'])) { |
|
| 238 | - $uri = $uri->withPort($server['SERVER_PORT']); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $hasQuery = false; |
|
| 242 | - if (isset($server['REQUEST_URI'])) { |
|
| 243 | - $requestUriParts = explode('?', $server['REQUEST_URI'], 2); |
|
| 244 | - $uri = $uri->withPath($requestUriParts[0]); |
|
| 245 | - if (isset($requestUriParts[1])) { |
|
| 246 | - $hasQuery = true; |
|
| 247 | - $uri = $uri->withQuery($requestUriParts[1]); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if (!$hasQuery && isset($server['QUERY_STRING'])) { |
|
| 252 | - $uri = $uri->withQuery($server['QUERY_STRING']); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $uri; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - private function normalizeFiles(array $files): array |
|
| 259 | - { |
|
| 260 | - foreach ($files as $k => $v) { |
|
| 261 | - if ($v instanceof UploadedFileInterface) { |
|
| 262 | - continue; |
|
| 263 | - } |
|
| 264 | - if (!\is_array($v)) { |
|
| 265 | - unset($files[$k]); |
|
| 266 | - } elseif (!isset($v['tmp_name'])) { |
|
| 267 | - $files[$k] = $this->normalizeFiles($v); |
|
| 268 | - } else { |
|
| 269 | - $files[$k] = $this->createUploadedFileFromSpec($v); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return $files; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Create and return an UploadedFile instance from a $_FILES specification. |
|
| 278 | - * |
|
| 279 | - * @param array $value $_FILES struct |
|
| 280 | - * |
|
| 281 | - * @return UploadedFileInterface|UploadedFileInterface[] |
|
| 282 | - */ |
|
| 283 | - private function createUploadedFileFromSpec(array $value) |
|
| 284 | - { |
|
| 285 | - if (!is_array($tmpName = $value['tmp_name'])) { |
|
| 286 | - $file = is_file($tmpName) ? $this->createStreamFromFile($tmpName, 'r') : $this->createStream(); |
|
| 287 | - |
|
| 288 | - return $this->createUploadedFile($file, $value['size'], $value['error'], $value['name'], $value['type']); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - foreach ($tmpName as $k => $v) { |
|
| 292 | - $tmpName[$k] = $this->createUploadedFileFromSpec([ |
|
| 293 | - 'tmp_name' => $v, |
|
| 294 | - 'size' => $value['size'][$k] ?? null, |
|
| 295 | - 'error' => $value['error'][$k] ?? null, |
|
| 296 | - 'name' => $value['name'][$k] ?? null, |
|
| 297 | - 'type' => $value['type'][$k] ?? null, |
|
| 298 | - ]); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return $tmpName; |
|
| 302 | - } |
|
| 44 | + private $requestFactory; |
|
| 45 | + private $responseFactory; |
|
| 46 | + private $serverRequestFactory; |
|
| 47 | + private $streamFactory; |
|
| 48 | + private $uploadedFileFactory; |
|
| 49 | + private $uriFactory; |
|
| 50 | + |
|
| 51 | + public function __construct( |
|
| 52 | + ?RequestFactoryInterface $requestFactory = null, |
|
| 53 | + ?ResponseFactoryInterface $responseFactory = null, |
|
| 54 | + ?ServerRequestFactoryInterface $serverRequestFactory = null, |
|
| 55 | + ?StreamFactoryInterface $streamFactory = null, |
|
| 56 | + ?UploadedFileFactoryInterface $uploadedFileFactory = null, |
|
| 57 | + ?UriFactoryInterface $uriFactory = null |
|
| 58 | + ) { |
|
| 59 | + $this->requestFactory = $requestFactory; |
|
| 60 | + $this->responseFactory = $responseFactory; |
|
| 61 | + $this->serverRequestFactory = $serverRequestFactory; |
|
| 62 | + $this->streamFactory = $streamFactory; |
|
| 63 | + $this->uploadedFileFactory = $uploadedFileFactory; |
|
| 64 | + $this->uriFactory = $uriFactory; |
|
| 65 | + |
|
| 66 | + $this->setFactory($requestFactory); |
|
| 67 | + $this->setFactory($responseFactory); |
|
| 68 | + $this->setFactory($serverRequestFactory); |
|
| 69 | + $this->setFactory($streamFactory); |
|
| 70 | + $this->setFactory($uploadedFileFactory); |
|
| 71 | + $this->setFactory($uriFactory); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param UriInterface|string $uri |
|
| 76 | + */ |
|
| 77 | + public function createRequest(string $method, $uri): RequestInterface |
|
| 78 | + { |
|
| 79 | + $factory = $this->requestFactory ?? $this->setFactory(Psr17FactoryDiscovery::findRequestFactory()); |
|
| 80 | + |
|
| 81 | + return $factory->createRequest(...\func_get_args()); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
| 85 | + { |
|
| 86 | + $factory = $this->responseFactory ?? $this->setFactory(Psr17FactoryDiscovery::findResponseFactory()); |
|
| 87 | + |
|
| 88 | + return $factory->createResponse(...\func_get_args()); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param UriInterface|string $uri |
|
| 93 | + */ |
|
| 94 | + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface |
|
| 95 | + { |
|
| 96 | + $factory = $this->serverRequestFactory ?? $this->setFactory(Psr17FactoryDiscovery::findServerRequestFactory()); |
|
| 97 | + |
|
| 98 | + return $factory->createServerRequest(...\func_get_args()); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function createServerRequestFromGlobals(?array $server = null, ?array $get = null, ?array $post = null, ?array $cookie = null, ?array $files = null, ?StreamInterface $body = null): ServerRequestInterface |
|
| 102 | + { |
|
| 103 | + $server = $server ?? $_SERVER; |
|
| 104 | + $request = $this->createServerRequest($server['REQUEST_METHOD'] ?? 'GET', $this->createUriFromGlobals($server), $server); |
|
| 105 | + |
|
| 106 | + return $this->buildServerRequestFromGlobals($request, $server, $files ?? $_FILES) |
|
| 107 | + ->withQueryParams($get ?? $_GET) |
|
| 108 | + ->withParsedBody($post ?? $_POST) |
|
| 109 | + ->withCookieParams($cookie ?? $_COOKIE) |
|
| 110 | + ->withBody($body ?? $this->createStreamFromFile('php://input', 'r+')); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function createStream(string $content = ''): StreamInterface |
|
| 114 | + { |
|
| 115 | + $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 116 | + |
|
| 117 | + return $factory->createStream($content); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface |
|
| 121 | + { |
|
| 122 | + $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 123 | + |
|
| 124 | + return $factory->createStreamFromFile($filename, $mode); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param resource $resource |
|
| 129 | + */ |
|
| 130 | + public function createStreamFromResource($resource): StreamInterface |
|
| 131 | + { |
|
| 132 | + $factory = $this->streamFactory ?? $this->setFactory(Psr17FactoryDiscovery::findStreamFactory()); |
|
| 133 | + |
|
| 134 | + return $factory->createStreamFromResource($resource); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function createUploadedFile(StreamInterface $stream, ?int $size = null, int $error = \UPLOAD_ERR_OK, ?string $clientFilename = null, ?string $clientMediaType = null): UploadedFileInterface |
|
| 138 | + { |
|
| 139 | + $factory = $this->uploadedFileFactory ?? $this->setFactory(Psr17FactoryDiscovery::findUploadedFileFactory()); |
|
| 140 | + |
|
| 141 | + return $factory->createUploadedFile(...\func_get_args()); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + public function createUri(string $uri = ''): UriInterface |
|
| 145 | + { |
|
| 146 | + $factory = $this->uriFactory ?? $this->setFactory(Psr17FactoryDiscovery::findUriFactory()); |
|
| 147 | + |
|
| 148 | + return $factory->createUri(...\func_get_args()); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + public function createUriFromGlobals(?array $server = null): UriInterface |
|
| 152 | + { |
|
| 153 | + return $this->buildUriFromGlobals($this->createUri(''), $server ?? $_SERVER); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + private function setFactory($factory) |
|
| 157 | + { |
|
| 158 | + if (!$this->requestFactory && $factory instanceof RequestFactoryInterface) { |
|
| 159 | + $this->requestFactory = $factory; |
|
| 160 | + } |
|
| 161 | + if (!$this->responseFactory && $factory instanceof ResponseFactoryInterface) { |
|
| 162 | + $this->responseFactory = $factory; |
|
| 163 | + } |
|
| 164 | + if (!$this->serverRequestFactory && $factory instanceof ServerRequestFactoryInterface) { |
|
| 165 | + $this->serverRequestFactory = $factory; |
|
| 166 | + } |
|
| 167 | + if (!$this->streamFactory && $factory instanceof StreamFactoryInterface) { |
|
| 168 | + $this->streamFactory = $factory; |
|
| 169 | + } |
|
| 170 | + if (!$this->uploadedFileFactory && $factory instanceof UploadedFileFactoryInterface) { |
|
| 171 | + $this->uploadedFileFactory = $factory; |
|
| 172 | + } |
|
| 173 | + if (!$this->uriFactory && $factory instanceof UriFactoryInterface) { |
|
| 174 | + $this->uriFactory = $factory; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $factory; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + private function buildServerRequestFromGlobals(ServerRequestInterface $request, array $server, array $files): ServerRequestInterface |
|
| 181 | + { |
|
| 182 | + $request = $request |
|
| 183 | + ->withProtocolVersion(isset($server['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1') |
|
| 184 | + ->withUploadedFiles($this->normalizeFiles($files)); |
|
| 185 | + |
|
| 186 | + $headers = []; |
|
| 187 | + foreach ($server as $k => $v) { |
|
| 188 | + if (0 === strpos($k, 'HTTP_')) { |
|
| 189 | + $k = substr($k, 5); |
|
| 190 | + } elseif (!\in_array($k, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) { |
|
| 191 | + continue; |
|
| 192 | + } |
|
| 193 | + $k = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $k)))); |
|
| 194 | + |
|
| 195 | + $headers[$k] = $v; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + if (!isset($headers['Authorization'])) { |
|
| 199 | + if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { |
|
| 200 | + $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; |
|
| 201 | + } elseif (isset($_SERVER['PHP_AUTH_USER'])) { |
|
| 202 | + $headers['Authorization'] = 'Basic '.base64_encode($_SERVER['PHP_AUTH_USER'].':'.($_SERVER['PHP_AUTH_PW'] ?? '')); |
|
| 203 | + } elseif (isset($_SERVER['PHP_AUTH_DIGEST'])) { |
|
| 204 | + $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST']; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + foreach ($headers as $k => $v) { |
|
| 209 | + try { |
|
| 210 | + $request = $request->withHeader($k, $v); |
|
| 211 | + } catch (\InvalidArgumentException $e) { |
|
| 212 | + // ignore invalid headers |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + return $request; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + private function buildUriFromGlobals(UriInterface $uri, array $server): UriInterface |
|
| 220 | + { |
|
| 221 | + $uri = $uri->withScheme(!empty($server['HTTPS']) && 'off' !== strtolower($server['HTTPS']) ? 'https' : 'http'); |
|
| 222 | + |
|
| 223 | + $hasPort = false; |
|
| 224 | + if (isset($server['HTTP_HOST'])) { |
|
| 225 | + $parts = parse_url('http://'.$server['HTTP_HOST']); |
|
| 226 | + |
|
| 227 | + $uri = $uri->withHost($parts['host'] ?? 'localhost'); |
|
| 228 | + |
|
| 229 | + if ($parts['port'] ?? false) { |
|
| 230 | + $hasPort = true; |
|
| 231 | + $uri = $uri->withPort($parts['port']); |
|
| 232 | + } |
|
| 233 | + } else { |
|
| 234 | + $uri = $uri->withHost($server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? 'localhost'); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if (!$hasPort && isset($server['SERVER_PORT'])) { |
|
| 238 | + $uri = $uri->withPort($server['SERVER_PORT']); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $hasQuery = false; |
|
| 242 | + if (isset($server['REQUEST_URI'])) { |
|
| 243 | + $requestUriParts = explode('?', $server['REQUEST_URI'], 2); |
|
| 244 | + $uri = $uri->withPath($requestUriParts[0]); |
|
| 245 | + if (isset($requestUriParts[1])) { |
|
| 246 | + $hasQuery = true; |
|
| 247 | + $uri = $uri->withQuery($requestUriParts[1]); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if (!$hasQuery && isset($server['QUERY_STRING'])) { |
|
| 252 | + $uri = $uri->withQuery($server['QUERY_STRING']); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $uri; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + private function normalizeFiles(array $files): array |
|
| 259 | + { |
|
| 260 | + foreach ($files as $k => $v) { |
|
| 261 | + if ($v instanceof UploadedFileInterface) { |
|
| 262 | + continue; |
|
| 263 | + } |
|
| 264 | + if (!\is_array($v)) { |
|
| 265 | + unset($files[$k]); |
|
| 266 | + } elseif (!isset($v['tmp_name'])) { |
|
| 267 | + $files[$k] = $this->normalizeFiles($v); |
|
| 268 | + } else { |
|
| 269 | + $files[$k] = $this->createUploadedFileFromSpec($v); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return $files; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Create and return an UploadedFile instance from a $_FILES specification. |
|
| 278 | + * |
|
| 279 | + * @param array $value $_FILES struct |
|
| 280 | + * |
|
| 281 | + * @return UploadedFileInterface|UploadedFileInterface[] |
|
| 282 | + */ |
|
| 283 | + private function createUploadedFileFromSpec(array $value) |
|
| 284 | + { |
|
| 285 | + if (!is_array($tmpName = $value['tmp_name'])) { |
|
| 286 | + $file = is_file($tmpName) ? $this->createStreamFromFile($tmpName, 'r') : $this->createStream(); |
|
| 287 | + |
|
| 288 | + return $this->createUploadedFile($file, $value['size'], $value['error'], $value['name'], $value['type']); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + foreach ($tmpName as $k => $v) { |
|
| 292 | + $tmpName[$k] = $this->createUploadedFileFromSpec([ |
|
| 293 | + 'tmp_name' => $v, |
|
| 294 | + 'size' => $value['size'][$k] ?? null, |
|
| 295 | + 'error' => $value['error'][$k] ?? null, |
|
| 296 | + 'name' => $value['name'][$k] ?? null, |
|
| 297 | + 'type' => $value['type'][$k] ?? null, |
|
| 298 | + ]); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return $tmpName; |
|
| 302 | + } |
|
| 303 | 303 | } |
@@ -14,21 +14,21 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | final class HttpClientDiscovery extends ClassDiscovery |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Finds an HTTP Client. |
|
| 19 | - * |
|
| 20 | - * @return HttpClient |
|
| 21 | - * |
|
| 22 | - * @throws Exception\NotFoundException |
|
| 23 | - */ |
|
| 24 | - public static function find() |
|
| 25 | - { |
|
| 26 | - try { |
|
| 27 | - $client = static::findOneByType(HttpClient::class); |
|
| 28 | - } catch (DiscoveryFailedException $e) { |
|
| 29 | - 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); |
|
| 30 | - } |
|
| 17 | + /** |
|
| 18 | + * Finds an HTTP Client. |
|
| 19 | + * |
|
| 20 | + * @return HttpClient |
|
| 21 | + * |
|
| 22 | + * @throws Exception\NotFoundException |
|
| 23 | + */ |
|
| 24 | + public static function find() |
|
| 25 | + { |
|
| 26 | + try { |
|
| 27 | + $client = static::findOneByType(HttpClient::class); |
|
| 28 | + } catch (DiscoveryFailedException $e) { |
|
| 29 | + 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); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - return static::instantiateClass($client); |
|
| 33 | - } |
|
| 32 | + return static::instantiateClass($client); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -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,21 +13,21 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | final class Psr18ClientDiscovery extends ClassDiscovery |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Finds a PSR-18 HTTP Client. |
|
| 18 | - * |
|
| 19 | - * @return ClientInterface |
|
| 20 | - * |
|
| 21 | - * @throws RealNotFoundException |
|
| 22 | - */ |
|
| 23 | - public static function find() |
|
| 24 | - { |
|
| 25 | - try { |
|
| 26 | - $client = static::findOneByType(ClientInterface::class); |
|
| 27 | - } catch (DiscoveryFailedException $e) { |
|
| 28 | - 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); |
|
| 29 | - } |
|
| 16 | + /** |
|
| 17 | + * Finds a PSR-18 HTTP Client. |
|
| 18 | + * |
|
| 19 | + * @return ClientInterface |
|
| 20 | + * |
|
| 21 | + * @throws RealNotFoundException |
|
| 22 | + */ |
|
| 23 | + public static function find() |
|
| 24 | + { |
|
| 25 | + try { |
|
| 26 | + $client = static::findOneByType(ClientInterface::class); |
|
| 27 | + } catch (DiscoveryFailedException $e) { |
|
| 28 | + 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); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - return static::instantiateClass($client); |
|
| 32 | - } |
|
| 31 | + return static::instantiateClass($client); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -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 | * |
@@ -11,41 +11,41 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | final class DiscoveryFailedException extends \Exception implements Exception |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var \Exception[] |
|
| 16 | - */ |
|
| 17 | - private $exceptions; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @param string $message |
|
| 21 | - * @param \Exception[] $exceptions |
|
| 22 | - */ |
|
| 23 | - public function __construct($message, array $exceptions = []) |
|
| 24 | - { |
|
| 25 | - $this->exceptions = $exceptions; |
|
| 26 | - |
|
| 27 | - parent::__construct($message); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param \Exception[] $exceptions |
|
| 32 | - */ |
|
| 33 | - public static function create($exceptions) |
|
| 34 | - { |
|
| 35 | - $message = 'Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors'; |
|
| 36 | - foreach ($exceptions as $e) { |
|
| 37 | - $message .= "\n - ".$e->getMessage(); |
|
| 38 | - } |
|
| 39 | - $message .= "\n\n"; |
|
| 40 | - |
|
| 41 | - return new self($message, $exceptions); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return \Exception[] |
|
| 46 | - */ |
|
| 47 | - public function getExceptions() |
|
| 48 | - { |
|
| 49 | - return $this->exceptions; |
|
| 50 | - } |
|
| 14 | + /** |
|
| 15 | + * @var \Exception[] |
|
| 16 | + */ |
|
| 17 | + private $exceptions; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @param string $message |
|
| 21 | + * @param \Exception[] $exceptions |
|
| 22 | + */ |
|
| 23 | + public function __construct($message, array $exceptions = []) |
|
| 24 | + { |
|
| 25 | + $this->exceptions = $exceptions; |
|
| 26 | + |
|
| 27 | + parent::__construct($message); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param \Exception[] $exceptions |
|
| 32 | + */ |
|
| 33 | + public static function create($exceptions) |
|
| 34 | + { |
|
| 35 | + $message = 'Could not find resource using any discovery strategy. Find more information at http://docs.php-http.org/en/latest/discovery.html#common-errors'; |
|
| 36 | + foreach ($exceptions as $e) { |
|
| 37 | + $message .= "\n - ".$e->getMessage(); |
|
| 38 | + } |
|
| 39 | + $message .= "\n\n"; |
|
| 40 | + |
|
| 41 | + return new self($message, $exceptions); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return \Exception[] |
|
| 46 | + */ |
|
| 47 | + public function getExceptions() |
|
| 48 | + { |
|
| 49 | + return $this->exceptions; |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -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 | } |
@@ -11,37 +11,37 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | final class NoCandidateFoundException extends \Exception implements Exception |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @param string $strategy |
|
| 16 | - */ |
|
| 17 | - public function __construct($strategy, array $candidates) |
|
| 18 | - { |
|
| 19 | - $classes = array_map( |
|
| 20 | - function ($a) { |
|
| 21 | - return $a['class']; |
|
| 22 | - }, |
|
| 23 | - $candidates |
|
| 24 | - ); |
|
| 25 | - |
|
| 26 | - $message = sprintf( |
|
| 27 | - 'No valid candidate found using strategy "%s". We tested the following candidates: %s.', |
|
| 28 | - $strategy, |
|
| 29 | - implode(', ', array_map([$this, 'stringify'], $classes)) |
|
| 30 | - ); |
|
| 31 | - |
|
| 32 | - parent::__construct($message); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - private function stringify($mixed) |
|
| 36 | - { |
|
| 37 | - if (is_string($mixed)) { |
|
| 38 | - return $mixed; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - if (is_array($mixed) && 2 === count($mixed)) { |
|
| 42 | - return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return is_object($mixed) ? get_class($mixed) : gettype($mixed); |
|
| 46 | - } |
|
| 14 | + /** |
|
| 15 | + * @param string $strategy |
|
| 16 | + */ |
|
| 17 | + public function __construct($strategy, array $candidates) |
|
| 18 | + { |
|
| 19 | + $classes = array_map( |
|
| 20 | + function ($a) { |
|
| 21 | + return $a['class']; |
|
| 22 | + }, |
|
| 23 | + $candidates |
|
| 24 | + ); |
|
| 25 | + |
|
| 26 | + $message = sprintf( |
|
| 27 | + 'No valid candidate found using strategy "%s". We tested the following candidates: %s.', |
|
| 28 | + $strategy, |
|
| 29 | + implode(', ', array_map([$this, 'stringify'], $classes)) |
|
| 30 | + ); |
|
| 31 | + |
|
| 32 | + parent::__construct($message); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + private function stringify($mixed) |
|
| 36 | + { |
|
| 37 | + if (is_string($mixed)) { |
|
| 38 | + return $mixed; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + if (is_array($mixed) && 2 === count($mixed)) { |
|
| 42 | + return sprintf('%s::%s', $this->stringify($mixed[0]), $mixed[1]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return is_object($mixed) ? get_class($mixed) : gettype($mixed); |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -17,7 +17,7 @@ |
||
| 17 | 17 | public function __construct($strategy, array $candidates) |
| 18 | 18 | { |
| 19 | 19 | $classes = array_map( |
| 20 | - function ($a) { |
|
| 20 | + function($a) { |
|
| 21 | 21 | return $a['class']; |
| 22 | 22 | }, |
| 23 | 23 | $candidates |
@@ -48,138 +48,138 @@ |
||
| 48 | 48 | */ |
| 49 | 49 | final class CommonClassesStrategy implements DiscoveryStrategy |
| 50 | 50 | { |
| 51 | - /** |
|
| 52 | - * @var array |
|
| 53 | - */ |
|
| 54 | - private static $classes = [ |
|
| 55 | - MessageFactory::class => [ |
|
| 56 | - ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 57 | - ['class' => GuzzleMessageFactory::class, 'condition' => [GuzzleRequest::class, GuzzleMessageFactory::class]], |
|
| 58 | - ['class' => DiactorosMessageFactory::class, 'condition' => [DiactorosRequest::class, DiactorosMessageFactory::class]], |
|
| 59 | - ['class' => SlimMessageFactory::class, 'condition' => [SlimRequest::class, SlimMessageFactory::class]], |
|
| 60 | - ], |
|
| 61 | - StreamFactory::class => [ |
|
| 62 | - ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 63 | - ['class' => GuzzleStreamFactory::class, 'condition' => [GuzzleRequest::class, GuzzleStreamFactory::class]], |
|
| 64 | - ['class' => DiactorosStreamFactory::class, 'condition' => [DiactorosRequest::class, DiactorosStreamFactory::class]], |
|
| 65 | - ['class' => SlimStreamFactory::class, 'condition' => [SlimRequest::class, SlimStreamFactory::class]], |
|
| 66 | - ], |
|
| 67 | - UriFactory::class => [ |
|
| 68 | - ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 69 | - ['class' => GuzzleUriFactory::class, 'condition' => [GuzzleRequest::class, GuzzleUriFactory::class]], |
|
| 70 | - ['class' => DiactorosUriFactory::class, 'condition' => [DiactorosRequest::class, DiactorosUriFactory::class]], |
|
| 71 | - ['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]], |
|
| 72 | - ], |
|
| 73 | - HttpAsyncClient::class => [ |
|
| 74 | - ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, [self::class, 'isPsr17FactoryInstalled']]], |
|
| 75 | - ['class' => Guzzle7::class, 'condition' => Guzzle7::class], |
|
| 76 | - ['class' => Guzzle6::class, 'condition' => Guzzle6::class], |
|
| 77 | - ['class' => Curl::class, 'condition' => Curl::class], |
|
| 78 | - ['class' => React::class, 'condition' => React::class], |
|
| 79 | - ], |
|
| 80 | - HttpClient::class => [ |
|
| 81 | - ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, [self::class, 'isPsr17FactoryInstalled'], [self::class, 'isSymfonyImplementingHttpClient']]], |
|
| 82 | - ['class' => Guzzle7::class, 'condition' => Guzzle7::class], |
|
| 83 | - ['class' => Guzzle6::class, 'condition' => Guzzle6::class], |
|
| 84 | - ['class' => Guzzle5::class, 'condition' => Guzzle5::class], |
|
| 85 | - ['class' => Curl::class, 'condition' => Curl::class], |
|
| 86 | - ['class' => Socket::class, 'condition' => Socket::class], |
|
| 87 | - ['class' => Buzz::class, 'condition' => Buzz::class], |
|
| 88 | - ['class' => React::class, 'condition' => React::class], |
|
| 89 | - ['class' => Cake::class, 'condition' => Cake::class], |
|
| 90 | - ['class' => Artax::class, 'condition' => Artax::class], |
|
| 91 | - [ |
|
| 92 | - 'class' => [self::class, 'buzzInstantiate'], |
|
| 93 | - 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], |
|
| 94 | - ], |
|
| 95 | - ], |
|
| 96 | - Psr18Client::class => [ |
|
| 97 | - [ |
|
| 98 | - 'class' => [self::class, 'symfonyPsr18Instantiate'], |
|
| 99 | - 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class], |
|
| 100 | - ], |
|
| 101 | - [ |
|
| 102 | - 'class' => GuzzleHttp::class, |
|
| 103 | - 'condition' => [self::class, 'isGuzzleImplementingPsr18'], |
|
| 104 | - ], |
|
| 105 | - [ |
|
| 106 | - 'class' => [self::class, 'buzzInstantiate'], |
|
| 107 | - 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], |
|
| 108 | - ], |
|
| 109 | - ], |
|
| 110 | - ]; |
|
| 51 | + /** |
|
| 52 | + * @var array |
|
| 53 | + */ |
|
| 54 | + private static $classes = [ |
|
| 55 | + MessageFactory::class => [ |
|
| 56 | + ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 57 | + ['class' => GuzzleMessageFactory::class, 'condition' => [GuzzleRequest::class, GuzzleMessageFactory::class]], |
|
| 58 | + ['class' => DiactorosMessageFactory::class, 'condition' => [DiactorosRequest::class, DiactorosMessageFactory::class]], |
|
| 59 | + ['class' => SlimMessageFactory::class, 'condition' => [SlimRequest::class, SlimMessageFactory::class]], |
|
| 60 | + ], |
|
| 61 | + StreamFactory::class => [ |
|
| 62 | + ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 63 | + ['class' => GuzzleStreamFactory::class, 'condition' => [GuzzleRequest::class, GuzzleStreamFactory::class]], |
|
| 64 | + ['class' => DiactorosStreamFactory::class, 'condition' => [DiactorosRequest::class, DiactorosStreamFactory::class]], |
|
| 65 | + ['class' => SlimStreamFactory::class, 'condition' => [SlimRequest::class, SlimStreamFactory::class]], |
|
| 66 | + ], |
|
| 67 | + UriFactory::class => [ |
|
| 68 | + ['class' => NyholmHttplugFactory::class, 'condition' => [NyholmHttplugFactory::class]], |
|
| 69 | + ['class' => GuzzleUriFactory::class, 'condition' => [GuzzleRequest::class, GuzzleUriFactory::class]], |
|
| 70 | + ['class' => DiactorosUriFactory::class, 'condition' => [DiactorosRequest::class, DiactorosUriFactory::class]], |
|
| 71 | + ['class' => SlimUriFactory::class, 'condition' => [SlimRequest::class, SlimUriFactory::class]], |
|
| 72 | + ], |
|
| 73 | + HttpAsyncClient::class => [ |
|
| 74 | + ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, Promise::class, [self::class, 'isPsr17FactoryInstalled']]], |
|
| 75 | + ['class' => Guzzle7::class, 'condition' => Guzzle7::class], |
|
| 76 | + ['class' => Guzzle6::class, 'condition' => Guzzle6::class], |
|
| 77 | + ['class' => Curl::class, 'condition' => Curl::class], |
|
| 78 | + ['class' => React::class, 'condition' => React::class], |
|
| 79 | + ], |
|
| 80 | + HttpClient::class => [ |
|
| 81 | + ['class' => SymfonyHttplug::class, 'condition' => [SymfonyHttplug::class, [self::class, 'isPsr17FactoryInstalled'], [self::class, 'isSymfonyImplementingHttpClient']]], |
|
| 82 | + ['class' => Guzzle7::class, 'condition' => Guzzle7::class], |
|
| 83 | + ['class' => Guzzle6::class, 'condition' => Guzzle6::class], |
|
| 84 | + ['class' => Guzzle5::class, 'condition' => Guzzle5::class], |
|
| 85 | + ['class' => Curl::class, 'condition' => Curl::class], |
|
| 86 | + ['class' => Socket::class, 'condition' => Socket::class], |
|
| 87 | + ['class' => Buzz::class, 'condition' => Buzz::class], |
|
| 88 | + ['class' => React::class, 'condition' => React::class], |
|
| 89 | + ['class' => Cake::class, 'condition' => Cake::class], |
|
| 90 | + ['class' => Artax::class, 'condition' => Artax::class], |
|
| 91 | + [ |
|
| 92 | + 'class' => [self::class, 'buzzInstantiate'], |
|
| 93 | + 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], |
|
| 94 | + ], |
|
| 95 | + ], |
|
| 96 | + Psr18Client::class => [ |
|
| 97 | + [ |
|
| 98 | + 'class' => [self::class, 'symfonyPsr18Instantiate'], |
|
| 99 | + 'condition' => [SymfonyPsr18::class, Psr17RequestFactory::class], |
|
| 100 | + ], |
|
| 101 | + [ |
|
| 102 | + 'class' => GuzzleHttp::class, |
|
| 103 | + 'condition' => [self::class, 'isGuzzleImplementingPsr18'], |
|
| 104 | + ], |
|
| 105 | + [ |
|
| 106 | + 'class' => [self::class, 'buzzInstantiate'], |
|
| 107 | + 'condition' => [\Buzz\Client\FileGetContents::class, \Buzz\Message\ResponseBuilder::class], |
|
| 108 | + ], |
|
| 109 | + ], |
|
| 110 | + ]; |
|
| 111 | 111 | |
| 112 | - public static function getCandidates($type) |
|
| 113 | - { |
|
| 114 | - if (Psr18Client::class === $type) { |
|
| 115 | - return self::getPsr18Candidates(); |
|
| 116 | - } |
|
| 112 | + public static function getCandidates($type) |
|
| 113 | + { |
|
| 114 | + if (Psr18Client::class === $type) { |
|
| 115 | + return self::getPsr18Candidates(); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - return self::$classes[$type] ?? []; |
|
| 119 | - } |
|
| 118 | + return self::$classes[$type] ?? []; |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @return array The return value is always an array with zero or more elements. Each |
|
| 123 | - * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 124 | - */ |
|
| 125 | - private static function getPsr18Candidates() |
|
| 126 | - { |
|
| 127 | - $candidates = self::$classes[Psr18Client::class]; |
|
| 121 | + /** |
|
| 122 | + * @return array The return value is always an array with zero or more elements. Each |
|
| 123 | + * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 124 | + */ |
|
| 125 | + private static function getPsr18Candidates() |
|
| 126 | + { |
|
| 127 | + $candidates = self::$classes[Psr18Client::class]; |
|
| 128 | 128 | |
| 129 | - // HTTPlug 2.0 clients implements PSR18Client too. |
|
| 130 | - foreach (self::$classes[HttpClient::class] as $c) { |
|
| 131 | - if (!is_string($c['class'])) { |
|
| 132 | - continue; |
|
| 133 | - } |
|
| 134 | - try { |
|
| 135 | - if (ClassDiscovery::safeClassExists($c['class']) && is_subclass_of($c['class'], Psr18Client::class)) { |
|
| 136 | - $candidates[] = $c; |
|
| 137 | - } |
|
| 138 | - } catch (\Throwable $e) { |
|
| 139 | - trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); |
|
| 140 | - } |
|
| 141 | - } |
|
| 129 | + // HTTPlug 2.0 clients implements PSR18Client too. |
|
| 130 | + foreach (self::$classes[HttpClient::class] as $c) { |
|
| 131 | + if (!is_string($c['class'])) { |
|
| 132 | + continue; |
|
| 133 | + } |
|
| 134 | + try { |
|
| 135 | + if (ClassDiscovery::safeClassExists($c['class']) && is_subclass_of($c['class'], Psr18Client::class)) { |
|
| 136 | + $candidates[] = $c; |
|
| 137 | + } |
|
| 138 | + } catch (\Throwable $e) { |
|
| 139 | + trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-18 Client is available', get_class($e), $e->getMessage()), E_USER_WARNING); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - return $candidates; |
|
| 144 | - } |
|
| 143 | + return $candidates; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - public static function buzzInstantiate() |
|
| 147 | - { |
|
| 148 | - return new \Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory()); |
|
| 149 | - } |
|
| 146 | + public static function buzzInstantiate() |
|
| 147 | + { |
|
| 148 | + return new \Buzz\Client\FileGetContents(Psr17FactoryDiscovery::findResponseFactory()); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - public static function symfonyPsr18Instantiate() |
|
| 152 | - { |
|
| 153 | - return new SymfonyPsr18(null, Psr17FactoryDiscovery::findResponseFactory(), Psr17FactoryDiscovery::findStreamFactory()); |
|
| 154 | - } |
|
| 151 | + public static function symfonyPsr18Instantiate() |
|
| 152 | + { |
|
| 153 | + return new SymfonyPsr18(null, Psr17FactoryDiscovery::findResponseFactory(), Psr17FactoryDiscovery::findStreamFactory()); |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - public static function isGuzzleImplementingPsr18() |
|
| 157 | - { |
|
| 158 | - return defined('GuzzleHttp\ClientInterface::MAJOR_VERSION'); |
|
| 159 | - } |
|
| 156 | + public static function isGuzzleImplementingPsr18() |
|
| 157 | + { |
|
| 158 | + return defined('GuzzleHttp\ClientInterface::MAJOR_VERSION'); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - public static function isSymfonyImplementingHttpClient() |
|
| 162 | - { |
|
| 163 | - return is_subclass_of(SymfonyHttplug::class, HttpClient::class); |
|
| 164 | - } |
|
| 161 | + public static function isSymfonyImplementingHttpClient() |
|
| 162 | + { |
|
| 163 | + return is_subclass_of(SymfonyHttplug::class, HttpClient::class); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Can be used as a condition. |
|
| 168 | - * |
|
| 169 | - * @return bool |
|
| 170 | - */ |
|
| 171 | - public static function isPsr17FactoryInstalled() |
|
| 172 | - { |
|
| 173 | - try { |
|
| 174 | - Psr17FactoryDiscovery::findResponseFactory(); |
|
| 175 | - } catch (NotFoundException $e) { |
|
| 176 | - return false; |
|
| 177 | - } catch (\Throwable $e) { |
|
| 178 | - trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-17 ResponseFactory is available', get_class($e), $e->getMessage()), E_USER_WARNING); |
|
| 166 | + /** |
|
| 167 | + * Can be used as a condition. |
|
| 168 | + * |
|
| 169 | + * @return bool |
|
| 170 | + */ |
|
| 171 | + public static function isPsr17FactoryInstalled() |
|
| 172 | + { |
|
| 173 | + try { |
|
| 174 | + Psr17FactoryDiscovery::findResponseFactory(); |
|
| 175 | + } catch (NotFoundException $e) { |
|
| 176 | + return false; |
|
| 177 | + } catch (\Throwable $e) { |
|
| 178 | + trigger_error(sprintf('Got exception "%s (%s)" while checking if a PSR-17 ResponseFactory is available', get_class($e), $e->getMessage()), E_USER_WARNING); |
|
| 179 | 179 | |
| 180 | - return false; |
|
| 181 | - } |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - return true; |
|
| 184 | - } |
|
| 183 | + return true; |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -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 | */ |
@@ -19,72 +19,72 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class PuliBetaStrategy implements DiscoveryStrategy |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var GeneratedPuliFactory |
|
| 24 | - */ |
|
| 25 | - protected static $puliFactory; |
|
| 22 | + /** |
|
| 23 | + * @var GeneratedPuliFactory |
|
| 24 | + */ |
|
| 25 | + protected static $puliFactory; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var Discovery |
|
| 29 | - */ |
|
| 30 | - protected static $puliDiscovery; |
|
| 27 | + /** |
|
| 28 | + * @var Discovery |
|
| 29 | + */ |
|
| 30 | + protected static $puliDiscovery; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @return GeneratedPuliFactory |
|
| 34 | - * |
|
| 35 | - * @throws PuliUnavailableException |
|
| 36 | - */ |
|
| 37 | - private static function getPuliFactory() |
|
| 38 | - { |
|
| 39 | - if (null === self::$puliFactory) { |
|
| 40 | - if (!defined('PULI_FACTORY_CLASS')) { |
|
| 41 | - throw new PuliUnavailableException('Puli Factory is not available'); |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * @return GeneratedPuliFactory |
|
| 34 | + * |
|
| 35 | + * @throws PuliUnavailableException |
|
| 36 | + */ |
|
| 37 | + private static function getPuliFactory() |
|
| 38 | + { |
|
| 39 | + if (null === self::$puliFactory) { |
|
| 40 | + if (!defined('PULI_FACTORY_CLASS')) { |
|
| 41 | + throw new PuliUnavailableException('Puli Factory is not available'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - $puliFactoryClass = PULI_FACTORY_CLASS; |
|
| 44 | + $puliFactoryClass = PULI_FACTORY_CLASS; |
|
| 45 | 45 | |
| 46 | - if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
|
| 47 | - throw new PuliUnavailableException('Puli Factory class does not exist'); |
|
| 48 | - } |
|
| 46 | + if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
|
| 47 | + throw new PuliUnavailableException('Puli Factory class does not exist'); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - self::$puliFactory = new $puliFactoryClass(); |
|
| 51 | - } |
|
| 50 | + self::$puliFactory = new $puliFactoryClass(); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - return self::$puliFactory; |
|
| 54 | - } |
|
| 53 | + return self::$puliFactory; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Returns the Puli discovery layer. |
|
| 58 | - * |
|
| 59 | - * @return Discovery |
|
| 60 | - * |
|
| 61 | - * @throws PuliUnavailableException |
|
| 62 | - */ |
|
| 63 | - private static function getPuliDiscovery() |
|
| 64 | - { |
|
| 65 | - if (!isset(self::$puliDiscovery)) { |
|
| 66 | - $factory = self::getPuliFactory(); |
|
| 67 | - $repository = $factory->createRepository(); |
|
| 56 | + /** |
|
| 57 | + * Returns the Puli discovery layer. |
|
| 58 | + * |
|
| 59 | + * @return Discovery |
|
| 60 | + * |
|
| 61 | + * @throws PuliUnavailableException |
|
| 62 | + */ |
|
| 63 | + private static function getPuliDiscovery() |
|
| 64 | + { |
|
| 65 | + if (!isset(self::$puliDiscovery)) { |
|
| 66 | + $factory = self::getPuliFactory(); |
|
| 67 | + $repository = $factory->createRepository(); |
|
| 68 | 68 | |
| 69 | - self::$puliDiscovery = $factory->createDiscovery($repository); |
|
| 70 | - } |
|
| 69 | + self::$puliDiscovery = $factory->createDiscovery($repository); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return self::$puliDiscovery; |
|
| 73 | - } |
|
| 72 | + return self::$puliDiscovery; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public static function getCandidates($type) |
|
| 76 | - { |
|
| 77 | - $returnData = []; |
|
| 78 | - $bindings = self::getPuliDiscovery()->findBindings($type); |
|
| 75 | + public static function getCandidates($type) |
|
| 76 | + { |
|
| 77 | + $returnData = []; |
|
| 78 | + $bindings = self::getPuliDiscovery()->findBindings($type); |
|
| 79 | 79 | |
| 80 | - foreach ($bindings as $binding) { |
|
| 81 | - $condition = true; |
|
| 82 | - if ($binding->hasParameterValue('depends')) { |
|
| 83 | - $condition = $binding->getParameterValue('depends'); |
|
| 84 | - } |
|
| 85 | - $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
|
| 86 | - } |
|
| 80 | + foreach ($bindings as $binding) { |
|
| 81 | + $condition = true; |
|
| 82 | + if ($binding->hasParameterValue('depends')) { |
|
| 83 | + $condition = $binding->getParameterValue('depends'); |
|
| 84 | + } |
|
| 85 | + $returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - return $returnData; |
|
| 89 | - } |
|
| 88 | + return $returnData; |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -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 | */ |
@@ -9,15 +9,15 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface DiscoveryStrategy |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Find a resource of a specific type. |
|
| 14 | - * |
|
| 15 | - * @param string $type |
|
| 16 | - * |
|
| 17 | - * @return array The return value is always an array with zero or more elements. Each |
|
| 18 | - * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 19 | - * |
|
| 20 | - * @throws StrategyUnavailableException if we cannot use this strategy |
|
| 21 | - */ |
|
| 22 | - public static function getCandidates($type); |
|
| 12 | + /** |
|
| 13 | + * Find a resource of a specific type. |
|
| 14 | + * |
|
| 15 | + * @param string $type |
|
| 16 | + * |
|
| 17 | + * @return array The return value is always an array with zero or more elements. Each |
|
| 18 | + * element is an array with two keys ['class' => string, 'condition' => mixed]. |
|
| 19 | + * |
|
| 20 | + * @throws StrategyUnavailableException if we cannot use this strategy |
|
| 21 | + */ |
|
| 22 | + public static function getCandidates($type); |
|
| 23 | 23 | } |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * @author Tobias Nyholm <[email protected]> |
| 9 | 9 | */ |
| 10 | -interface DiscoveryStrategy |
|
| 11 | -{ |
|
| 10 | +interface DiscoveryStrategy { |
|
| 12 | 11 | /** |
| 13 | 12 | * Find a resource of a specific type. |
| 14 | 13 | * |