Client.php 6.5 KB

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