Client.php 6.3 KB

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