Client.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\Client as BaseClient;
  14. use Symfony\Component\HttpKernel\Profiler\Profiler as HttpProfiler;
  15. use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. /**
  19. * Client simulates a browser and makes requests to a Kernel object.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class Client extends BaseClient
  24. {
  25. private $hasPerformedRequest = false;
  26. /**
  27. * Returns the container.
  28. *
  29. * @return ContainerInterface
  30. */
  31. public function getContainer()
  32. {
  33. return $this->kernel->getContainer();
  34. }
  35. /**
  36. * Returns the kernel.
  37. *
  38. * @return HttpKernelInterface
  39. */
  40. public function getKernel()
  41. {
  42. return $this->kernel;
  43. }
  44. /**
  45. * Gets the profile associated with the current Response.
  46. *
  47. * @return HttpProfile A Profile instance
  48. */
  49. public function getProfile()
  50. {
  51. if (!$this->kernel->getContainer()->has('profiler')) {
  52. return false;
  53. }
  54. return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
  55. }
  56. /**
  57. * Makes a request.
  58. *
  59. * @param Request $request A Request instance
  60. *
  61. * @return Response A Response instance
  62. */
  63. protected function doRequest($request)
  64. {
  65. // avoid shutting down the Kernel if no request has been performed yet
  66. // WebTestCase::createClient() boots the Kernel but do not handle a request
  67. if ($this->hasPerformedRequest) {
  68. $this->kernel->shutdown();
  69. } else {
  70. $this->hasPerformedRequest = true;
  71. }
  72. return $this->kernel->handle($request);
  73. }
  74. /**
  75. * Returns the script to execute when the request must be insulated.
  76. *
  77. * @param Request $request A Request instance
  78. *
  79. * @return string The script content
  80. */
  81. protected function getScript($request)
  82. {
  83. $kernel = serialize($this->kernel);
  84. $request = serialize($request);
  85. $r = new \ReflectionObject($this->kernel);
  86. $path = $r->getFileName();
  87. return <<<EOF
  88. <?php
  89. require_once '$path';
  90. \$kernel = unserialize('$kernel');
  91. \$kernel->boot();
  92. echo serialize(\$kernel->handle(unserialize('$request')));
  93. EOF;
  94. }
  95. }