You are here

class DiactorosFactory in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/psr-http-message-bridge/Factory/DiactorosFactory.php \Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory

Builds Psr\HttpMessage instances using the Zend Diactoros implementation.

@author Kévin Dunglas <dunglas@gmail.com>

Hierarchy

Expanded class hierarchy of DiactorosFactory

2 files declare their use of DiactorosFactory
ControllerResolverTest.php in core/tests/Drupal/Tests/Core/Controller/ControllerResolverTest.php
Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.
DiactorosFactoryTest.php in vendor/symfony/psr-http-message-bridge/Tests/Factory/DiactorosFactoryTest.php
1 string reference to 'DiactorosFactory'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DiactorosFactory
psr7.http_message_factory in core/core.services.yml
Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory

File

vendor/symfony/psr-http-message-bridge/Factory/DiactorosFactory.php, line 31

Namespace

Symfony\Bridge\PsrHttpMessage\Factory
View source
class DiactorosFactory implements HttpMessageFactoryInterface {
  public function __construct() {
    if (!class_exists('Zend\\Diactoros\\ServerRequestFactory')) {
      throw new \RuntimeException('Zend Diactoros must be installed to use the DiactorosFactory.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function createRequest(Request $symfonyRequest) {
    $server = DiactorosRequestFactory::normalizeServer($symfonyRequest->server
      ->all());
    $headers = $symfonyRequest->headers
      ->all();
    try {
      $body = new DiactorosStream($symfonyRequest
        ->getContent(true));
    } catch (\LogicException $e) {
      $body = new DiactorosStream('php://temp', 'wb+');
      $body
        ->write($symfonyRequest
        ->getContent());
    }
    $request = new ServerRequest($server, DiactorosRequestFactory::normalizeFiles($this
      ->getFiles($symfonyRequest->files
      ->all())), $symfonyRequest
      ->getUri(), $symfonyRequest
      ->getMethod(), $body, $headers);
    $request = $request
      ->withCookieParams($symfonyRequest->cookies
      ->all())
      ->withQueryParams($symfonyRequest->query
      ->all())
      ->withParsedBody($symfonyRequest->request
      ->all());
    foreach ($symfonyRequest->attributes
      ->all() as $key => $value) {
      $request = $request
        ->withAttribute($key, $value);
    }
    return $request;
  }

  /**
   * Converts Symfony uploaded files array to the PSR one.
   *
   * @param array $uploadedFiles
   *
   * @return array
   */
  private function getFiles(array $uploadedFiles) {
    $files = array();
    foreach ($uploadedFiles as $key => $value) {
      if ($value instanceof UploadedFile) {
        $files[$key] = $this
          ->createUploadedFile($value);
      }
      else {
        $files[$key] = $this
          ->getFiles($value);
      }
    }
    return $files;
  }

  /**
   * Creates a PSR-7 UploadedFile instance from a Symfony one.
   *
   * @param UploadedFile $symfonyUploadedFile
   *
   * @return UploadedFileInterface
   */
  private function createUploadedFile(UploadedFile $symfonyUploadedFile) {
    return new DiactorosUploadedFile($symfonyUploadedFile
      ->getRealPath(), $symfonyUploadedFile
      ->getSize(), $symfonyUploadedFile
      ->getError(), $symfonyUploadedFile
      ->getClientOriginalName(), $symfonyUploadedFile
      ->getClientMimeType());
  }

  /**
   * {@inheritdoc}
   */
  public function createResponse(Response $symfonyResponse) {
    if ($symfonyResponse instanceof BinaryFileResponse) {
      $stream = new DiactorosStream($symfonyResponse
        ->getFile()
        ->getPathname(), 'r');
    }
    else {
      $stream = new DiactorosStream('php://temp', 'wb+');
      if ($symfonyResponse instanceof StreamedResponse) {
        ob_start(function ($buffer) use ($stream) {
          $stream
            ->write($buffer);
          return false;
        });
        $symfonyResponse
          ->sendContent();
        ob_end_clean();
      }
      else {
        $stream
          ->write($symfonyResponse
          ->getContent());
      }
    }
    $headers = $symfonyResponse->headers
      ->all();
    $cookies = $symfonyResponse->headers
      ->getCookies();
    if (!empty($cookies)) {
      $headers['Set-Cookie'] = array();
      foreach ($cookies as $cookie) {
        $headers['Set-Cookie'][] = $cookie
          ->__toString();
      }
    }
    $response = new DiactorosResponse($stream, $symfonyResponse
      ->getStatusCode(), $headers);
    $protocolVersion = $symfonyResponse
      ->getProtocolVersion();
    if ('1.1' !== $protocolVersion) {
      $response = $response
        ->withProtocolVersion($protocolVersion);
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiactorosFactory::createRequest public function Creates a PSR-7 Request instance from a Symfony one. Overrides HttpMessageFactoryInterface::createRequest
DiactorosFactory::createResponse public function Creates a PSR-7 Response instance from a Symfony one. Overrides HttpMessageFactoryInterface::createResponse
DiactorosFactory::createUploadedFile private function Creates a PSR-7 UploadedFile instance from a Symfony one.
DiactorosFactory::getFiles private function Converts Symfony uploaded files array to the PSR one.
DiactorosFactory::__construct public function