|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespaceSymfony\Component\HttpKernel; |
| 13 | + |
| 14 | +usePsr\Log\LoggerInterface; |
| 15 | +usePsr\Log\NullLogger; |
| 16 | +useSymfony\Component\HttpClient\HttpClient; |
| 17 | +useSymfony\Component\HttpFoundation\Request; |
| 18 | +useSymfony\Component\HttpFoundation\Response; |
| 19 | +useSymfony\Component\Mime\Part\AbstractPart; |
| 20 | +useSymfony\Component\Mime\Part\DataPart; |
| 21 | +useSymfony\Component\Mime\Part\Multipart\FormDataPart; |
| 22 | +useSymfony\Component\Mime\Part\TextPart; |
| 23 | +useSymfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * An implementation of a Symfony HTTP kernel using a "real" HTTP client. |
| 27 | + * |
| 28 | + * @author Fabien Potencier <fabien@symfony.com> |
| 29 | + */ |
| 30 | +finalclass RealHttpKernelimplements HttpKernelInterface |
| 31 | +{ |
| 32 | +private$client; |
| 33 | +private$logger; |
| 34 | + |
| 35 | +publicfunction__construct(HttpClientInterface$client =null,LoggerInterface$logger =null) |
| 36 | + { |
| 37 | +if (!class_exists(HttpClient::class)) { |
| 38 | +thrownew \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".',__CLASS__)); |
| 39 | + } |
| 40 | + |
| 41 | +$this->client =$client ?? HttpClient::create(); |
| 42 | +$this->logger =$logger ??newNullLogger(); |
| 43 | + } |
| 44 | + |
| 45 | +publicfunctionhandle(Request$request,$type = HttpKernelInterface::MASTER_REQUEST,$catch =true) |
| 46 | + { |
| 47 | +$this->logger->debug(sprintf('Request: %s %s',$request->getMethod(),$request->getUri())); |
| 48 | + |
| 49 | +$headers =$this->getHeaders($request); |
| 50 | +$body =''; |
| 51 | +if (null !==$part =$this->getBody($request)) { |
| 52 | +$headers =array_merge($headers,$part->getPreparedHeaders()->toArray()); |
| 53 | +$body =$part->bodyToIterable(); |
| 54 | + } |
| 55 | +$response =$this->client->request($request->getMethod(),$request->getUri(), [ |
| 56 | +'headers' =>$headers, |
| 57 | +'body' =>$body, |
| 58 | +'max_redirects' =>0, |
| 59 | + ] +$request->attributes->get('http_client_options', [])); |
| 60 | + |
| 61 | +$this->logger->debug(sprintf('Response: %s %s',$response->getStatusCode(),$request->getUri())); |
| 62 | + |
| 63 | +returnnewResponse($response->getContent(!$catch),$response->getStatusCode(),$response->getHeaders(!$catch)); |
| 64 | + } |
| 65 | + |
| 66 | +privatefunctiongetBody(Request$request): ?AbstractPart |
| 67 | + { |
| 68 | +if (\in_array($request->getMethod(), ['GET','HEAD'])) { |
| 69 | +returnnull; |
| 70 | + } |
| 71 | + |
| 72 | +if (!class_exists(AbstractPart::class)) { |
| 73 | +thrownew \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 74 | + } |
| 75 | + |
| 76 | +if ($content =$request->getContent()) { |
| 77 | +returnnewTextPart($content,'utf-8','plain','8bit'); |
| 78 | + } |
| 79 | + |
| 80 | +$fields =$request->request->all(); |
| 81 | +foreach ($request->files->all()as$name =>$file) { |
| 82 | +$fields[$name] = DataPart::fromPath($file->getPathname(),$file->getClientOriginalName(),$file->getClientMimeType()); |
| 83 | + } |
| 84 | + |
| 85 | +returnnewFormDataPart($fields); |
| 86 | + } |
| 87 | + |
| 88 | +privatefunctiongetHeaders(Request$request):array |
| 89 | + { |
| 90 | +$headers = []; |
| 91 | +foreach ($request->headersas$key =>$value) { |
| 92 | +$headers[$key] =$value; |
| 93 | + } |
| 94 | +$cookies = []; |
| 95 | +foreach ($request->cookies->all()as$name =>$value) { |
| 96 | +$cookies[] =$name.'='.$value; |
| 97 | + } |
| 98 | +if ($cookies) { |
| 99 | +$headers['cookie'] =implode(';',$cookies); |
| 100 | + } |
| 101 | + |
| 102 | +return$headers; |
| 103 | + } |
| 104 | +} |