-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Client.php
119 lines (101 loc) · 3.83 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Symfony\Components\HttpKernel;
use Symfony\Components\HttpKernel\HttpKernelInterface;
use Symfony\Components\HttpFoundation\Request;
use Symfony\Components\BrowserKit\Client as BaseClient;
use Symfony\Components\BrowserKit\Request as DomRequest;
use Symfony\Components\BrowserKit\Response as DomResponse;
use Symfony\Components\BrowserKit\History;
use Symfony\Components\BrowserKit\CookieJar;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Client simulates a browser and makes requests to a Kernel object.
*
* @package Symfony
* @subpackage Components_HttpKernel
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Client extends BaseClient
{
protected $kernel;
/**
* Constructor.
*
* @param Symfony\Components\HttpKernel\HttpKernelInterface $kernel An HttpKernel instance
* @param array $server The server parameters (equivalent of $_SERVER)
* @param Symfony\Components\BrowserKit\History $history A History instance to store the browser history
* @param Symfony\Components\BrowserKit\CookieJar $cookieJar A CookieJar instance to store the cookies
*/
public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
{
$this->kernel = $kernel;
parent::__construct($server, $history, $cookieJar);
$this->followRedirects = false;
}
/**
* Makes a request.
*
* @param Symfony\Components\HttpFoundation\Request $request A Request instance
*
* @param Symfony\Components\HttpFoundation\Response $response A Response instance
*/
protected function doRequest($request)
{
return $this->kernel->handle($request);
}
/**
* Returns the script to execute when the request must be insulated.
*
* @param Symfony\Components\HttpFoundation\Request $request A Request instance
*/
protected function getScript($request)
{
$kernel = serialize($this->kernel);
$request = serialize($request);
$r = new \ReflectionClass('\\Symfony\\Framework\\UniversalClassLoader');
$requirePath = $r->getFileName();
$symfonyPath = realpath(__DIR__.'/../../..');
return <<<EOF
<?php
require_once '$requirePath';
\$loader = new Symfony\Framework\UniversalClassLoader();
\$loader->registerNamespaces(array('Symfony' => '$symfonyPath'));
\$loader->register();
\$kernel = unserialize('$kernel');
echo serialize(\$kernel->handle(unserialize('$request')));
EOF;
}
/**
* Converts the BrowserKit request to a HttpKernel request.
*
* @param Symfony\Components\BrowserKit\Request $request A Request instance
*
* @return Symfony\Components\HttpFoundation\Request A Request instance
*/
protected function filterRequest(DomRequest $request)
{
$uri = $request->getUri();
if (preg_match('#^https?\://([^/]+)/(.*)$#', $uri, $matches)) {
$uri = '/'.$matches[2];
}
return Request::create($uri, $request->getMethod(), $request->getParameters(), $request->getFiles(), $request->getCookies(), $request->getServer());
}
/**
* Converts the HttpKernel response to a BrowserKit response.
*
* @param Symfony\Components\HttpFoundation\Response $response A Response instance
*
* @return Symfony\Components\BrowserKit\Response A Response instance
*/
protected function filterResponse($response)
{
return new DomResponse($response->getContent(), $response->getStatusCode(), $response->headers->all());
}
}