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