vendor/symfony/psr-http-message-bridge/Factory/PsrHttpFactory.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\PsrHttpMessage\Factory;
  11. use Psr\Http\Message\ResponseFactoryInterface;
  12. use Psr\Http\Message\ServerRequestFactoryInterface;
  13. use Psr\Http\Message\StreamFactoryInterface;
  14. use Psr\Http\Message\UploadedFileFactoryInterface;
  15. use Psr\Http\Message\UploadedFileInterface;
  16. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  17. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\StreamedResponse;
  22. /**
  23.  * Builds Psr\HttpMessage instances using a PSR-17 implementation.
  24.  *
  25.  * @author Antonio J. GarcĂ­a Lagar <aj@garcialagar.es>
  26.  */
  27. class PsrHttpFactory implements HttpMessageFactoryInterface
  28. {
  29.     private $serverRequestFactory;
  30.     private $streamFactory;
  31.     private $uploadedFileFactory;
  32.     private $responseFactory;
  33.     public function __construct(ServerRequestFactoryInterface $serverRequestFactoryStreamFactoryInterface $streamFactoryUploadedFileFactoryInterface $uploadedFileFactoryResponseFactoryInterface $responseFactory)
  34.     {
  35.         $this->serverRequestFactory $serverRequestFactory;
  36.         $this->streamFactory $streamFactory;
  37.         $this->uploadedFileFactory $uploadedFileFactory;
  38.         $this->responseFactory $responseFactory;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function createRequest(Request $symfonyRequest)
  44.     {
  45.         $uri $symfonyRequest->server->get('QUERY_STRING''');
  46.         $uri $symfonyRequest->getSchemeAndHttpHost().$symfonyRequest->getBaseUrl().$symfonyRequest->getPathInfo().('' !== $uri '?'.$uri '');
  47.         $request $this->serverRequestFactory->createServerRequest(
  48.             $symfonyRequest->getMethod(),
  49.             $uri,
  50.             $symfonyRequest->server->all()
  51.         );
  52.         foreach ($symfonyRequest->headers->all() as $name => $value) {
  53.             $request $request->withHeader($name$value);
  54.         }
  55.         $body $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));
  56.         $request $request
  57.             ->withBody($body)
  58.             ->withUploadedFiles($this->getFiles($symfonyRequest->files->all()))
  59.             ->withCookieParams($symfonyRequest->cookies->all())
  60.             ->withQueryParams($symfonyRequest->query->all())
  61.             ->withParsedBody($symfonyRequest->request->all())
  62.         ;
  63.         foreach ($symfonyRequest->attributes->all() as $key => $value) {
  64.             $request $request->withAttribute($key$value);
  65.         }
  66.         return $request;
  67.     }
  68.     /**
  69.      * Converts Symfony uploaded files array to the PSR one.
  70.      *
  71.      * @return array
  72.      */
  73.     private function getFiles(array $uploadedFiles)
  74.     {
  75.         $files = [];
  76.         foreach ($uploadedFiles as $key => $value) {
  77.             if (null === $value) {
  78.                 $files[$key] = $this->uploadedFileFactory->createUploadedFile($this->streamFactory->createStream(), 0, \UPLOAD_ERR_NO_FILE);
  79.                 continue;
  80.             }
  81.             if ($value instanceof UploadedFile) {
  82.                 $files[$key] = $this->createUploadedFile($value);
  83.             } else {
  84.                 $files[$key] = $this->getFiles($value);
  85.             }
  86.         }
  87.         return $files;
  88.     }
  89.     /**
  90.      * Creates a PSR-7 UploadedFile instance from a Symfony one.
  91.      *
  92.      * @return UploadedFileInterface
  93.      */
  94.     private function createUploadedFile(UploadedFile $symfonyUploadedFile)
  95.     {
  96.         return $this->uploadedFileFactory->createUploadedFile(
  97.             $this->streamFactory->createStreamFromFile(
  98.                 $symfonyUploadedFile->getRealPath()
  99.             ),
  100.             (int) $symfonyUploadedFile->getSize(),
  101.             $symfonyUploadedFile->getError(),
  102.             $symfonyUploadedFile->getClientOriginalName(),
  103.             $symfonyUploadedFile->getClientMimeType()
  104.         );
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function createResponse(Response $symfonyResponse)
  110.     {
  111.         $response $this->responseFactory->createResponse($symfonyResponse->getStatusCode(), Response::$statusTexts[$symfonyResponse->getStatusCode()] ?? '');
  112.         if ($symfonyResponse instanceof BinaryFileResponse && !$symfonyResponse->headers->has('Content-Range')) {
  113.             $stream $this->streamFactory->createStreamFromFile(
  114.                 $symfonyResponse->getFile()->getPathname()
  115.             );
  116.         } else {
  117.             $stream $this->streamFactory->createStreamFromFile('php://temp''wb+');
  118.             if ($symfonyResponse instanceof StreamedResponse || $symfonyResponse instanceof BinaryFileResponse) {
  119.                 ob_start(function ($buffer) use ($stream) {
  120.                     $stream->write($buffer);
  121.                     return '';
  122.                 });
  123.                 $symfonyResponse->sendContent();
  124.                 ob_end_clean();
  125.             } else {
  126.                 $stream->write($symfonyResponse->getContent());
  127.             }
  128.         }
  129.         $response $response->withBody($stream);
  130.         $headers $symfonyResponse->headers->all();
  131.         $cookies $symfonyResponse->headers->getCookies();
  132.         if (!empty($cookies)) {
  133.             $headers['Set-Cookie'] = [];
  134.             foreach ($cookies as $cookie) {
  135.                 $headers['Set-Cookie'][] = $cookie->__toString();
  136.             }
  137.         }
  138.         foreach ($headers as $name => $value) {
  139.             $response $response->withHeader($name$value);
  140.         }
  141.         $protocolVersion $symfonyResponse->getProtocolVersion();
  142.         $response $response->withProtocolVersion($protocolVersion);
  143.         return $response;
  144.     }
  145. }