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
|
|
|
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\BrowserKit\CookieJar;
|
|
|
|
use Symfony\Component\BrowserKit\History;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\HttpKernel\Client as BaseClient;
|
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
|
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;
|
2015-03-16 09:47:08 +00:00
|
|
|
private $reboot = true;
|
2011-05-11 10:05:57 +01:00
|
|
|
|
2012-09-19 04:59:05 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2012-09-19 04:59:05 +01:00
|
|
|
*/
|
2019-01-16 09:39:14 +00:00
|
|
|
public function __construct(KernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
|
2012-09-19 04:59:05 +01:00
|
|
|
{
|
|
|
|
parent::__construct($kernel, $server, $history, $cookieJar);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the container.
|
|
|
|
*
|
2016-03-14 21:10:13 +00:00
|
|
|
* @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet
|
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.
|
|
|
|
*
|
2012-09-19 04:59:05 +01:00
|
|
|
* @return KernelInterface
|
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
|
|
|
*
|
2017-11-07 11:54:30 +00:00
|
|
|
* @return HttpProfile|false 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
|
|
|
{
|
2019-06-25 16:43:39 +01:00
|
|
|
if (null === $this->response || !$this->kernel->getContainer()->has('profiler')) {
|
2010-06-25 15:13:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-24 20:08:47 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 09:47:08 +00:00
|
|
|
/**
|
2015-03-23 08:23:11 +00:00
|
|
|
* Disables kernel reboot between requests.
|
|
|
|
*
|
2015-03-16 09:47:08 +00:00
|
|
|
* By default, the Client reboots the Kernel for each request. This method
|
|
|
|
* allows to keep the same kernel across requests.
|
|
|
|
*/
|
|
|
|
public function disableReboot()
|
|
|
|
{
|
|
|
|
$this->reboot = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-23 08:23:11 +00:00
|
|
|
* Enables kernel reboot between requests.
|
2015-03-16 09:47:08 +00:00
|
|
|
*/
|
|
|
|
public function enableReboot()
|
|
|
|
{
|
|
|
|
$this->reboot = true;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2013-04-25 06:52:12 +01:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
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
|
2015-03-16 09:47:08 +00:00
|
|
|
if ($this->hasPerformedRequest && $this->reboot) {
|
2011-05-03 12:26:45 +01:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2013-01-18 10:08:30 +00:00
|
|
|
return parent::doRequest($request);
|
2010-04-21 11:05:34 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 14:40:24 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2013-04-25 06:52:12 +01:00
|
|
|
*
|
|
|
|
* @param Request $request A Request instance
|
|
|
|
*
|
|
|
|
* @return Response A Response instance
|
2012-07-13 14:40:24 +01:00
|
|
|
*/
|
|
|
|
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)
|
|
|
|
{
|
2018-09-03 15:40:22 +01:00
|
|
|
$kernel = var_export(serialize($this->kernel), true);
|
|
|
|
$request = var_export(serialize($request), true);
|
2017-03-08 09:00:17 +00:00
|
|
|
$errorReporting = error_reporting();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2017-03-08 09:00:17 +00:00
|
|
|
$requires = '';
|
|
|
|
foreach (get_declared_classes() as $class) {
|
|
|
|
if (0 === strpos($class, 'ComposerAutoloaderInit')) {
|
|
|
|
$r = new \ReflectionClass($class);
|
2018-07-26 09:45:46 +01:00
|
|
|
$file = \dirname(\dirname($r->getFileName())).'/autoload.php';
|
2017-03-08 09:00:17 +00:00
|
|
|
if (file_exists($file)) {
|
2018-09-05 12:56:21 +01:00
|
|
|
$requires .= 'require_once '.var_export($file, true).";\n";
|
2017-03-08 09:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 22:01:26 +01:00
|
|
|
|
2017-03-08 09:00:17 +00:00
|
|
|
if (!$requires) {
|
|
|
|
throw new \RuntimeException('Composer autoloader not found.');
|
2012-07-01 22:01:26 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 12:56:21 +01:00
|
|
|
$requires .= 'require_once '.var_export((new \ReflectionObject($this->kernel))->getFileName(), true).";\n";
|
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();';
|
|
|
|
}
|
|
|
|
|
2013-09-29 20:03:21 +01:00
|
|
|
$code = <<<EOF
|
2010-04-21 11:05:34 +01:00
|
|
|
<?php
|
|
|
|
|
2015-12-10 14:12:08 +00:00
|
|
|
error_reporting($errorReporting);
|
2014-12-23 06:43:53 +00:00
|
|
|
|
2017-03-08 09:00:17 +00:00
|
|
|
$requires
|
2010-04-21 11:05:34 +01:00
|
|
|
|
2018-09-03 15:40:22 +01:00
|
|
|
\$kernel = unserialize($kernel);
|
2010-04-21 11:05:34 +01:00
|
|
|
\$kernel->boot();
|
2012-07-13 14:40:24 +01:00
|
|
|
$profilerCode
|
2013-09-29 20:03:21 +01:00
|
|
|
|
2018-09-03 15:40:22 +01:00
|
|
|
\$request = unserialize($request);
|
2010-04-21 11:05:34 +01:00
|
|
|
EOF;
|
2013-09-29 20:03:21 +01:00
|
|
|
|
|
|
|
return $code.$this->getHandleScript();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|