Client.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\HttpKernel\HttpKernelInterface;
  5. use Symfony\Component\HttpKernel\Client as BaseClient;
  6. use Symfony\Component\BrowserKit\History;
  7. use Symfony\Component\BrowserKit\CookieJar;
  8. use Symfony\Component\HttpKernel\Profiler\Profiler as HttpProfiler;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. /*
  12. * This file is part of the Symfony package.
  13. *
  14. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  15. *
  16. * For the full copyright and license information, please view the LICENSE
  17. * file that was distributed with this source code.
  18. */
  19. /**
  20. * Client simulates a browser and makes requests to a Kernel object.
  21. *
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class Client extends BaseClient
  25. {
  26. protected $container;
  27. /**
  28. * Constructor.
  29. *
  30. * @param HttpKernelInterface $kernel A Kernel instance
  31. * @param array $server The server parameters (equivalent of $_SERVER)
  32. * @param History $history A History instance to store the browser history
  33. * @param CookieJar $cookieJar A CookieJar instance to store the cookies
  34. */
  35. public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  36. {
  37. parent::__construct($kernel, $server, $history, $cookieJar);
  38. $this->container = $kernel->getContainer();
  39. }
  40. /**
  41. * Returns the container.
  42. *
  43. * @return ContainerInterface
  44. */
  45. public function getContainer()
  46. {
  47. return $this->container;
  48. }
  49. /**
  50. * Returns the kernel.
  51. *
  52. * @return Kernel
  53. */
  54. public function getKernel()
  55. {
  56. return $this->kernel;
  57. }
  58. /**
  59. * Gets a profiler for the current Response.
  60. *
  61. * @return HttpProfiler A Profiler instance
  62. */
  63. public function getProfiler()
  64. {
  65. if (!$this->container->has('profiler')) {
  66. return false;
  67. }
  68. return $this->container->get('profiler')->loadFromResponse($this->response);
  69. }
  70. /**
  71. * Makes a request.
  72. *
  73. * @param Request $request A Request instance
  74. *
  75. * @return Response A Response instance
  76. */
  77. protected function doRequest($request)
  78. {
  79. $returnValue = $this->kernel->handle($request);
  80. $this->kernel->reboot();
  81. return $returnValue;
  82. }
  83. /**
  84. * Returns the script to execute when the request must be insulated.
  85. *
  86. * @param Request $request A Request instance
  87. *
  88. * @return string The script content
  89. */
  90. protected function getScript($request)
  91. {
  92. $kernel = serialize($this->kernel);
  93. $request = serialize($request);
  94. $r = new \ReflectionObject($this->kernel);
  95. $path = $r->getFileName();
  96. return <<<EOF
  97. <?php
  98. require_once '$path';
  99. \$kernel = unserialize('$kernel');
  100. \$kernel->boot();
  101. echo serialize(\$kernel->handle(unserialize('$request')));
  102. EOF;
  103. }
  104. }