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;
|
2011-06-01 09:55:01 +01:00
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
|
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;
|
2012-07-13 14:40:24 +01:00
|
|
|
private $profiler = 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
|
|
|
/**
|
2011-06-01 09:55:01 +01:00
|
|
|
* Gets the profile associated with the current Response.
|
2010-06-25 15:13:07 +01:00
|
|
|
*
|
2011-06-01 09:55:01 +01:00
|
|
|
* @return HttpProfile A Profile instance
|
2010-06-25 15:13:07 +01:00
|
|
|
*/
|
2011-06-01 09:55:01 +01:00
|
|
|
public function getProfile()
|
2010-06-25 15:13:07 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-07-13 14:40:24 +01:00
|
|
|
/**
|
|
|
|
* Enables the profiler for the very next request.
|
|
|
|
*
|
2012-09-18 15:33:46 +01:00
|
|
|
* If the profiler is not enabled, the call to this method does nothing.
|
2012-07-13 14:40:24 +01:00
|
|
|
*/
|
|
|
|
public function enableProfiler()
|
|
|
|
{
|
2012-09-18 15:33:46 +01:00
|
|
|
if ($this->kernel->getContainer()->has('profiler')) {
|
|
|
|
$this->profiler = true;
|
2012-07-13 14:40:24 +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
|
|
|
|
2012-07-13 14:40:24 +01:00
|
|
|
if ($this->profiler) {
|
|
|
|
$this->profiler = false;
|
|
|
|
|
|
|
|
$this->kernel->boot();
|
|
|
|
$this->kernel->getContainer()->get('profiler')->enable();
|
|
|
|
}
|
|
|
|
|
2011-04-19 21:55:27 +01:00
|
|
|
return $this->kernel->handle($request);
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 14:40:24 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function doRequestInProcess($request)
|
|
|
|
{
|
|
|
|
$response = parent::doRequestInProcess($request);
|
|
|
|
|
|
|
|
$this->profiler = false;
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the script to execute when the request must be insulated.
|
|
|
|
*
|
2012-07-01 22:01:26 +01:00
|
|
|
* It assumes that the autoloader is named 'autoload.php' and that it is
|
|
|
|
* stored in the same directory as the kernel (this is the case for the
|
|
|
|
* Symfony Standard Edition). If this is not your case, create your own
|
|
|
|
* client and override this method.
|
|
|
|
*
|
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)
|
|
|
|
{
|
2011-07-22 12:37:23 +01:00
|
|
|
$kernel = str_replace("'", "\\'", serialize($this->kernel));
|
|
|
|
$request = str_replace("'", "\\'", serialize($request));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$r = new \ReflectionObject($this->kernel);
|
2012-07-01 22:01:26 +01:00
|
|
|
|
|
|
|
$autoloader = dirname($r->getFileName()).'/autoload.php';
|
|
|
|
if (is_file($autoloader)) {
|
|
|
|
$autoloader = str_replace("'", "\\'", $autoloader);
|
|
|
|
} else {
|
|
|
|
$autoloader = '';
|
|
|
|
}
|
|
|
|
|
2011-07-22 12:37:23 +01:00
|
|
|
$path = str_replace("'", "\\'", $r->getFileName());
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-07-13 14:40:24 +01:00
|
|
|
$profilerCode = '';
|
|
|
|
if ($this->profiler) {
|
|
|
|
$profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return <<<EOF
|
2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
2012-07-01 22:01:26 +01:00
|
|
|
if ('$autoloader') {
|
|
|
|
require_once '$autoloader';
|
|
|
|
}
|
2010-04-21 11:05:34 +01:00
|
|
|
require_once '$path';
|
|
|
|
|
|
|
|
\$kernel = unserialize('$kernel');
|
|
|
|
\$kernel->boot();
|
2012-07-13 14:40:24 +01:00
|
|
|
$profilerCode
|
2010-04-21 11:05:34 +01:00
|
|
|
echo serialize(\$kernel->handle(unserialize('$request')));
|
|
|
|
EOF;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|