Client.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\BrowserKit\CookieJar;
  12. use Symfony\Component\BrowserKit\History;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Client as BaseClient;
  17. use Symfony\Component\HttpKernel\KernelInterface;
  18. use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
  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|false 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 = var_export(serialize($this->kernel), true);
  145. $request = var_export(serialize($request), true);
  146. $r = new \ReflectionObject($this->kernel);
  147. $autoloader = \dirname($r->getFileName()).'/autoload.php';
  148. if (is_file($autoloader)) {
  149. $autoloader = var_export($autoloader, true);
  150. } else {
  151. $autoloader = 'false';
  152. }
  153. $path = var_export($r->getFileName(), true);
  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. }