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

Breadcrumb

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

class Esi

Esi implements the ESI capabilities to Request and Response instances.

For more information, read the following W3C notes:

  • ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang)
  • Edge Architecture Specification (http://www.w3.org/TR/edge-arch)

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\HttpKernel\HttpCache\AbstractSurrogate implements \Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
    • class \Symfony\Component\HttpKernel\HttpCache\Esi extends \Symfony\Component\HttpKernel\HttpCache\AbstractSurrogate

Expanded class hierarchy of Esi

3 string references to 'Esi'
Esi::getName in vendor/symfony/http-kernel/HttpCache/Esi.php
Returns surrogate name.
Esi::process in vendor/symfony/http-kernel/HttpCache/Esi.php
Replaces a Response Surrogate tags with the included resource content.
EsiFragmentRenderer::getName in vendor/symfony/http-kernel/Fragment/EsiFragmentRenderer.php
Gets the name of the strategy.

File

vendor/symfony/http-kernel/HttpCache/Esi.php, line 28

Namespace

Symfony\Component\HttpKernel\HttpCache
View source
class Esi extends AbstractSurrogate {
    public function getName() : string {
        return 'esi';
    }
    public function addSurrogateControl(Response $response) : void {
        if (str_contains($response->getContent(), '<esi:include')) {
            $response->headers
                ->set('Surrogate-Control', 'content="ESI/1.0"');
        }
    }
    public function renderIncludeTag(string $uri, ?string $alt = null, bool $ignoreErrors = true, string $comment = '') : string {
        $html = \sprintf('<esi:include src="%s"%s%s />', $uri, $ignoreErrors ? ' onerror="continue"' : '', $alt ? \sprintf(' alt="%s"', $alt) : '');
        if ($comment) {
            return \sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
        }
        return $html;
    }
    public function process(Request $request, Response $response) : Response {
        $type = $response->headers
            ->get('Content-Type');
        if (!$type) {
            $type = 'text/html';
        }
        $parts = explode(';', $type);
        if (!\in_array($parts[0], $this->contentTypes, true)) {
            return $response;
        }
        // we don't use a proper XML parser here as we can have ESI tags in a plain text response
        $content = $response->getContent();
        $content = preg_replace('#<esi\\:remove>.*?</esi\\:remove>#s', '', $content);
        $content = preg_replace('#<esi\\:comment[^>]+>#s', '', $content);
        $boundary = self::generateBodyEvalBoundary();
        $chunks = preg_split('#<esi\\:include\\s+(.*?)\\s*(?:/|</esi\\:include)>#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
        $i = 1;
        while (isset($chunks[$i])) {
            $options = [];
            preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER);
            foreach ($matches as $set) {
                $options[$set[1]] = $set[2];
            }
            if (!isset($options['src'])) {
                throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
            }
            $chunks[$i] = $boundary . $options['src'] . "\n" . ($options['alt'] ?? '') . "\n" . ('continue' === ($options['onerror'] ?? '')) . "\n";
            $i += 2;
        }
        $content = $boundary . implode('', $chunks) . $boundary;
        $response->setContent($content);
        $response->headers
            ->set('X-Body-Eval', 'ESI');
        // remove ESI/1.0 from the Surrogate-Control header
        $this->removeFromControl($response);
        return $response;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AbstractSurrogate::addSurrogateCapability public function Adds Surrogate-capability to the given Request. Overrides SurrogateInterface::addSurrogateCapability
AbstractSurrogate::createCacheStrategy public function Returns a new cache strategy instance. Overrides SurrogateInterface::createCacheStrategy
AbstractSurrogate::generateBodyEvalBoundary protected static function
AbstractSurrogate::handle public function Handles a Surrogate from the cache. Overrides SurrogateInterface::handle
AbstractSurrogate::hasSurrogateCapability public function Checks that at least one surrogate has Surrogate capability. Overrides SurrogateInterface::hasSurrogateCapability
AbstractSurrogate::needsParsing public function Checks that the Response needs to be parsed for Surrogate tags. Overrides SurrogateInterface::needsParsing
AbstractSurrogate::removeFromControl protected function Remove the Surrogate from the Surrogate-Control header.
AbstractSurrogate::__construct public function
Esi::addSurrogateControl public function Adds HTTP headers to specify that the Response needs to be parsed for Surrogate. Overrides SurrogateInterface::addSurrogateControl
Esi::getName public function Returns surrogate name. Overrides SurrogateInterface::getName
Esi::process public function Replaces a Response Surrogate tags with the included resource content. Overrides SurrogateInterface::process
Esi::renderIncludeTag public function Renders a Surrogate tag. Overrides SurrogateInterface::renderIncludeTag
RSS feed
Powered by Drupal