菜谱项目

Client.php 6.0KB

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