|
11 | 11 |
|
12 | 12 | namespace Symfony\Bundle\FrameworkBundle;
|
13 | 13 |
|
14 |
| -@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', Client::class, KernelBrowser::class), E_USER_DEPRECATED); |
| 14 | +use Symfony\Component\BrowserKit\CookieJar; |
| 15 | +use Symfony\Component\BrowserKit\History; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\HttpKernel\HttpKernelBrowser; |
| 20 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 21 | +use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; |
15 | 22 |
|
16 |
| -class Client extends KernelBrowser |
| 23 | +/** |
| 24 | + * Client simulates a browser and makes requests to a Kernel object. |
| 25 | + * |
| 26 | + * @deprecated since Symfony 4.3, use KernelBrowser instead. |
| 27 | + */ |
| 28 | +class Client extends HttpKernelBrowser |
17 | 29 | {
|
| 30 | + private $hasPerformedRequest = false; |
| 31 | + private $profiler = false; |
| 32 | + private $reboot = true; |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + public function __construct(KernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null) |
| 38 | + { |
| 39 | + parent::__construct($kernel, $server, $history, $cookieJar); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns the container. |
| 44 | + * |
| 45 | + * @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet |
| 46 | + */ |
| 47 | + public function getContainer() |
| 48 | + { |
| 49 | + return $this->kernel->getContainer(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the kernel. |
| 54 | + * |
| 55 | + * @return KernelInterface |
| 56 | + */ |
| 57 | + public function getKernel() |
| 58 | + { |
| 59 | + return $this->kernel; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the profile associated with the current Response. |
| 64 | + * |
| 65 | + * @return HttpProfile|false A Profile instance |
| 66 | + */ |
| 67 | + public function getProfile() |
| 68 | + { |
| 69 | + if (!$this->kernel->getContainer()->has('profiler')) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Enables the profiler for the very next request. |
| 78 | + * |
| 79 | + * If the profiler is not enabled, the call to this method does nothing. |
| 80 | + */ |
| 81 | + public function enableProfiler() |
| 82 | + { |
| 83 | + if ($this->kernel->getContainer()->has('profiler')) { |
| 84 | + $this->profiler = true; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Disables kernel reboot between requests. |
| 90 | + * |
| 91 | + * By default, the Client reboots the Kernel for each request. This method |
| 92 | + * allows to keep the same kernel across requests. |
| 93 | + */ |
| 94 | + public function disableReboot() |
| 95 | + { |
| 96 | + $this->reboot = false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Enables kernel reboot between requests. |
| 101 | + */ |
| 102 | + public function enableReboot() |
| 103 | + { |
| 104 | + $this->reboot = true; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@inheritdoc} |
| 109 | + * |
| 110 | + * @param Request $request A Request instance |
| 111 | + * |
| 112 | + * @return Response A Response instance |
| 113 | + */ |
| 114 | + protected function doRequest($request) |
| 115 | + { |
| 116 | + // avoid shutting down the Kernel if no request has been performed yet |
| 117 | + // WebTestCase::createClient() boots the Kernel but do not handle a request |
| 118 | + if ($this->hasPerformedRequest && $this->reboot) { |
| 119 | + $this->kernel->shutdown(); |
| 120 | + } else { |
| 121 | + $this->hasPerformedRequest = true; |
| 122 | + } |
| 123 | + |
| 124 | + if ($this->profiler) { |
| 125 | + $this->profiler = false; |
| 126 | + |
| 127 | + $this->kernel->boot(); |
| 128 | + $this->kernel->getContainer()->get('profiler')->enable(); |
| 129 | + } |
| 130 | + |
| 131 | + return parent::doRequest($request); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * {@inheritdoc} |
| 136 | + * |
| 137 | + * @param Request $request A Request instance |
| 138 | + * |
| 139 | + * @return Response A Response instance |
| 140 | + */ |
| 141 | + protected function doRequestInProcess($request) |
| 142 | + { |
| 143 | + $response = parent::doRequestInProcess($request); |
| 144 | + |
| 145 | + $this->profiler = false; |
| 146 | + |
| 147 | + return $response; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns the script to execute when the request must be insulated. |
| 152 | + * |
| 153 | + * It assumes that the autoloader is named 'autoload.php' and that it is |
| 154 | + * stored in the same directory as the kernel (this is the case for the |
| 155 | + * Symfony Standard Edition). If this is not your case, create your own |
| 156 | + * client and override this method. |
| 157 | + * |
| 158 | + * @param Request $request A Request instance |
| 159 | + * |
| 160 | + * @return string The script content |
| 161 | + */ |
| 162 | + protected function getScript($request) |
| 163 | + { |
| 164 | + $kernel = var_export(serialize($this->kernel), true); |
| 165 | + $request = var_export(serialize($request), true); |
| 166 | + $errorReporting = error_reporting(); |
| 167 | + |
| 168 | + $requires = ''; |
| 169 | + foreach (get_declared_classes() as $class) { |
| 170 | + if (0 === strpos($class, 'ComposerAutoloaderInit')) { |
| 171 | + $r = new \ReflectionClass($class); |
| 172 | + $file = \dirname(\dirname($r->getFileName())).'/autoload.php'; |
| 173 | + if (file_exists($file)) { |
| 174 | + $requires .= 'require_once '.var_export($file, true).";\n"; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (!$requires) { |
| 180 | + throw new \RuntimeException('Composer autoloader not found.'); |
| 181 | + } |
| 182 | + |
| 183 | + $requires .= 'require_once '.var_export((new \ReflectionObject($this->kernel))->getFileName(), true).";\n"; |
| 184 | + |
| 185 | + $profilerCode = ''; |
| 186 | + if ($this->profiler) { |
| 187 | + $profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();'; |
| 188 | + } |
| 189 | + |
| 190 | + $code = <<<EOF |
| 191 | +<?php |
| 192 | +
|
| 193 | +error_reporting($errorReporting); |
| 194 | +
|
| 195 | +$requires |
| 196 | +
|
| 197 | +\$kernel = unserialize($kernel); |
| 198 | +\$kernel->boot(); |
| 199 | +$profilerCode |
| 200 | +
|
| 201 | +\$request = unserialize($request); |
| 202 | +EOF; |
| 203 | + |
| 204 | + return $code.$this->getHandleScript(); |
| 205 | + } |
18 | 206 | }
|
0 commit comments