You are here

class Client in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/http-kernel/Client.php \Symfony\Component\HttpKernel\Client
  2. 8 vendor/symfony/browser-kit/Client.php \Symfony\Component\BrowserKit\Client
  3. 8 vendor/fabpot/goutte/Goutte/Client.php \Goutte\Client
  4. 8 vendor/guzzlehttp/guzzle/src/Client.php \GuzzleHttp\Client
  5. 8 vendor/behat/mink-goutte-driver/src/Goutte/Client.php \Behat\Mink\Driver\Goutte\Client
Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/Client.php \Symfony\Component\HttpKernel\Client

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

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

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

Expanded class hierarchy of Client

5 files declare their use of Client
BaseUrlTest.php in vendor/behat/mink-browserkit-driver/tests/Custom/BaseUrlTest.php
BrowserKitConfig.php in vendor/behat/mink-browserkit-driver/tests/BrowserKitConfig.php
BrowserKitDriver.php in vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php
ClientTest.php in vendor/symfony/http-kernel/Tests/ClientTest.php
TestClient.php in vendor/symfony/http-kernel/Tests/Fixtures/TestClient.php

File

vendor/symfony/http-kernel/Client.php, line 29

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) {

    // These class properties must be set before calling the parent constructor, as it may depend on it.
    $this->kernel = $kernel;
    $this->followRedirects = false;
    parent::__construct($server, $history, $cookieJar);
  }

  /**
   * {@inheritdoc}
   *
   * @return Request|null A Request instance
   */
  public function getRequest() {
    return parent::getRequest();
  }

  /**
   * {@inheritdoc}
   *
   * @return Response|null A Response instance
   */
  public function getResponse() {
    return parent::getResponse();
  }

  /**
   * 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
   *
   * @return string
   */
  protected function getScript($request) {
    $kernel = str_replace("'", "\\'", serialize($this->kernel));
    $request = str_replace("'", "\\'", serialize($request));
    $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
    $requirePath = str_replace("'", "\\'", $r
      ->getFileName());
    $symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
    $errorReporting = error_reporting();
    $code = <<<EOF
<?php

error_reporting({<span class="php-variable">$errorReporting</span>} & ~E_USER_DEPRECATED);

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

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

\$kernel = unserialize('{<span class="php-variable">$kernel</span>}');
\$request = unserialize('{<span class="php-variable">$request</span>}');
EOF;
    return $code . $this
      ->getHandleScript();
  }
  protected function getHandleScript() {
    return <<<'EOF'
$response = $kernel->handle($request);

if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
    $kernel->terminate($request, $response);
}

echo serialize($response);
EOF;
  }

  /**
   * Converts the BrowserKit request to a HttpKernel request.
   *
   * @param DomRequest $request A DomRequest 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());
    foreach ($this
      ->filterFiles($httpRequest->files
      ->all()) as $key => $value) {
      $httpRequest->files
        ->set($key, $value);
    }
    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 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);
        }
      }
    }
    return $filtered;
  }

  /**
   * Converts the HttpKernel response to a BrowserKit response.
   *
   * @param Response $response A Response instance
   *
   * @return DomResponse A DomResponse 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;
    }

    // this is needed to support StreamedResponse
    ob_start();
    $response
      ->sendContent();
    $content = ob_get_clean();
    return new DomResponse($content, $response
      ->getStatusCode(), $headers);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Client::$cookieJar protected property
Client::$crawler protected property
Client::$followRedirects protected property
Client::$history protected property
Client::$insulated protected property
Client::$internalRequest protected property
Client::$internalResponse protected property
Client::$isMainRequest private property
Client::$kernel protected property
Client::$maxRedirects private property
Client::$redirect protected property
Client::$redirectCount private property
Client::$request protected property
Client::$response protected property
Client::$server protected property
Client::back public function Goes back in the browser history.
Client::click public function Clicks on a given link.
Client::createCrawlerFromContent protected function Creates a crawler.
Client::doRequest protected function Makes a request. Overrides Client::doRequest
Client::doRequestInProcess protected function Makes a request in another process.
Client::extractHost private function
Client::filterFiles protected function Filters an array of files.
Client::filterRequest protected function Converts the BrowserKit request to a HttpKernel request. Overrides Client::filterRequest
Client::filterResponse protected function Converts the HttpKernel response to a BrowserKit response. Overrides Client::filterResponse
Client::followRedirect public function Follow redirects?
Client::followRedirects public function Sets whether to automatically follow redirects or not.
Client::forward public function Goes forward in the browser history.
Client::getAbsoluteUri protected function Takes a URI and converts it to absolute if it is not already absolute.
Client::getCookieJar public function Returns the CookieJar instance.
Client::getCrawler public function Returns the current Crawler instance.
Client::getHandleScript protected function
Client::getHistory public function Returns the History instance.
Client::getInternalRequest public function Returns the current BrowserKit Request instance.
Client::getInternalResponse public function Returns the current BrowserKit Response instance.
Client::getRequest public function Overrides Client::getRequest
Client::getResponse public function Overrides Client::getResponse
Client::getScript protected function Returns the script to execute when the request must be insulated. Overrides Client::getScript
Client::getServerParameter public function Gets single server parameter for specified key.
Client::insulate public function Sets the insulated flag.
Client::reload public function Reloads the current browser.
Client::request public function Calls a URI.
Client::requestFromRequest protected function Makes a request from a Request object directly.
Client::restart public function Restarts the client.
Client::setMaxRedirects public function Sets the maximum number of requests that crawler can follow.
Client::setServerParameter public function Sets single server parameter.
Client::setServerParameters public function Sets server parameters.
Client::submit public function Submits a form.
Client::updateServerFromUri private function
Client::__construct public function Constructor. Overrides Client::__construct