2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-09-15 19:49:16 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle;
|
2010-04-21 11:05:34 +01:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Client as BaseClient;
|
2010-10-04 11:09:20 +01:00
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profiler as HttpProfiler;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2010-04-21 11:05:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Client simulates a browser and makes requests to a Kernel object.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-04-21 11:05:34 +01:00
|
|
|
*/
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2011-05-03 12:26:45 +01:00
|
|
|
private $hasPerformedRequest = false;
|
2011-05-11 10:05:57 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the container.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return ContainerInterface
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getContainer()
|
|
|
|
{
|
2011-04-19 21:55:27 +01:00
|
|
|
return $this->kernel->getContainer();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the kernel.
|
|
|
|
*
|
2011-01-25 16:20:20 +00:00
|
|
|
* @return HttpKernelInterface
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getKernel()
|
|
|
|
{
|
|
|
|
return $this->kernel;
|
|
|
|
}
|
|
|
|
|
2010-06-25 15:13:07 +01:00
|
|
|
/**
|
|
|
|
* Gets a profiler for the current Response.
|
|
|
|
*
|
2010-10-04 11:09:20 +01:00
|
|
|
* @return HttpProfiler A Profiler instance
|
2010-06-25 15:13:07 +01:00
|
|
|
*/
|
|
|
|
public function getProfiler()
|
|
|
|
{
|
2011-04-19 21:55:27 +01:00
|
|
|
if (!$this->kernel->getContainer()->has('profiler')) {
|
2010-06-25 15:13:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-30 17:54:40 +01:00
|
|
|
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
|
2010-06-25 15:13:07 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Makes a request.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Request $request A Request instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Response A Response instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function doRequest($request)
|
|
|
|
{
|
2011-05-11 10:05:57 +01:00
|
|
|
// avoid shutting down the Kernel if no request has been performed yet
|
|
|
|
// WebTestCase::createClient() boots the Kernel but do not handle a request
|
2011-05-03 12:26:45 +01:00
|
|
|
if ($this->hasPerformedRequest) {
|
|
|
|
$this->kernel->shutdown();
|
|
|
|
} else {
|
|
|
|
$this->hasPerformedRequest = true;
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-04-19 21:55:27 +01:00
|
|
|
return $this->kernel->handle($request);
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the script to execute when the request must be insulated.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @param Request $request A Request instance
|
|
|
|
*
|
|
|
|
* @return string The script content
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
protected function getScript($request)
|
|
|
|
{
|
|
|
|
$kernel = serialize($this->kernel);
|
|
|
|
$request = serialize($request);
|
|
|
|
|
|
|
|
$r = new \ReflectionObject($this->kernel);
|
|
|
|
$path = $r->getFileName();
|
|
|
|
|
|
|
|
return <<<EOF
|
2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once '$path';
|
|
|
|
|
|
|
|
\$kernel = unserialize('$kernel');
|
|
|
|
\$kernel->boot();
|
|
|
|
echo serialize(\$kernel->handle(unserialize('$request')));
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-05-03 12:26:45 +01:00
|
|
|
}
|