HttpKernelBrowser.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\Component\HttpKernel;
  11. use Symfony\Component\BrowserKit\AbstractBrowser;
  12. use Symfony\Component\BrowserKit\CookieJar;
  13. use Symfony\Component\BrowserKit\History;
  14. use Symfony\Component\BrowserKit\Request as DomRequest;
  15. use Symfony\Component\BrowserKit\Response as DomResponse;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. /**
  20. * Simulates a browser and makes requests to an HttpKernel instance.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @method Request getRequest() A Request instance
  25. * @method Response getResponse() A Response instance
  26. */
  27. class HttpKernelBrowser extends AbstractBrowser
  28. {
  29. protected $kernel;
  30. private $catchExceptions = true;
  31. /**
  32. * @param array $server The server parameters (equivalent of $_SERVER)
  33. */
  34. public function __construct(HttpKernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
  35. {
  36. // These class properties must be set before calling the parent constructor, as it may depend on it.
  37. $this->kernel = $kernel;
  38. $this->followRedirects = false;
  39. parent::__construct($server, $history, $cookieJar);
  40. }
  41. /**
  42. * Sets whether to catch exceptions when the kernel is handling a request.
  43. */
  44. public function catchExceptions(bool $catchExceptions)
  45. {
  46. $this->catchExceptions = $catchExceptions;
  47. }
  48. /**
  49. * Makes a request.
  50. *
  51. * @return Response A Response instance
  52. */
  53. protected function doRequest($request)
  54. {
  55. $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $this->catchExceptions);
  56. if ($this->kernel instanceof TerminableInterface) {
  57. $this->kernel->terminate($request, $response);
  58. }
  59. return $response;
  60. }
  61. /**
  62. * Returns the script to execute when the request must be insulated.
  63. *
  64. * @return string
  65. */
  66. protected function getScript($request)
  67. {
  68. $kernel = var_export(serialize($this->kernel), true);
  69. $request = var_export(serialize($request), true);
  70. $errorReporting = error_reporting();
  71. $requires = '';
  72. foreach (get_declared_classes() as $class) {
  73. if (0 === strpos($class, 'ComposerAutoloaderInit')) {
  74. $r = new \ReflectionClass($class);
  75. $file = \dirname($r->getFileName(), 2).'/autoload.php';
  76. if (file_exists($file)) {
  77. $requires .= 'require_once '.var_export($file, true).";\n";
  78. }
  79. }
  80. }
  81. if (!$requires) {
  82. throw new \RuntimeException('Composer autoloader not found.');
  83. }
  84. $code = <<<EOF
  85. <?php
  86. error_reporting($errorReporting);
  87. $requires
  88. \$kernel = unserialize($kernel);
  89. \$request = unserialize($request);
  90. EOF;
  91. return $code.$this->getHandleScript();
  92. }
  93. protected function getHandleScript()
  94. {
  95. return <<<'EOF'
  96. $response = $kernel->handle($request);
  97. if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
  98. $kernel->terminate($request, $response);
  99. }
  100. echo serialize($response);
  101. EOF;
  102. }
  103. /**
  104. * Converts the BrowserKit request to a HttpKernel request.
  105. *
  106. * @return Request A Request instance
  107. */
  108. protected function filterRequest(DomRequest $request)
  109. {
  110. $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $server = $request->getServer(), $request->getContent());
  111. if (!isset($server['HTTP_ACCEPT'])) {
  112. $httpRequest->headers->remove('Accept');
  113. }
  114. foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
  115. $httpRequest->files->set($key, $value);
  116. }
  117. return $httpRequest;
  118. }
  119. /**
  120. * Filters an array of files.
  121. *
  122. * This method created test instances of UploadedFile so that the move()
  123. * method can be called on those instances.
  124. *
  125. * If the size of a file is greater than the allowed size (from php.ini) then
  126. * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
  127. *
  128. * @see UploadedFile
  129. *
  130. * @return array An array with all uploaded files marked as already moved
  131. */
  132. protected function filterFiles(array $files)
  133. {
  134. $filtered = [];
  135. foreach ($files as $key => $value) {
  136. if (\is_array($value)) {
  137. $filtered[$key] = $this->filterFiles($value);
  138. } elseif ($value instanceof UploadedFile) {
  139. if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
  140. $filtered[$key] = new UploadedFile(
  141. '',
  142. $value->getClientOriginalName(),
  143. $value->getClientMimeType(),
  144. \UPLOAD_ERR_INI_SIZE,
  145. true
  146. );
  147. } else {
  148. $filtered[$key] = new UploadedFile(
  149. $value->getPathname(),
  150. $value->getClientOriginalName(),
  151. $value->getClientMimeType(),
  152. $value->getError(),
  153. true
  154. );
  155. }
  156. }
  157. }
  158. return $filtered;
  159. }
  160. /**
  161. * Converts the HttpKernel response to a BrowserKit response.
  162. *
  163. * @return DomResponse A DomResponse instance
  164. */
  165. protected function filterResponse($response)
  166. {
  167. // this is needed to support StreamedResponse
  168. ob_start();
  169. $response->sendContent();
  170. $content = ob_get_clean();
  171. return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
  172. }
  173. }