Client.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  29. * @inheritdoc
  30. */
  31. public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  32. {
  33. parent::__construct($kernel, $server, $history, $cookieJar);
  34. }
  35. /**
  36. * Returns the container.
  37. *
  38. * @return ContainerInterface
  39. */
  40. public function getContainer()
  41. {
  42. return $this->kernel->getContainer();
  43. }
  44. /**
  45. * Returns the kernel.
  46. *
  47. * @return KernelInterface
  48. */
  49. public function getKernel()
  50. {
  51. return $this->kernel;
  52. }
  53. /**
  54. * Gets the profile associated with the current Response.
  55. *
  56. * @return HttpProfile A Profile instance
  57. */
  58. public function getProfile()
  59. {
  60. if (!$this->kernel->getContainer()->has('profiler')) {
  61. return false;
  62. }
  63. return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
  64. }
  65. /**
  66. * Enables the profiler for the very next request.
  67. *
  68. * If the profiler is not enabled, the call to this method does nothing.
  69. */
  70. public function enableProfiler()
  71. {
  72. if ($this->kernel->getContainer()->has('profiler')) {
  73. $this->profiler = true;
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @param Request $request A Request instance
  80. *
  81. * @return Response A Response instance
  82. */
  83. protected function doRequest($request)
  84. {
  85. // avoid shutting down the Kernel if no request has been performed yet
  86. // WebTestCase::createClient() boots the Kernel but do not handle a request
  87. if ($this->hasPerformedRequest) {
  88. $this->kernel->shutdown();
  89. } else {
  90. $this->hasPerformedRequest = true;
  91. }
  92. if ($this->profiler) {
  93. $this->profiler = false;
  94. $this->kernel->boot();
  95. $this->kernel->getContainer()->get('profiler')->enable();
  96. }
  97. return parent::doRequest($request);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. *
  102. * @param Request $request A Request instance
  103. *
  104. * @return Response A Response instance
  105. */
  106. protected function doRequestInProcess($request)
  107. {
  108. $response = parent::doRequestInProcess($request);
  109. $this->profiler = false;
  110. return $response;
  111. }
  112. /**
  113. * Returns the script to execute when the request must be insulated.
  114. *
  115. * It assumes that the autoloader is named 'autoload.php' and that it is
  116. * stored in the same directory as the kernel (this is the case for the
  117. * Symfony Standard Edition). If this is not your case, create your own
  118. * client and override this method.
  119. *
  120. * @param Request $request A Request instance
  121. *
  122. * @return string The script content
  123. */
  124. protected function getScript($request)
  125. {
  126. $kernel = str_replace("'", "\\'", serialize($this->kernel));
  127. $request = str_replace("'", "\\'", serialize($request));
  128. $r = new \ReflectionObject($this->kernel);
  129. $autoloader = dirname($r->getFileName()).'/autoload.php';
  130. if (is_file($autoloader)) {
  131. $autoloader = str_replace("'", "\\'", $autoloader);
  132. } else {
  133. $autoloader = '';
  134. }
  135. $path = str_replace("'", "\\'", $r->getFileName());
  136. $profilerCode = '';
  137. if ($this->profiler) {
  138. $profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
  139. }
  140. return <<<EOF
  141. <?php
  142. if ('$autoloader') {
  143. require_once '$autoloader';
  144. }
  145. require_once '$path';
  146. \$kernel = unserialize('$kernel');
  147. \$kernel->boot();
  148. $profilerCode
  149. echo serialize(\$kernel->handle(unserialize('$request')));
  150. EOF;
  151. }
  152. }