Client.php 6.3KB

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