PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php

https://gitlab.com/pr0055/symfonypizza
PHP | 204 lines | 101 code | 30 blank | 73 comment | 9 complexity | edc4f757e598060e6dc3791dca9f7641 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Symfony\Component\HttpKernel\Client as BaseClient;
  14. use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\BrowserKit\History;
  18. use Symfony\Component\BrowserKit\CookieJar;
  19. /**
  20. * Client simulates a browser and makes requests to a Kernel object.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class Client extends BaseClient
  25. {
  26. private $hasPerformedRequest = false;
  27. private $profiler = false;
  28. private $reboot = true;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  33. {
  34. parent::__construct($kernel, $server, $history, $cookieJar);
  35. }
  36. /**
  37. * Returns the container.
  38. *
  39. * @return ContainerInterface|null Returns null when the Kernel has been shutdown or not started yet
  40. */
  41. public function getContainer()
  42. {
  43. return $this->kernel->getContainer();
  44. }
  45. /**
  46. * Returns the kernel.
  47. *
  48. * @return KernelInterface
  49. */
  50. public function getKernel()
  51. {
  52. return $this->kernel;
  53. }
  54. /**
  55. * Gets the profile associated with the current Response.
  56. *
  57. * @return HttpProfile A Profile instance
  58. */
  59. public function getProfile()
  60. {
  61. if (!$this->kernel->getContainer()->has('profiler')) {
  62. return false;
  63. }
  64. return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
  65. }
  66. /**
  67. * Enables the profiler for the very next request.
  68. *
  69. * If the profiler is not enabled, the call to this method does nothing.
  70. */
  71. public function enableProfiler()
  72. {
  73. if ($this->kernel->getContainer()->has('profiler')) {
  74. $this->profiler = true;
  75. }
  76. }
  77. /**
  78. * Disables kernel reboot between requests.
  79. *
  80. * By default, the Client reboots the Kernel for each request. This method
  81. * allows to keep the same kernel across requests.
  82. */
  83. public function disableReboot()
  84. {
  85. $this->reboot = false;
  86. }
  87. /**
  88. * Enables kernel reboot between requests.
  89. */
  90. public function enableReboot()
  91. {
  92. $this->reboot = true;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. *
  97. * @param Request $request A Request instance
  98. *
  99. * @return Response A Response instance
  100. */
  101. protected function doRequest($request)
  102. {
  103. // avoid shutting down the Kernel if no request has been performed yet
  104. // WebTestCase::createClient() boots the Kernel but do not handle a request
  105. if ($this->hasPerformedRequest && $this->reboot) {
  106. $this->kernel->shutdown();
  107. } else {
  108. $this->hasPerformedRequest = true;
  109. }
  110. if ($this->profiler) {
  111. $this->profiler = false;
  112. $this->kernel->boot();
  113. $this->kernel->getContainer()->get('profiler')->enable();
  114. }
  115. return parent::doRequest($request);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. *
  120. * @param Request $request A Request instance
  121. *
  122. * @return Response A Response instance
  123. */
  124. protected function doRequestInProcess($request)
  125. {
  126. $response = parent::doRequestInProcess($request);
  127. $this->profiler = false;
  128. return $response;
  129. }
  130. /**
  131. * Returns the script to execute when the request must be insulated.
  132. *
  133. * It assumes that the autoloader is named 'autoload.php' and that it is
  134. * stored in the same directory as the kernel (this is the case for the
  135. * Symfony Standard Edition). If this is not your case, create your own
  136. * client and override this method.
  137. *
  138. * @param Request $request A Request instance
  139. *
  140. * @return string The script content
  141. */
  142. protected function getScript($request)
  143. {
  144. $kernel = str_replace("'", "\\'", serialize($this->kernel));
  145. $request = str_replace("'", "\\'", serialize($request));
  146. $r = new \ReflectionObject($this->kernel);
  147. $autoloader = dirname($r->getFileName()).'/autoload.php';
  148. if (is_file($autoloader)) {
  149. $autoloader = str_replace("'", "\\'", $autoloader);
  150. } else {
  151. $autoloader = '';
  152. }
  153. $path = str_replace("'", "\\'", $r->getFileName());
  154. $profilerCode = '';
  155. if ($this->profiler) {
  156. $profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
  157. }
  158. $errorReporting = error_reporting();
  159. $code = <<<EOF
  160. <?php
  161. error_reporting($errorReporting);
  162. if ('$autoloader') {
  163. require_once '$autoloader';
  164. }
  165. require_once '$path';
  166. \$kernel = unserialize('$kernel');
  167. \$kernel->boot();
  168. $profilerCode
  169. \$request = unserialize('$request');
  170. EOF;
  171. return $code.$this->getHandleScript();
  172. }
  173. }