PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Client.php

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