Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. HttpKernelBrowser.php

class HttpKernelBrowser

Simulates a browser and makes requests to an HttpKernel instance.

@author Fabien Potencier <fabien@symfony.com>

@template-extends AbstractBrowser<Request, Response>

Hierarchy

  • class \Symfony\Component\BrowserKit\AbstractBrowser
    • class \Symfony\Component\HttpKernel\HttpKernelBrowser extends \Symfony\Component\BrowserKit\AbstractBrowser

Expanded class hierarchy of HttpKernelBrowser

1 file declares its use of HttpKernelBrowser
BrowserKitDriver.php in vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php

File

vendor/symfony/http-kernel/HttpKernelBrowser.php, line 30

Namespace

Symfony\Component\HttpKernel
View source
class HttpKernelBrowser extends AbstractBrowser {
    private bool $catchExceptions = true;
    
    /**
     * @param array $server The server parameters (equivalent of $_SERVER)
     */
    public function __construct(HttpKernelInterface $kernel, array $server = [], ?History $history = null, ?CookieJar $cookieJar = null) {
        // These class properties must be set before calling the parent constructor, as it may depend on it.
        $this->followRedirects = false;
        parent::__construct($server, $history, $cookieJar);
    }
    
    /**
     * Sets whether to catch exceptions when the kernel is handling a request.
     */
    public function catchExceptions(bool $catchExceptions) : void {
        $this->catchExceptions = $catchExceptions;
    }
    
    /**
     * @param Request $request
     */
    protected function doRequest(object $request) : Response {
        $response = $this->kernel
            ->handle($request, HttpKernelInterface::MAIN_REQUEST, $this->catchExceptions);
        if ($this->kernel instanceof TerminableInterface) {
            $this->kernel
                ->terminate($request, $response);
        }
        return $response;
    }
    
    /**
     * @param Request $request
     */
    protected function getScript(object $request) : string {
        $kernel = var_export(serialize($this->kernel), true);
        $request = var_export(serialize($request), true);
        $errorReporting = error_reporting();
        $requires = '';
        foreach (get_declared_classes() as $class) {
            if (str_starts_with($class, 'ComposerAutoloaderInit')) {
                $r = new \ReflectionClass($class);
                $file = \dirname($r->getFileName(), 2) . '/autoload.php';
                if (file_exists($file)) {
                    $requires .= 'require_once ' . var_export($file, true) . ";\n";
                }
            }
        }
        if (!$requires) {
            throw new \RuntimeException('Composer autoloader not found.');
        }
        $code = <<<EOF
<?php

error_reporting({<span class="php-variable">$errorReporting</span>});

{<span class="php-variable">$requires</span>}

\$kernel = unserialize({<span class="php-variable">$kernel</span>});
\$request = unserialize({<span class="php-variable">$request</span>});
EOF;
        return $code . $this->getHandleScript();
    }
    protected function getHandleScript() : string {
        return <<<'EOF'
$response = $kernel->handle($request);

if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
    $kernel->terminate($request, $response);
}

echo serialize($response);
EOF;
    }
    protected function filterRequest(DomRequest $request) : Request {
        $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $server = $request->getServer(), $request->getContent());
        if (!isset($server['HTTP_ACCEPT'])) {
            $httpRequest->headers
                ->remove('Accept');
        }
        foreach ($this->filterFiles($httpRequest->files
            ->all()) as $key => $value) {
            $httpRequest->files
                ->set($key, $value);
        }
        return $httpRequest;
    }
    
    /**
     * Filters an array of files.
     *
     * This method created test instances of UploadedFile so that the move()
     * method can be called on those instances.
     *
     * If the size of a file is greater than the allowed size (from php.ini) then
     * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
     *
     * @see UploadedFile
     */
    protected function filterFiles(array $files) : array {
        $filtered = [];
        foreach ($files as $key => $value) {
            if (\is_array($value)) {
                $filtered[$key] = $this->filterFiles($value);
            }
            elseif ($value instanceof UploadedFile) {
                if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
                    $filtered[$key] = new UploadedFile('', $value->getClientOriginalName(), $value->getClientMimeType(), \UPLOAD_ERR_INI_SIZE, true);
                }
                else {
                    $filtered[$key] = new UploadedFile($value->getPathname(), $value->getClientOriginalName(), $value->getClientMimeType(), $value->getError(), true);
                }
            }
        }
        return $filtered;
    }
    
    /**
     * @param Response $response
     */
    protected function filterResponse(object $response) : DomResponse {
        // this is needed to support StreamedResponse
        ob_start();
        $response->sendContent();
        $content = ob_get_clean();
        return new DomResponse($content, $response->getStatusCode(), $response->headers
            ->all());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractBrowser::$cookieJar protected property
AbstractBrowser::$crawler protected property
AbstractBrowser::$followMetaRefresh protected property
AbstractBrowser::$followRedirects protected property
AbstractBrowser::$history protected property
AbstractBrowser::$insulated protected property
AbstractBrowser::$internalRequest protected property
AbstractBrowser::$internalResponse protected property
AbstractBrowser::$isMainRequest private property
AbstractBrowser::$maxRedirects private property
AbstractBrowser::$redirect protected property
AbstractBrowser::$redirectCount private property
AbstractBrowser::$redirects private property
AbstractBrowser::$request protected property @psalm-var TRequest
AbstractBrowser::$response protected property @psalm-var TResponse
AbstractBrowser::$server protected property
AbstractBrowser::$useHtml5Parser protected property
AbstractBrowser::back public function Goes back in the browser history.
AbstractBrowser::click public function Clicks on a given link.
AbstractBrowser::clickLink public function Clicks the first link (or clickable image) that contains the given text.
AbstractBrowser::createCrawlerFromContent protected function Creates a crawler.
AbstractBrowser::doRequestInProcess protected function Makes a request in another process.
AbstractBrowser::extractHost private function
AbstractBrowser::followMetaRefresh public function Sets whether to automatically follow meta refresh redirects or not.
AbstractBrowser::followRedirect public function Follow redirects?
AbstractBrowser::followRedirects public function Sets whether to automatically follow redirects or not.
AbstractBrowser::forward public function Goes forward in the browser history.
AbstractBrowser::getAbsoluteUri protected function Takes a URI and converts it to absolute if it is not already absolute.
AbstractBrowser::getCookieJar public function Returns the CookieJar instance.
AbstractBrowser::getCrawler public function Returns the current Crawler instance.
AbstractBrowser::getHistory public function Returns the History instance.
AbstractBrowser::getInternalRequest public function Returns the current BrowserKit Request instance.
AbstractBrowser::getInternalResponse public function Returns the current BrowserKit Response instance.
AbstractBrowser::getMaxRedirects public function Returns the maximum number of redirects that crawler can follow.
AbstractBrowser::getMetaRefreshUrl private function
AbstractBrowser::getRequest public function Returns the current origin Request instance.
AbstractBrowser::getResponse public function Returns the current origin response instance.
AbstractBrowser::getServerParameter public function Gets single server parameter for specified key.
AbstractBrowser::insulate public function Sets the insulated flag.
AbstractBrowser::isFollowingRedirects public function Returns whether client automatically follows redirects or not.
AbstractBrowser::jsonRequest public function Converts the request parameters into a JSON string and uses it as request content.
AbstractBrowser::reload public function Reloads the current browser.
AbstractBrowser::request public function Calls a URI.
AbstractBrowser::requestFromRequest protected function Makes a request from a Request object directly.
AbstractBrowser::restart public function Restarts the client.
AbstractBrowser::setMaxRedirects public function Sets the maximum number of redirects that crawler can follow.
AbstractBrowser::setServerParameter public function Sets single server parameter.
AbstractBrowser::setServerParameters public function Sets server parameters.
AbstractBrowser::submit public function Submits a form.
AbstractBrowser::submitForm public function Finds the first form that contains a button with the given content and
uses it to submit the given form field values.
AbstractBrowser::updateServerFromUri private function
AbstractBrowser::useHtml5Parser public function Sets whether parsing should be done using &quot;masterminds/html5&quot;.
AbstractBrowser::xmlHttpRequest public function
HttpKernelBrowser::$catchExceptions private property
HttpKernelBrowser::catchExceptions public function Sets whether to catch exceptions when the kernel is handling a request.
HttpKernelBrowser::doRequest protected function Overrides AbstractBrowser::doRequest
HttpKernelBrowser::filterFiles protected function Filters an array of files.
HttpKernelBrowser::filterRequest protected function Filters the BrowserKit request to the origin one. Overrides AbstractBrowser::filterRequest
HttpKernelBrowser::filterResponse protected function Overrides AbstractBrowser::filterResponse
HttpKernelBrowser::getHandleScript protected function
HttpKernelBrowser::getScript protected function Overrides AbstractBrowser::getScript
HttpKernelBrowser::__construct public function Overrides AbstractBrowser::__construct
RSS feed
Powered by Drupal