No Description

Client.php 6.0KB

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