Projekt

Obecné

Profil

Stáhnout (6.85 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?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
class Client extends BaseClient
30
{
31
    protected $kernel;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param HttpKernelInterface $kernel    An HttpKernel instance
37
     * @param array               $server    The server parameters (equivalent of $_SERVER)
38
     * @param History             $history   A History instance to store the browser history
39
     * @param CookieJar           $cookieJar A CookieJar instance to store the cookies
40
     */
41
    public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
42
    {
43
        // These class properties must be set before calling the parent constructor, as it may depend on it.
44
        $this->kernel = $kernel;
45
        $this->followRedirects = false;
46
47
        parent::__construct($server, $history, $cookieJar);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @return Request|null A Request instance
54
     */
55
    public function getRequest()
56
    {
57
        return parent::getRequest();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @return Response|null A Response instance
64
     */
65
    public function getResponse()
66
    {
67
        return parent::getResponse();
68
    }
69
70
    /**
71
     * Makes a request.
72
     *
73
     * @param Request $request A Request instance
74
     *
75
     * @return Response A Response instance
76
     */
77
    protected function doRequest($request)
78
    {
79
        $response = $this->kernel->handle($request);
80
81
        if ($this->kernel instanceof TerminableInterface) {
82
            $this->kernel->terminate($request, $response);
83
        }
84
85
        return $response;
86
    }
87
88
    /**
89
     * Returns the script to execute when the request must be insulated.
90
     *
91
     * @param Request $request A Request instance
92
     *
93
     * @return string
94
     */
95
    protected function getScript($request)
96
    {
97
        $kernel = str_replace("'", "\\'", serialize($this->kernel));
98
        $request = str_replace("'", "\\'", serialize($request));
99
100
        $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
101
        $requirePath = str_replace("'", "\\'", $r->getFileName());
102
        $symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
103
        $errorReporting = error_reporting();
104
105
        $code = <<<EOF
106
<?php
107
108
error_reporting($errorReporting);
109
110
require_once '$requirePath';
111
112
\$loader = new Symfony\Component\ClassLoader\ClassLoader();
113
\$loader->addPrefix('Symfony', '$symfonyPath');
114
\$loader->register();
115
116
\$kernel = unserialize('$kernel');
117
\$request = unserialize('$request');
118
EOF;
119
120
        return $code.$this->getHandleScript();
121
    }
122
123
    protected function getHandleScript()
124
    {
125
        return <<<'EOF'
126
$response = $kernel->handle($request);
127
128
if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
129
    $kernel->terminate($request, $response);
130
}
131
132
echo serialize($response);
133
EOF;
134
    }
135
136
    /**
137
     * Converts the BrowserKit request to a HttpKernel request.
138
     *
139
     * @param DomRequest $request A DomRequest instance
140
     *
141
     * @return Request A Request instance
142
     */
143
    protected function filterRequest(DomRequest $request)
144
    {
145
        $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
146
147
        foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
148
            $httpRequest->files->set($key, $value);
149
        }
150
151
        return $httpRequest;
152
    }
153
154
    /**
155
     * Filters an array of files.
156
     *
157
     * This method created test instances of UploadedFile so that the move()
158
     * method can be called on those instances.
159
     *
160
     * If the size of a file is greater than the allowed size (from php.ini) then
161
     * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
162
     *
163
     * @see UploadedFile
164
     *
165
     * @param array $files An array of files
166
     *
167
     * @return array An array with all uploaded files marked as already moved
168
     */
169
    protected function filterFiles(array $files)
170
    {
171
        $filtered = array();
172
        foreach ($files as $key => $value) {
173
            if (is_array($value)) {
174
                $filtered[$key] = $this->filterFiles($value);
175
            } elseif ($value instanceof UploadedFile) {
176
                if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
177
                    $filtered[$key] = new UploadedFile(
178
                        '',
179
                        $value->getClientOriginalName(),
180
                        $value->getClientMimeType(),
181
                        0,
182
                        UPLOAD_ERR_INI_SIZE,
183
                        true
184
                    );
185
                } else {
186
                    $filtered[$key] = new UploadedFile(
187
                        $value->getPathname(),
188
                        $value->getClientOriginalName(),
189
                        $value->getClientMimeType(),
190
                        $value->getClientSize(),
191
                        $value->getError(),
192
                        true
193
                    );
194
                }
195
            }
196
        }
197
198
        return $filtered;
199
    }
200
201
    /**
202
     * Converts the HttpKernel response to a BrowserKit response.
203
     *
204
     * @param Response $response A Response instance
205
     *
206
     * @return DomResponse A DomResponse instance
207
     */
208
    protected function filterResponse($response)
209
    {
210
        $headers = $response->headers->all();
211
        if ($response->headers->getCookies()) {
212
            $cookies = array();
213
            foreach ($response->headers->getCookies() as $cookie) {
214
                $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
215
            }
216
            $headers['Set-Cookie'] = $cookies;
217
        }
218
219
        // this is needed to support StreamedResponse
220
        ob_start();
221
        $response->sendContent();
222
        $content = ob_get_clean();
223
224
        return new DomResponse($content, $response->getStatusCode(), $headers);
225
    }
226
}