comparison vendor/symfony/http-kernel/Client.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpKernel;
13
14 use Symfony\Component\BrowserKit\Client as BaseClient;
15 use Symfony\Component\BrowserKit\Request as DomRequest;
16 use Symfony\Component\BrowserKit\Response as DomResponse;
17 use Symfony\Component\BrowserKit\Cookie as DomCookie;
18 use Symfony\Component\BrowserKit\History;
19 use Symfony\Component\BrowserKit\CookieJar;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
23
24 /**
25 * Client simulates a browser and makes requests to a Kernel object.
26 *
27 * @author Fabien Potencier <fabien@symfony.com>
28 *
29 * @method Request|null getRequest() A Request instance
30 * @method Response|null getResponse() A Response instance
31 */
32 class Client extends BaseClient
33 {
34 protected $kernel;
35
36 /**
37 * Constructor.
38 *
39 * @param HttpKernelInterface $kernel An HttpKernel instance
40 * @param array $server The server parameters (equivalent of $_SERVER)
41 * @param History $history A History instance to store the browser history
42 * @param CookieJar $cookieJar A CookieJar instance to store the cookies
43 */
44 public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
45 {
46 // These class properties must be set before calling the parent constructor, as it may depend on it.
47 $this->kernel = $kernel;
48 $this->followRedirects = false;
49
50 parent::__construct($server, $history, $cookieJar);
51 }
52
53 /**
54 * Makes a request.
55 *
56 * @param Request $request A Request instance
57 *
58 * @return Response A Response instance
59 */
60 protected function doRequest($request)
61 {
62 $response = $this->kernel->handle($request);
63
64 if ($this->kernel instanceof TerminableInterface) {
65 $this->kernel->terminate($request, $response);
66 }
67
68 return $response;
69 }
70
71 /**
72 * Returns the script to execute when the request must be insulated.
73 *
74 * @param Request $request A Request instance
75 *
76 * @return string
77 */
78 protected function getScript($request)
79 {
80 $kernel = str_replace("'", "\\'", serialize($this->kernel));
81 $request = str_replace("'", "\\'", serialize($request));
82
83 $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
84 $requirePath = str_replace("'", "\\'", $r->getFileName());
85 $symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
86 $errorReporting = error_reporting();
87
88 $code = <<<EOF
89 <?php
90
91 error_reporting($errorReporting);
92
93 require_once '$requirePath';
94
95 \$loader = new Symfony\Component\ClassLoader\ClassLoader();
96 \$loader->addPrefix('Symfony', '$symfonyPath');
97 \$loader->register();
98
99 \$kernel = unserialize('$kernel');
100 \$request = unserialize('$request');
101 EOF;
102
103 return $code.$this->getHandleScript();
104 }
105
106 protected function getHandleScript()
107 {
108 return <<<'EOF'
109 $response = $kernel->handle($request);
110
111 if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
112 $kernel->terminate($request, $response);
113 }
114
115 echo serialize($response);
116 EOF;
117 }
118
119 /**
120 * Converts the BrowserKit request to a HttpKernel request.
121 *
122 * @param DomRequest $request A DomRequest instance
123 *
124 * @return Request A Request instance
125 */
126 protected function filterRequest(DomRequest $request)
127 {
128 $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
129
130 foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
131 $httpRequest->files->set($key, $value);
132 }
133
134 return $httpRequest;
135 }
136
137 /**
138 * Filters an array of files.
139 *
140 * This method created test instances of UploadedFile so that the move()
141 * method can be called on those instances.
142 *
143 * If the size of a file is greater than the allowed size (from php.ini) then
144 * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
145 *
146 * @see UploadedFile
147 *
148 * @param array $files An array of files
149 *
150 * @return array An array with all uploaded files marked as already moved
151 */
152 protected function filterFiles(array $files)
153 {
154 $filtered = array();
155 foreach ($files as $key => $value) {
156 if (is_array($value)) {
157 $filtered[$key] = $this->filterFiles($value);
158 } elseif ($value instanceof UploadedFile) {
159 if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
160 $filtered[$key] = new UploadedFile(
161 '',
162 $value->getClientOriginalName(),
163 $value->getClientMimeType(),
164 0,
165 UPLOAD_ERR_INI_SIZE,
166 true
167 );
168 } else {
169 $filtered[$key] = new UploadedFile(
170 $value->getPathname(),
171 $value->getClientOriginalName(),
172 $value->getClientMimeType(),
173 $value->getClientSize(),
174 $value->getError(),
175 true
176 );
177 }
178 }
179 }
180
181 return $filtered;
182 }
183
184 /**
185 * Converts the HttpKernel response to a BrowserKit response.
186 *
187 * @param Response $response A Response instance
188 *
189 * @return DomResponse A DomResponse instance
190 */
191 protected function filterResponse($response)
192 {
193 $headers = $response->headers->all();
194 if ($response->headers->getCookies()) {
195 $cookies = array();
196 foreach ($response->headers->getCookies() as $cookie) {
197 $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
198 }
199 $headers['Set-Cookie'] = $cookies;
200 }
201
202 // this is needed to support StreamedResponse
203 ob_start();
204 $response->sendContent();
205 $content = ob_get_clean();
206
207 return new DomResponse($content, $response->getStatusCode(), $headers);
208 }
209 }