No Description

Client.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Request as DomRequest;
  13. use Symfony\Component\BrowserKit\Response as DomResponse;
  14. use Symfony\Component\BrowserKit\Cookie as DomCookie;
  15. use Symfony\Component\BrowserKit\History;
  16. use Symfony\Component\BrowserKit\CookieJar;
  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. * @api
  26. */
  27. class Client extends BaseClient
  28. {
  29. protected $kernel;
  30. /**
  31. * Constructor.
  32. *
  33. * @param HttpKernelInterface $kernel An HttpKernel instance
  34. * @param array $server The server parameters (equivalent of $_SERVER)
  35. * @param History $history A History instance to store the browser history
  36. * @param CookieJar $cookieJar A CookieJar instance to store the cookies
  37. */
  38. public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  39. {
  40. // These class properties must be set before calling the parent constructor, as it may depend on it.
  41. $this->kernel = $kernel;
  42. $this->followRedirects = false;
  43. parent::__construct($server, $history, $cookieJar);
  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. $errorReporting = error_reporting();
  93. $code = <<<EOF
  94. <?php
  95. error_reporting($errorReporting);
  96. require_once '$requirePath';
  97. \$loader = new Symfony\Component\ClassLoader\ClassLoader();
  98. \$loader->addPrefix('Symfony', '$symfonyPath');
  99. \$loader->register();
  100. \$kernel = unserialize('$kernel');
  101. \$request = unserialize('$request');
  102. EOF;
  103. return $code.$this->getHandleScript();
  104. }
  105. protected function getHandleScript()
  106. {
  107. return <<<'EOF'
  108. $response = $kernel->handle($request);
  109. if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
  110. $kernel->terminate($request, $response);
  111. }
  112. echo serialize($response);
  113. EOF;
  114. }
  115. /**
  116. * Converts the BrowserKit request to a HttpKernel request.
  117. *
  118. * @param DomRequest $request A DomRequest instance
  119. *
  120. * @return Request A Request instance
  121. */
  122. protected function filterRequest(DomRequest $request)
  123. {
  124. $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
  125. foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
  126. $httpRequest->files->set($key, $value);
  127. }
  128. return $httpRequest;
  129. }
  130. /**
  131. * Filters an array of files.
  132. *
  133. * This method created test instances of UploadedFile so that the move()
  134. * method can be called on those instances.
  135. *
  136. * If the size of a file is greater than the allowed size (from php.ini) then
  137. * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
  138. *
  139. * @see UploadedFile
  140. *
  141. * @param array $files An array of files
  142. *
  143. * @return array An array with all uploaded files marked as already moved
  144. */
  145. protected function filterFiles(array $files)
  146. {
  147. $filtered = array();
  148. foreach ($files as $key => $value) {
  149. if (is_array($value)) {
  150. $filtered[$key] = $this->filterFiles($value);
  151. } elseif ($value instanceof UploadedFile) {
  152. if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
  153. $filtered[$key] = new UploadedFile(
  154. '',
  155. $value->getClientOriginalName(),
  156. $value->getClientMimeType(),
  157. 0,
  158. UPLOAD_ERR_INI_SIZE,
  159. true
  160. );
  161. } else {
  162. $filtered[$key] = new UploadedFile(
  163. $value->getPathname(),
  164. $value->getClientOriginalName(),
  165. $value->getClientMimeType(),
  166. $value->getClientSize(),
  167. $value->getError(),
  168. true
  169. );
  170. }
  171. }
  172. }
  173. return $filtered;
  174. }
  175. /**
  176. * Converts the HttpKernel response to a BrowserKit response.
  177. *
  178. * @param Response $response A Response instance
  179. *
  180. * @return DomResponse A DomResponse instance
  181. */
  182. protected function filterResponse($response)
  183. {
  184. $headers = $response->headers->all();
  185. if ($response->headers->getCookies()) {
  186. $cookies = array();
  187. foreach ($response->headers->getCookies() as $cookie) {
  188. $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
  189. }
  190. $headers['Set-Cookie'] = $cookies;
  191. }
  192. // this is needed to support StreamedResponse
  193. ob_start();
  194. $response->sendContent();
  195. $content = ob_get_clean();
  196. return new DomResponse($content, $response->getStatusCode(), $headers);
  197. }
  198. }