4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel;
14 use Symfony\Component\BrowserKit\Client as BaseClient;
15 use Symfony\Component\BrowserKit\Request as DomRequest;
16 use Symfony\Component\BrowserKit\Response as DomResponse;
17 use Symfony\Component\BrowserKit\History;
18 use Symfony\Component\BrowserKit\CookieJar;
19 use Symfony\Component\HttpFoundation\File\UploadedFile;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
24 * Client simulates a browser and makes requests to a Kernel object.
26 * @author Fabien Potencier <fabien@symfony.com>
28 * @method Request|null getRequest() A Request instance
29 * @method Response|null getResponse() A Response instance
31 class Client extends BaseClient
34 private $catchExceptions = true;
37 * @param HttpKernelInterface $kernel An HttpKernel instance
38 * @param array $server The server parameters (equivalent of $_SERVER)
39 * @param History $history A History instance to store the browser history
40 * @param CookieJar $cookieJar A CookieJar instance to store the cookies
42 public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
44 // These class properties must be set before calling the parent constructor, as it may depend on it.
45 $this->kernel = $kernel;
46 $this->followRedirects = false;
48 parent::__construct($server, $history, $cookieJar);
52 * Sets whether to catch exceptions when the kernel is handling a request.
54 * @param bool $catchExceptions Whether to catch exceptions
56 public function catchExceptions($catchExceptions)
58 $this->catchExceptions = $catchExceptions;
64 * @return Response A Response instance
66 protected function doRequest($request)
68 $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
70 if ($this->kernel instanceof TerminableInterface) {
71 $this->kernel->terminate($request, $response);
78 * Returns the script to execute when the request must be insulated.
82 protected function getScript($request)
84 $kernel = str_replace("'", "\\'", serialize($this->kernel));
85 $request = str_replace("'", "\\'", serialize($request));
86 $errorReporting = error_reporting();
89 foreach (get_declared_classes() as $class) {
90 if (0 === strpos($class, 'ComposerAutoloaderInit')) {
91 $r = new \ReflectionClass($class);
92 $file = dirname(dirname($r->getFileName())).'/autoload.php';
93 if (file_exists($file)) {
94 $requires .= "require_once '".str_replace("'", "\\'", $file)."';\n";
100 throw new \RuntimeException('Composer autoloader not found.');
106 error_reporting($errorReporting);
110 \$kernel = unserialize('$kernel');
111 \$request = unserialize('$request');
114 return $code.$this->getHandleScript();
117 protected function getHandleScript()
120 $response = $kernel->handle($request);
122 if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
123 $kernel->terminate($request, $response);
126 echo serialize($response);
131 * Converts the BrowserKit request to a HttpKernel request.
133 * @return Request A Request instance
135 protected function filterRequest(DomRequest $request)
137 $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
139 foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
140 $httpRequest->files->set($key, $value);
147 * Filters an array of files.
149 * This method created test instances of UploadedFile so that the move()
150 * method can be called on those instances.
152 * If the size of a file is greater than the allowed size (from php.ini) then
153 * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
157 * @return array An array with all uploaded files marked as already moved
159 protected function filterFiles(array $files)
162 foreach ($files as $key => $value) {
163 if (is_array($value)) {
164 $filtered[$key] = $this->filterFiles($value);
165 } elseif ($value instanceof UploadedFile) {
166 if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
167 $filtered[$key] = new UploadedFile(
169 $value->getClientOriginalName(),
170 $value->getClientMimeType(),
176 $filtered[$key] = new UploadedFile(
177 $value->getPathname(),
178 $value->getClientOriginalName(),
179 $value->getClientMimeType(),
180 $value->getClientSize(),
192 * Converts the HttpKernel response to a BrowserKit response.
194 * @return DomResponse A DomResponse instance
196 protected function filterResponse($response)
198 // this is needed to support StreamedResponse
200 $response->sendContent();
201 $content = ob_get_clean();
203 return new DomResponse($content, $response->getStatusCode(), $response->headers->all());