MergePlugin.php 1.78 KB
Newer Older
Ketan's avatar
Ketan committed
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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\PageCache\Model\Layout;

use Magento\Framework\View\EntitySpecificHandlesList;

/**
 * Plugin for @see \Magento\Framework\View\Model\Layout\Merge
 */
class MergePlugin
{
    /**
     * @var EntitySpecificHandlesList
     */
    private $entitySpecificHandlesList;

    /**
     * Constructor
     *
     * @param EntitySpecificHandlesList $entitySpecificHandlesList
     */
    public function __construct(
        EntitySpecificHandlesList $entitySpecificHandlesList
    ) {
        $this->entitySpecificHandlesList = $entitySpecificHandlesList;
    }

    /**
     * Make sure that page specific handles (those which contain entity ID) do not have any declarations of ESI blocks
     *
     * ESI blocks cannot be declared for page specific handles, otherwise they will not be shared between pages
     *
     * @param \Magento\Framework\View\Model\Layout\Merge $subject
     * @param string $handle
     * @param string $updateXml
     * @return array|null
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function beforeValidateUpdate(\Magento\Framework\View\Model\Layout\Merge $subject, $handle, $updateXml)
    {
        if (in_array($handle, $this->entitySpecificHandlesList->getHandles())
            && (strpos($updateXml, 'ttl=') !== false)
        ) {
            throw new \LogicException(
                "Handle '{$handle}' must not contain blocks with 'ttl' attribute specified. "
                . "Otherwise, these blocks will be treated as ESI by Varnish, however will not be shared between pages "
                . "because handle '{$handle}' is not generic. Such blocks will not be rendered on the page"
            );
        }
        return null;
    }
}