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

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 Enterprise License (PEL)
  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 PEL
  13.  */
  14. namespace Pimcore\Cache;
  15. final class Runtime extends \ArrayObject
  16. {
  17.     const SERVICE_ID __CLASS__;
  18.     protected static $tempInstance;
  19.     protected static $instance;
  20.     /**
  21.      * Array of indexes which are blocked from cache. If a given
  22.      * index is queried or set, an exception with the given message
  23.      * is thrown.
  24.      *
  25.      * @var array
  26.      */
  27.     private static $blockedIndexes = [
  28.         'pimcore_tag_block_current' => 'Index "%s" is now handled via "pimcore.document.tag.block_state_stack" service',
  29.         'pimcore_tag_block_numeration' => 'Index "%s" is now handled via "pimcore.document.tag.block_state_stack" service'
  30.     ];
  31.     /**
  32.      * Retrieves the default registry instance.
  33.      *
  34.      * @return self
  35.      */
  36.     public static function getInstance()
  37.     {
  38.         if (self::$instance) {
  39.             return self::$instance;
  40.         } elseif (\Pimcore::hasContainer()) {
  41.             $container = \Pimcore::getContainer();
  42.             /** @var self $instance */
  43.             $instance null;
  44.             if ($container->initialized(self::SERVICE_ID)) {
  45.                 $instance $container->get(self::SERVICE_ID);
  46.             } else {
  47.                 $instance = new self;
  48.                 $container->set(self::SERVICE_ID$instance);
  49.             }
  50.             self::$instance $instance;
  51.             if (self::$tempInstance) {
  52.                 // copy values from static temp. instance to the service instance
  53.                 foreach (self::$tempInstance as $key => $value) {
  54.                     $instance->offsetSet($key$value);
  55.                 }
  56.                 self::$tempInstance null;
  57.             }
  58.             return $instance;
  59.         } else {
  60.             // create a temp. instance
  61.             // this is necessary because the runtime cache is sometimes in use before the actual service container
  62.             // is initialized
  63.             if (!self::$tempInstance) {
  64.                 self::$tempInstance = new self;
  65.             }
  66.             return self::$tempInstance;
  67.         }
  68.     }
  69.     /**
  70.      * getter method, basically same as offsetGet().
  71.      *
  72.      * This method can be called from an object of type \Pimcore\Cache\Runtime, or it
  73.      * can be called statically.  In the latter case, it uses the default
  74.      * static instance stored in the class.
  75.      *
  76.      * @param string $index - get the value associated with $index
  77.      *
  78.      * @return mixed
  79.      *
  80.      * @throws \Exception if no entry is registered for $index.
  81.      */
  82.     public static function get($index)
  83.     {
  84.         $instance self::getInstance();
  85.         if (!$instance->offsetExists($index)) {
  86.             throw new \Exception("No entry is registered for key '$index'");
  87.         }
  88.         return $instance->offsetGet($index);
  89.     }
  90.     /**
  91.      * setter method, basically same as offsetSet().
  92.      *
  93.      * This method can be called from an object of type \Pimcore\Cache\Runtime, or it
  94.      * can be called statically.  In the latter case, it uses the default
  95.      * static instance stored in the class.
  96.      *
  97.      * @param string $index The location in the ArrayObject in which to store
  98.      *   the value.
  99.      * @param mixed $value The object to store in the ArrayObject.
  100.      *
  101.      * @return void
  102.      */
  103.     public static function set($index$value)
  104.     {
  105.         self::checkIndexes($index);
  106.         $instance self::getInstance();
  107.         $instance->offsetSet($index$value);
  108.     }
  109.     /**
  110.      * Returns TRUE if the $index is a named value in the registry,
  111.      * or FALSE if $index was not found in the registry.
  112.      *
  113.      * @param  string $index
  114.      *
  115.      * @return bool
  116.      */
  117.     public static function isRegistered($index)
  118.     {
  119.         $instance self::getInstance();
  120.         return $instance->offsetExists($index);
  121.     }
  122.     /**
  123.      * Constructs a parent ArrayObject with default
  124.      * ARRAY_AS_PROPS to allow access as an object
  125.      *
  126.      * @param array $array data array
  127.      * @param int $flags ArrayObject flags
  128.      */
  129.     public function __construct($array = [], $flags parent::ARRAY_AS_PROPS)
  130.     {
  131.         self::checkIndexes(array_keys($array));
  132.         parent::__construct($array$flags);
  133.     }
  134.     /**
  135.      * @inheritDoc
  136.      */
  137.     public function offsetSet($index$value)
  138.     {
  139.         self::checkIndexes($index);
  140.         parent::offsetSet($index$value);
  141.     }
  142.     /**
  143.      * @param string $index
  144.      *
  145.      * @return mixed
  146.      *
  147.      * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
  148.      */
  149.     public function offsetExists($index)
  150.     {
  151.         return array_key_exists($index$this);
  152.     }
  153.     /**
  154.      * Alias of self::set() to be compatible with Pimcore\Cache
  155.      *
  156.      * @param $data
  157.      * @param $id
  158.      *
  159.      * @return mixed
  160.      */
  161.     public static function save($data$id)
  162.     {
  163.         self::set($id$data);
  164.     }
  165.     /**
  166.      * Alias of self::get() to be compatible with Pimcore\Cache
  167.      *
  168.      * @param $id
  169.      *
  170.      * @return mixed
  171.      */
  172.     public static function load($id)
  173.     {
  174.         return self::get($id);
  175.     }
  176.     /**
  177.      * @param array $keepItems
  178.      */
  179.     public static function clear($keepItems = [])
  180.     {
  181.         self::$instance null;
  182.         $newInstance = new self();
  183.         $oldInstance self::getInstance();
  184.         foreach ($keepItems as $key) {
  185.             if ($oldInstance->offsetExists($key)) {
  186.                 $newInstance->offsetSet($key$oldInstance->offsetGet($key));
  187.             }
  188.         }
  189.         \Pimcore::getContainer()->set(self::SERVICE_ID$newInstance);
  190.         self::$instance $newInstance;
  191.     }
  192.     /**
  193.      * Check if index is in list of blocked indexes
  194.      *
  195.      * @param string|array $indexes
  196.      */
  197.     private static function checkIndexes($indexes)
  198.     {
  199.         if (!is_array($indexes)) {
  200.             $indexes = [$indexes];
  201.         }
  202.         foreach ($indexes as $index) {
  203.             if (isset(self::$blockedIndexes[$index])) {
  204.                 $message self::$blockedIndexes[$index];
  205.                 if (!$message) {
  206.                     $message 'Index "%s" is blocked by configuration';
  207.                 }
  208.                 if (false !== strpos($message'%s')) {
  209.                     $message sprintf($message$index);
  210.                 }
  211.                 throw new \InvalidArgumentException($message);
  212.             }
  213.         }
  214.     }
  215. }