vendor/pimcore/pimcore/lib/Twig/Extension/CacheExtension.php line 153

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Twig\Extension;
  15. use Pimcore\Cache as CacheManager;
  16. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\TwigFunction;
  19. /**
  20.  * @internal
  21.  */
  22. class CacheExtension extends AbstractExtension
  23. {
  24.     /**
  25.      * @var string
  26.      */
  27.     protected $key;
  28.     /**
  29.      * @var bool[]
  30.      */
  31.     protected $captureEnabled = [];
  32.     /**
  33.      * @var bool
  34.      */
  35.     protected $force false;
  36.     /**
  37.      * @var int
  38.      */
  39.     protected $lifetime;
  40.     /**
  41.      * @var EditmodeResolver
  42.      */
  43.     protected $editmodeResolver;
  44.     public function __construct(EditmodeResolver $editmodeResolver)
  45.     {
  46.         $this->editmodeResolver $editmodeResolver;
  47.     }
  48.     public function getFunctions(): array
  49.     {
  50.         return [
  51.             new TwigFunction('pimcore_cache', [$this'init'], ['is_safe' => ['html']]),
  52.         ];
  53.     }
  54.     /**
  55.      * @param string $name
  56.      * @param int|null $lifetime
  57.      * @param bool $force
  58.      *
  59.      * @return $this
  60.      */
  61.     public function init($name$lifetime null$force false)
  62.     {
  63.         $this->key 'pimcore_viewcache_' $name;
  64.         $this->force $force;
  65.         if (!$lifetime) {
  66.             $lifetime null;
  67.         }
  68.         $this->lifetime $lifetime;
  69.         return $this;
  70.     }
  71.     /**
  72.      * @return bool
  73.      */
  74.     public function start()
  75.     {
  76.         if (\Pimcore\Tool::isFrontendRequestByAdmin() && !$this->force) {
  77.             return false;
  78.         }
  79.         if ($content CacheManager::load($this->key)) {
  80.             $this->outputContent($content$this->keytrue);
  81.             return true;
  82.         }
  83.         $this->captureEnabled[$this->key] = true;
  84.         ob_start();
  85.         return false;
  86.     }
  87.     /**
  88.      *  @return void
  89.      */
  90.     public function end()
  91.     {
  92.         if ($this->captureEnabled[$this->key] ?? false) {
  93.             $this->captureEnabled[$this->key] = false;
  94.             $tags = ['in_template'];
  95.             if (!$this->lifetime) {
  96.                 $tags[] = 'output';
  97.             }
  98.             $content ob_get_clean();
  99.             $this->saveContentToCache($content$this->key$tags);
  100.             $this->outputContent($content$this->keyfalse);
  101.         }
  102.     }
  103.     /**
  104.      *  @return void
  105.      */
  106.     public function stop()
  107.     {
  108.         $this->end();
  109.     }
  110.     /**
  111.      * Output the content.
  112.      *
  113.      * @param string $content the content, either rendered or retrieved directly from the cache.
  114.      * @param string $key the cache key
  115.      * @param bool $isLoadedFromCache true if the content origins from the cache and hasn't been created "live".
  116.      */
  117.     protected function outputContent($contentstring $keybool $isLoadedFromCache)
  118.     {
  119.         echo $content;
  120.     }
  121.     /**
  122.      * Save the (rendered) content to to cache. May be overriden to add custom markup / code, or specific tags, etc.
  123.      *
  124.      * @param string $content
  125.      * @param string $key
  126.      * @param array $tags
  127.      */
  128.     protected function saveContentToCache($contentstring $key, array $tags)
  129.     {
  130.         CacheManager::save($content$key$tags$this->lifetime996true);
  131.     }
  132. }