use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Request as DomRequest;
use Symfony\Component\BrowserKit\Response as DomResponse;
-use Symfony\Component\BrowserKit\Cookie as DomCookie;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\HttpFoundation\File\UploadedFile;
*
* @author Fabien Potencier <fabien@symfony.com>
*
- * @method Request|null getRequest() A Request instance
+ * @method Request|null getRequest() A Request instance
* @method Response|null getResponse() A Response instance
*/
class Client extends BaseClient
{
protected $kernel;
+ private $catchExceptions = true;
/**
- * 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
}
/**
- * Makes a request.
+ * Sets whether to catch exceptions when the kernel is handling a request.
*
- * @param Request $request A Request instance
+ * @param bool $catchExceptions Whether to catch exceptions
+ */
+ public function catchExceptions($catchExceptions)
+ {
+ $this->catchExceptions = $catchExceptions;
+ }
+
+ /**
+ * Makes a request.
*
* @return Response A Response instance
*/
protected function doRequest($request)
{
- $response = $this->kernel->handle($request);
+ $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
if ($this->kernel instanceof TerminableInterface) {
$this->kernel->terminate($request, $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();
+ $requires = '';
+ foreach (get_declared_classes() as $class) {
+ if (0 === strpos($class, 'ComposerAutoloaderInit')) {
+ $r = new \ReflectionClass($class);
+ $file = dirname(dirname($r->getFileName())).'/autoload.php';
+ if (file_exists($file)) {
+ $requires .= "require_once '".str_replace("'", "\\'", $file)."';\n";
+ }
+ }
+ }
+
+ if (!$requires) {
+ throw new \RuntimeException('Composer autoloader not found.');
+ }
+
$code = <<<EOF
<?php
error_reporting($errorReporting);
-require_once '$requirePath';
-
-\$loader = new Symfony\Component\ClassLoader\ClassLoader();
-\$loader->addPrefix('Symfony', '$symfonyPath');
-\$loader->register();
+$requires
\$kernel = unserialize('$kernel');
\$request = unserialize('$request');
/**
* Converts the BrowserKit request to a HttpKernel request.
*
- * @param DomRequest $request A DomRequest instance
- *
* @return Request A Request instance
*/
protected function filterRequest(DomRequest $request)
*
* @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)
/**
* 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);
+ return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
}
}