Client simulates a browser and makes requests to a Kernel object.
@author Fabien Potencier <fabien@symfony.com>
@api
Expanded class hierarchy of Client
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);
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Client:: |
protected | property | ||
Client:: |
protected | function | Makes a request. | |
Client:: |
protected | function | Filters an array of files. | |
Client:: |
protected | function | Converts the BrowserKit request to a HttpKernel request. | |
Client:: |
protected | function | Converts the HttpKernel response to a BrowserKit response. | |
Client:: |
protected | function | Returns the script to execute when the request must be insulated. | 1 |
Client:: |
public | function | Constructor. |