class Client

Client simulates a browser and makes requests to a Kernel object.

@author Fabien Potencier <fabien@symfony.com>

@api

Hierarchy

  • class \Symfony\Component\HttpKernel\Client extends \Symfony\Component\BrowserKit\Client

Expanded class hierarchy of Client

2 files declare their use of Client
ClientTest.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/ClientTest.php
TestClient.php in drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/Fixtures/TestClient.php

File

drupal/core/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php, line 31

Namespace

Symfony\Component\HttpKernel
View source
class Client extends BaseClient {
  protected $kernel;

  /**
   * Constructor.
   *
   * @param HttpKernelInterface $kernel    An HttpKernel instance
   * @param array               $server    The server parameters (equivalent of $_SERVER)
   * @param History             $history   A History instance to store the browser history
   * @param CookieJar           $cookieJar A CookieJar instance to store the cookies
   */
  public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null) {
    $this->kernel = $kernel;
    parent::__construct($server, $history, $cookieJar);
    $this->followRedirects = false;
  }

  /**
   * Makes a request.
   *
   * @param Request $request A Request instance
   *
   * @return Response A Response instance
   */
  protected function doRequest($request) {
    $response = $this->kernel
      ->handle($request);
    if ($this->kernel instanceof TerminableInterface) {
      $this->kernel
        ->terminate($request, $response);
    }
    return $response;
  }

  /**
   * Returns the script to execute when the request must be insulated.
   *
   * @param Request $request A Request instance
   */
  protected function getScript($request) {
    $kernel = str_replace("'", "\\'", serialize($this->kernel));
    $request = str_replace("'", "\\'", serialize($request));
    $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\UniversalClassLoader');
    $requirePath = str_replace("'", "\\'", $r
      ->getFileName());
    $symfonyPath = str_replace("'", "\\'", realpath(__DIR__ . '/../../..'));
    return <<<EOF
<?php

require_once '{<span class="php-variable">$requirePath</span>}';

\$loader = new Symfony\\Component\\ClassLoader\\UniversalClassLoader();
\$loader->registerNamespaces(array('Symfony' => '{<span class="php-variable">$symfonyPath</span>}'));
\$loader->register();

\$kernel = unserialize('{<span class="php-variable">$kernel</span>}');
echo serialize(\$kernel->handle(unserialize('{<span class="php-variable">$request</span>}')));
EOF;
  }

  /**
   * Converts the BrowserKit request to a HttpKernel request.
   *
   * @param DomRequest $request A Request instance
   *
   * @return Request A Request instance
   */
  protected function filterRequest(DomRequest $request) {
    $httpRequest = Request::create($request
      ->getUri(), $request
      ->getMethod(), $request
      ->getParameters(), $request
      ->getCookies(), $request
      ->getFiles(), $request
      ->getServer(), $request
      ->getContent());
    $httpRequest->files
      ->replace($this
      ->filterFiles($httpRequest->files
      ->all()));
    return $httpRequest;
  }

  /**
   * Filters an array of files.
   *
   * This method created test instances of UploadedFile so that the move()
   * method can be called on those instances.
   *
   * If the size of a file is greater than the allowed size (from php.ini) then
   * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
   *
   * @see Symfony\Component\HttpFoundation\File\UploadedFile
   *
   * @param array $files An array of files
   *
   * @return array An array with all uploaded files marked as already moved
   */
  protected function filterFiles(array $files) {
    $filtered = array();
    foreach ($files as $key => $value) {
      if (is_array($value)) {
        $filtered[$key] = $this
          ->filterFiles($value);
      }
      elseif ($value instanceof UploadedFile) {
        if ($value
          ->isValid() && $value
          ->getSize() > UploadedFile::getMaxFilesize()) {
          $filtered[$key] = new UploadedFile('', $value
            ->getClientOriginalName(), $value
            ->getClientMimeType(), 0, UPLOAD_ERR_INI_SIZE, true);
        }
        else {
          $filtered[$key] = new UploadedFile($value
            ->getPathname(), $value
            ->getClientOriginalName(), $value
            ->getClientMimeType(), $value
            ->getClientSize(), $value
            ->getError(), true);
        }
      }
      else {
        $filtered[$key] = $value;
      }
    }
    return $filtered;
  }

  /**
   * Converts the HttpKernel response to a BrowserKit response.
   *
   * @param Response $response A Response instance
   *
   * @return Response A Response instance
   */
  protected function filterResponse($response) {
    $headers = $response->headers
      ->all();
    if ($response->headers
      ->getCookies()) {
      $cookies = array();
      foreach ($response->headers
        ->getCookies() as $cookie) {
        $cookies[] = new DomCookie($cookie
          ->getName(), $cookie
          ->getValue(), $cookie
          ->getExpiresTime(), $cookie
          ->getPath(), $cookie
          ->getDomain(), $cookie
          ->isSecure(), $cookie
          ->isHttpOnly());
      }
      $headers['Set-Cookie'] = $cookies;
    }
    return new DomResponse($response
      ->getContent(), $response
      ->getStatusCode(), $headers);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Client::$kernel protected property
Client::doRequest protected function Makes a request.
Client::filterFiles protected function Filters an array of files.
Client::filterRequest protected function Converts the BrowserKit request to a HttpKernel request.
Client::filterResponse protected function Converts the HttpKernel response to a BrowserKit response.
Client::getScript protected function Returns the script to execute when the request must be insulated. 1
Client::__construct public function Constructor.