PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php

https://github.com/djae138/symfony
PHP | 226 lines | 119 code | 30 blank | 77 comment | 8 complexity | 9d74065353ba50a797208fffd0f8fada MD5 | raw file
Possible License(s): BSD-3-Clause
  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\HttpKernel\TerminableInterface;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. /**
  22. * Client simulates a browser and makes requests to a Kernel object.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. *
  26. * @api
  27. */
  28. class Client extends BaseClient
  29. {
  30. protected $kernel;
  31. /**
  32. * Constructor.
  33. *
  34. * @param HttpKernelInterface $kernel An HttpKernel instance
  35. * @param array $server The server parameters (equivalent of $_SERVER)
  36. * @param History $history A History instance to store the browser history
  37. * @param CookieJar $cookieJar A CookieJar instance to store the cookies
  38. */
  39. public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  40. {
  41. $this->kernel = $kernel;
  42. parent::__construct($server, $history, $cookieJar);
  43. $this->followRedirects = false;
  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. $code = <<<EOF
  93. <?php
  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. $httpRequest->files->replace($this->filterFiles($httpRequest->files->all()));
  124. return $httpRequest;
  125. }
  126. /**
  127. * Filters an array of files.
  128. *
  129. * This method created test instances of UploadedFile so that the move()
  130. * method can be called on those instances.
  131. *
  132. * If the size of a file is greater than the allowed size (from php.ini) then
  133. * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
  134. *
  135. * @see Symfony\Component\HttpFoundation\File\UploadedFile
  136. *
  137. * @param array $files An array of files
  138. *
  139. * @return array An array with all uploaded files marked as already moved
  140. */
  141. protected function filterFiles(array $files)
  142. {
  143. $filtered = array();
  144. foreach ($files as $key => $value) {
  145. if (is_array($value)) {
  146. $filtered[$key] = $this->filterFiles($value);
  147. } elseif ($value instanceof UploadedFile) {
  148. if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
  149. $filtered[$key] = new UploadedFile(
  150. '',
  151. $value->getClientOriginalName(),
  152. $value->getClientMimeType(),
  153. 0,
  154. UPLOAD_ERR_INI_SIZE,
  155. true
  156. );
  157. } else {
  158. $filtered[$key] = new UploadedFile(
  159. $value->getPathname(),
  160. $value->getClientOriginalName(),
  161. $value->getClientMimeType(),
  162. $value->getClientSize(),
  163. $value->getError(),
  164. true
  165. );
  166. }
  167. } else {
  168. $filtered[$key] = $value;
  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. }