Client.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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|null getRequest() A Request instance
  25. * @method Response|null getResponse() A Response instance
  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. * Makes a request.
  47. *
  48. * @param Request $request A Request instance
  49. *
  50. * @return Response A Response instance
  51. */
  52. protected function doRequest($request)
  53. {
  54. $response = $this->kernel->handle($request);
  55. if ($this->kernel instanceof TerminableInterface) {
  56. $this->kernel->terminate($request, $response);
  57. }
  58. return $response;
  59. }
  60. /**
  61. * Returns the script to execute when the request must be insulated.
  62. *
  63. * @param Request $request A Request instance
  64. *
  65. * @return string
  66. */
  67. protected function getScript($request)
  68. {
  69. $kernel = str_replace("'", "\\'", serialize($this->kernel));
  70. $request = str_replace("'", "\\'", serialize($request));
  71. $errorReporting = error_reporting();
  72. $requires = '';
  73. foreach (get_declared_classes() as $class) {
  74. if (0 === strpos($class, 'ComposerAutoloaderInit')) {
  75. $r = new \ReflectionClass($class);
  76. $file = dirname(dirname($r->getFileName())).'/autoload.php';
  77. if (file_exists($file)) {
  78. $requires .= "require_once '".str_replace("'", "\\'", $file)."';\n";
  79. }
  80. }
  81. }
  82. if (!$requires) {
  83. throw new \RuntimeException('Composer autoloader not found.');
  84. }
  85. $code = <<<EOF
  86. <?php
  87. error_reporting($errorReporting);
  88. $requires
  89. \$kernel = unserialize('$kernel');
  90. \$request = unserialize('$request');
  91. EOF;
  92. return $code.$this->getHandleScript();
  93. }
  94. protected function getHandleScript()
  95. {
  96. return <<<'EOF'
  97. $response = $kernel->handle($request);
  98. if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
  99. $kernel->terminate($request, $response);
  100. }
  101. echo serialize($response);
  102. EOF;
  103. }
  104. /**
  105. * Converts the BrowserKit request to a HttpKernel request.
  106. *
  107. * @param DomRequest $request A DomRequest instance
  108. *
  109. * @return Request A Request instance
  110. */
  111. protected function filterRequest(DomRequest $request)
  112. {
  113. $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
  114. foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
  115. $httpRequest->files->set($key, $value);
  116. }
  117. return $httpRequest;
  118. }
  119. /**
  120. * Filters an array of files.
  121. *
  122. * This method created test instances of UploadedFile so that the move()
  123. * method can be called on those instances.
  124. *
  125. * If the size of a file is greater than the allowed size (from php.ini) then
  126. * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
  127. *
  128. * @see UploadedFile
  129. *
  130. * @param array $files An array of files
  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. 0,
  147. UPLOAD_ERR_INI_SIZE,
  148. true
  149. );
  150. } else {
  151. $filtered[$key] = new UploadedFile(
  152. $value->getPathname(),
  153. $value->getClientOriginalName(),
  154. $value->getClientMimeType(),
  155. $value->getClientSize(),
  156. $value->getError(),
  157. true
  158. );
  159. }
  160. }
  161. }
  162. return $filtered;
  163. }
  164. /**
  165. * Converts the HttpKernel response to a BrowserKit response.
  166. *
  167. * @param Response $response A Response instance
  168. *
  169. * @return DomResponse A DomResponse instance
  170. */
  171. protected function filterResponse($response)
  172. {
  173. // this is needed to support StreamedResponse
  174. ob_start();
  175. $response->sendContent();
  176. $content = ob_get_clean();
  177. return new DomResponse($content, $response->getStatusCode(), $response->headers->all());
  178. }
  179. }