No Description

Client.php 6.9KB

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