Attribute
2 years ago
Bundle
1 year ago
CacheClearer
2 years ago
CacheWarmer
1 year ago
Config
1 year ago
Controller
1 year ago
ControllerMetadata
1 year ago
DataCollector
1 year ago
Debug
1 year ago
DependencyInjection
1 year ago
Event
1 year ago
EventListener
1 year ago
Exception
1 year ago
Fragment
1 year ago
HttpCache
1 year ago
Log
1 year ago
Profiler
1 year ago
Resources
2 years ago
HttpClientKernel.php
1 year ago
HttpKernel.php
1 year ago
HttpKernelBrowser.php
1 year ago
HttpKernelInterface.php
1 year ago
Kernel.php
3 months ago
KernelEvents.php
2 years ago
KernelInterface.php
2 years ago
LICENSE
2 years ago
README.md
2 years ago
RebootableInterface.php
2 years ago
TerminableInterface.php
2 years ago
UriSigner.php
1 year ago
HttpKernelBrowser.php
175 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel; |
| 12 | |
| 13 | use Symfony\Component\BrowserKit\AbstractBrowser; |
| 14 | use Symfony\Component\BrowserKit\CookieJar; |
| 15 | use Symfony\Component\BrowserKit\History; |
| 16 | use Symfony\Component\BrowserKit\Request as DomRequest; |
| 17 | use Symfony\Component\BrowserKit\Response as DomResponse; |
| 18 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\File\UploadedFile; |
| 19 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 20 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response; |
| 21 | /** |
| 22 | * Simulates a browser and makes requests to an HttpKernel instance. |
| 23 | * |
| 24 | * @author Fabien Potencier <fabien@symfony.com> |
| 25 | * |
| 26 | * @method Request getRequest() |
| 27 | * @method Response getResponse() |
| 28 | */ |
| 29 | class HttpKernelBrowser extends AbstractBrowser |
| 30 | { |
| 31 | protected $kernel; |
| 32 | private $catchExceptions = \true; |
| 33 | /** |
| 34 | * @param array $server The server parameters (equivalent of $_SERVER) |
| 35 | */ |
| 36 | public function __construct(HttpKernelInterface $kernel, array $server = [], ?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 | * Sets whether to catch exceptions when the kernel is handling a request. |
| 45 | */ |
| 46 | public function catchExceptions(bool $catchExceptions) |
| 47 | { |
| 48 | $this->catchExceptions = $catchExceptions; |
| 49 | } |
| 50 | /** |
| 51 | * {@inheritdoc} |
| 52 | * |
| 53 | * @param Request $request |
| 54 | * |
| 55 | * @return Response |
| 56 | */ |
| 57 | protected function doRequest(object $request) |
| 58 | { |
| 59 | $response = $this->kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, $this->catchExceptions); |
| 60 | if ($this->kernel instanceof TerminableInterface) { |
| 61 | $this->kernel->terminate($request, $response); |
| 62 | } |
| 63 | return $response; |
| 64 | } |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | * |
| 68 | * @param Request $request |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected function getScript(object $request) |
| 73 | { |
| 74 | $kernel = var_export(serialize($this->kernel), \true); |
| 75 | $request = var_export(serialize($request), \true); |
| 76 | $errorReporting = error_reporting(); |
| 77 | $requires = ''; |
| 78 | foreach (get_declared_classes() as $class) { |
| 79 | if (0 === strpos($class, 'ComposerAutoloaderInit')) { |
| 80 | $r = new \ReflectionClass($class); |
| 81 | $file = \dirname($r->getFileName(), 2) . '/autoload.php'; |
| 82 | if (file_exists($file)) { |
| 83 | $requires .= 'require_once ' . var_export($file, \true) . ";\n"; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if (!$requires) { |
| 88 | throw new \RuntimeException('Composer autoloader not found.'); |
| 89 | } |
| 90 | $code = <<<EOF |
| 91 | <?php |
| 92 | |
| 93 | error_reporting({$errorReporting}); |
| 94 | |
| 95 | {$requires} |
| 96 | |
| 97 | \$kernel = unserialize({$kernel}); |
| 98 | \$request = unserialize({$request}); |
| 99 | EOF; |
| 100 | return $code . $this->getHandleScript(); |
| 101 | } |
| 102 | protected function getHandleScript() |
| 103 | { |
| 104 | return <<<'EOF' |
| 105 | $response = $kernel->handle($request); |
| 106 | |
| 107 | if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) { |
| 108 | $kernel->terminate($request, $response); |
| 109 | } |
| 110 | |
| 111 | echo serialize($response); |
| 112 | EOF; |
| 113 | } |
| 114 | /** |
| 115 | * {@inheritdoc} |
| 116 | * |
| 117 | * @return Request |
| 118 | */ |
| 119 | protected function filterRequest(DomRequest $request) |
| 120 | { |
| 121 | $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $server = $request->getServer(), $request->getContent()); |
| 122 | if (!isset($server['HTTP_ACCEPT'])) { |
| 123 | $httpRequest->headers->remove('Accept'); |
| 124 | } |
| 125 | foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) { |
| 126 | $httpRequest->files->set($key, $value); |
| 127 | } |
| 128 | return $httpRequest; |
| 129 | } |
| 130 | /** |
| 131 | * Filters an array of files. |
| 132 | * |
| 133 | * This method created test instances of UploadedFile so that the move() |
| 134 | * method can be called on those instances. |
| 135 | * |
| 136 | * If the size of a file is greater than the allowed size (from php.ini) then |
| 137 | * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE. |
| 138 | * |
| 139 | * @see UploadedFile |
| 140 | * |
| 141 | * @return array |
| 142 | */ |
| 143 | protected function filterFiles(array $files) |
| 144 | { |
| 145 | $filtered = []; |
| 146 | foreach ($files as $key => $value) { |
| 147 | if (\is_array($value)) { |
| 148 | $filtered[$key] = $this->filterFiles($value); |
| 149 | } elseif ($value instanceof UploadedFile) { |
| 150 | if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) { |
| 151 | $filtered[$key] = new UploadedFile('', $value->getClientOriginalName(), $value->getClientMimeType(), \UPLOAD_ERR_INI_SIZE, \true); |
| 152 | } else { |
| 153 | $filtered[$key] = new UploadedFile($value->getPathname(), $value->getClientOriginalName(), $value->getClientMimeType(), $value->getError(), \true); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | return $filtered; |
| 158 | } |
| 159 | /** |
| 160 | * {@inheritdoc} |
| 161 | * |
| 162 | * @param Response $response |
| 163 | * |
| 164 | * @return DomResponse |
| 165 | */ |
| 166 | protected function filterResponse(object $response) |
| 167 | { |
| 168 | // this is needed to support StreamedResponse |
| 169 | ob_start(); |
| 170 | $response->sendContent(); |
| 171 | $content = ob_get_clean(); |
| 172 | return new DomResponse($content, $response->getStatusCode(), $response->headers->all()); |
| 173 | } |
| 174 | } |
| 175 |