You are here

class HttpFoundationFactory in Zircon Profile 8

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

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

Hierarchy

Expanded class hierarchy of HttpFoundationFactory

1 file declares its use of HttpFoundationFactory
HttpFoundationFactoryTest.php in vendor/symfony/psr-http-message-bridge/Tests/Factory/HttpFoundationFactoryTest.php
1 string reference to 'HttpFoundationFactory'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses HttpFoundationFactory
psr7.http_foundation_factory in core/core.services.yml
Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory

File

vendor/symfony/psr-http-message-bridge/Factory/HttpFoundationFactory.php, line 28

Namespace

Symfony\Bridge\PsrHttpMessage\Factory
View source
class HttpFoundationFactory implements HttpFoundationFactoryInterface {

  /**
   * {@inheritdoc}
   */
  public function createRequest(ServerRequestInterface $psrRequest) {
    $parsedBody = $psrRequest
      ->getParsedBody();
    $parsedBody = is_array($parsedBody) ? $parsedBody : array();
    $request = new Request($psrRequest
      ->getQueryParams(), $parsedBody, $psrRequest
      ->getAttributes(), $psrRequest
      ->getCookieParams(), $this
      ->getFiles($psrRequest
      ->getUploadedFiles()), $psrRequest
      ->getServerParams(), $psrRequest
      ->getBody()
      ->__toString());
    $request->headers
      ->replace($psrRequest
      ->getHeaders());
    return $request;
  }

  /**
   * Converts to the input array to $_FILES structure.
   *
   * @param array $uploadedFiles
   *
   * @return array
   */
  private function getFiles(array $uploadedFiles) {
    $files = array();
    foreach ($uploadedFiles as $key => $value) {
      if ($value instanceof UploadedFileInterface) {
        $files[$key] = $this
          ->createUploadedFile($value);
      }
      else {
        $files[$key] = $this
          ->getFiles($value);
      }
    }
    return $files;
  }

  /**
   * Creates Symfony UploadedFile instance from PSR-7 ones.
   *
   * @param UploadedFileInterface $psrUploadedFile
   *
   * @return UploadedFile
   */
  private function createUploadedFile(UploadedFileInterface $psrUploadedFile) {
    $temporaryPath = $this
      ->getTemporaryPath();
    $psrUploadedFile
      ->moveTo($temporaryPath);
    $clientFileName = $psrUploadedFile
      ->getClientFilename();
    return new UploadedFile($temporaryPath, null === $clientFileName ? '' : $clientFileName, $psrUploadedFile
      ->getClientMediaType(), $psrUploadedFile
      ->getSize(), $psrUploadedFile
      ->getError(), true);
  }

  /**
   * Gets a temporary file path.
   *
   * @return string
   */
  protected function getTemporaryPath() {
    return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
  }

  /**
   * {@inheritdoc}
   */
  public function createResponse(ResponseInterface $psrResponse) {
    $response = new Response($psrResponse
      ->getBody()
      ->__toString(), $psrResponse
      ->getStatusCode(), $psrResponse
      ->getHeaders());
    $response
      ->setProtocolVersion($psrResponse
      ->getProtocolVersion());
    foreach ($psrResponse
      ->getHeader('Set-Cookie') as $cookie) {
      $response->headers
        ->setCookie($this
        ->createCookie($cookie));
    }
    return $response;
  }

  /**
   * Creates a Cookie instance from a cookie string.
   *
   * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
   *
   * @param string $cookie
   *
   * @return Cookie
   *
   * @throws \InvalidArgumentException
   */
  private function createCookie($cookie) {
    foreach (explode(';', $cookie) as $part) {
      $part = trim($part);
      $data = explode('=', $part, 2);
      $name = $data[0];
      $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\v\"") : null;
      if (!isset($cookieName)) {
        $cookieName = $name;
        $cookieValue = $value;
        continue;
      }
      if ('expires' === strtolower($name) && null !== $value) {
        $cookieExpire = new \DateTime($value);
        continue;
      }
      if ('path' === strtolower($name) && null !== $value) {
        $cookiePath = $value;
        continue;
      }
      if ('domain' === strtolower($name) && null !== $value) {
        $cookieDomain = $value;
        continue;
      }
      if ('secure' === strtolower($name)) {
        $cookieSecure = true;
        continue;
      }
      if ('httponly' === strtolower($name)) {
        $cookieHttpOnly = true;
        continue;
      }
    }
    if (!isset($cookieName)) {
      throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
    }
    return new Cookie($cookieName, $cookieValue, isset($cookieExpire) ? $cookieExpire : 0, isset($cookiePath) ? $cookiePath : '/', isset($cookieDomain) ? $cookieDomain : null, isset($cookieSecure), isset($cookieHttpOnly));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpFoundationFactory::createCookie private function Creates a Cookie instance from a cookie string.
HttpFoundationFactory::createRequest public function Creates a Symfony Request instance from a PSR-7 one. Overrides HttpFoundationFactoryInterface::createRequest
HttpFoundationFactory::createResponse public function Creates a Symfony Response instance from a PSR-7 one. Overrides HttpFoundationFactoryInterface::createResponse
HttpFoundationFactory::createUploadedFile private function Creates Symfony UploadedFile instance from PSR-7 ones.
HttpFoundationFactory::getFiles private function Converts to the input array to $_FILES structure.
HttpFoundationFactory::getTemporaryPath protected function Gets a temporary file path.