vendor/pimcore/pimcore/lib/Cache/Runtime.php line 152

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\Cache;
  15. final class Runtime extends \ArrayObject
  16. {
  17.     private const SERVICE_ID __CLASS__;
  18.     /**
  19.      * @var self|null
  20.      */
  21.     protected static $tempInstance;
  22.     /**
  23.      * @var self|null
  24.      */
  25.     protected static $instance;
  26.     /**
  27.      * Retrieves the default registry instance.
  28.      *
  29.      * @return self
  30.      */
  31.     public static function getInstance(): self
  32.     {
  33.         if (self::$instance) {
  34.             return self::$instance;
  35.         }
  36.         if (\Pimcore::hasContainer()) {
  37.             $container \Pimcore::getContainer();
  38.             /** @var self $instance */
  39.             $instance null;
  40.             if ($container->initialized(self::SERVICE_ID)) {
  41.                 $instance $container->get(self::SERVICE_ID);
  42.             } else {
  43.                 $instance = new self;
  44.                 $container->set(self::SERVICE_ID$instance);
  45.             }
  46.             self::$instance $instance;
  47.             if (self::$tempInstance) {
  48.                 // copy values from static temp. instance to the service instance
  49.                 foreach (self::$tempInstance as $key => $value) {
  50.                     $instance->offsetSet($key$value);
  51.                 }
  52.                 self::$tempInstance null;
  53.             }
  54.             return $instance;
  55.         }
  56.         // create a temp. instance
  57.         // this is necessary because the runtime cache is sometimes in use before the actual service container
  58.         // is initialized
  59.         if (!self::$tempInstance) {
  60.             self::$tempInstance = new self;
  61.         }
  62.         return self::$tempInstance;
  63.     }
  64.     /**
  65.      * getter method, basically same as offsetGet().
  66.      *
  67.      * This method can be called from an object of type \Pimcore\Cache\Runtime, or it
  68.      * can be called statically.  In the latter case, it uses the default
  69.      * static instance stored in the class.
  70.      *
  71.      * @param string $index - get the value associated with $index
  72.      *
  73.      * @return mixed
  74.      *
  75.      * @throws \Exception if no entry is registered for $index.
  76.      */
  77.     public static function get($index)
  78.     {
  79.         $instance self::getInstance();
  80.         if (!$instance->offsetExists($index)) {
  81.             throw new \Exception("No entry is registered for key '$index'");
  82.         }
  83.         return $instance->offsetGet($index);
  84.     }
  85.     /**
  86.      * setter method, basically same as offsetSet().
  87.      *
  88.      * This method can be called from an object of type \Pimcore\Cache\Runtime, or it
  89.      * can be called statically.  In the latter case, it uses the default
  90.      * static instance stored in the class.
  91.      *
  92.      * @param string $index The location in the ArrayObject in which to store
  93.      *   the value.
  94.      * @param mixed $value The object to store in the ArrayObject.
  95.      *
  96.      * @return void
  97.      */
  98.     public static function set($index$value)
  99.     {
  100.         $instance self::getInstance();
  101.         $instance->offsetSet($index$value);
  102.     }
  103.     /**
  104.      * Returns TRUE if the $index is a named value in the registry,
  105.      * or FALSE if $index was not found in the registry.
  106.      *
  107.      * @param  string $index
  108.      *
  109.      * @return bool
  110.      */
  111.     public static function isRegistered($index)
  112.     {
  113.         $instance self::getInstance();
  114.         return $instance->offsetExists($index);
  115.     }
  116.     /**
  117.      * Constructs a parent ArrayObject with default
  118.      * ARRAY_AS_PROPS to allow access as an object
  119.      *
  120.      * @param array $array data array
  121.      * @param int $flags ArrayObject flags
  122.      */
  123.     public function __construct($array = [], $flags parent::ARRAY_AS_PROPS)
  124.     {
  125.         parent::__construct($array$flags);
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function offsetSet($index$value)
  131.     {
  132.         parent::offsetSet($index$value);
  133.     }
  134.     /**
  135.      * Alias of self::set() to be compatible with Pimcore\Cache
  136.      *
  137.      * @param mixed $data
  138.      * @param string $id
  139.      */
  140.     public static function save($data$id)
  141.     {
  142.         self::set($id$data);
  143.     }
  144.     /**
  145.      * Alias of self::get() to be compatible with Pimcore\Cache
  146.      *
  147.      * @param string $id
  148.      *
  149.      * @return mixed
  150.      */
  151.     public static function load($id)
  152.     {
  153.         return self::get($id);
  154.     }
  155.     /**
  156.      * @param array $keepItems
  157.      */
  158.     public static function clear($keepItems = [])
  159.     {
  160.         self::$instance null;
  161.         $newInstance = new self();
  162.         $oldInstance self::getInstance();
  163.         foreach ($keepItems as $key) {
  164.             if ($oldInstance->offsetExists($key)) {
  165.                 $newInstance->offsetSet($key$oldInstance->offsetGet($key));
  166.             }
  167.         }
  168.         \Pimcore::getContainer()->set(self::SERVICE_ID$newInstance);
  169.         self::$instance $newInstance;
  170.     }
  171. }