vendor/pimcore/pimcore/lib/Templating/Helper/Placeholder/ContainerService.php line 99

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. /**
  15.  * ----------------------------------------------------------------------------------
  16.  * based on @author ZF1 Zend_View_Helper_Placeholder_Registry
  17.  * ----------------------------------------------------------------------------------
  18.  */
  19. /**
  20.  * Zend Framework
  21.  *
  22.  * LICENSE
  23.  *
  24.  * This source file is subject to the new BSD license that is bundled
  25.  * with this package in the file LICENSE.txt.
  26.  * It is also available through the world-wide-web at this URL:
  27.  * http://framework.zend.com/license/new-bsd
  28.  * If you did not receive a copy of the license and are unable to
  29.  * obtain it through the world-wide-web, please send an email
  30.  * to license@zend.com so we can send you a copy immediately.
  31.  *
  32.  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  34.  */
  35. namespace Pimcore\Templating\Helper\Placeholder;
  36. use Pimcore\Event\DocumentEvents;
  37. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  38. /**
  39.  * Registry for placeholder containers
  40.  */
  41. class ContainerService
  42. {
  43.     /**
  44.      * @var EventDispatcherInterface
  45.      */
  46.     private $eventDispatcher;
  47.     /**
  48.      * @var int
  49.      */
  50.     private $currentIndex 0;
  51.     /**
  52.      * Placeholder containers
  53.      *
  54.      * @var array
  55.      */
  56.     protected $_items = [];
  57.     public function __construct()
  58.     {
  59.         $this->_items[$this->currentIndex] = [];
  60.     }
  61.     /**
  62.      * TODO Pimcore 6 set event dispatcher as constructor parameter
  63.      *
  64.      * @internal
  65.      * @required
  66.      *
  67.      * @param EventDispatcherInterface $eventDispatcher
  68.      */
  69.     public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
  70.     {
  71.         $this->eventDispatcher $eventDispatcher;
  72.         // lazily add event listeners - these listeners are only added if the container service is actually built
  73.         // when rendering a new document, the index is pushed to create a new, empty context
  74.         $eventDispatcher->addListener(DocumentEvents::RENDERER_PRE_RENDER, [$this'pushIndex']);
  75.         $eventDispatcher->addListener(DocumentEvents::RENDERER_POST_RENDER, [$this'popIndex']);
  76.     }
  77.     public function pushIndex()
  78.     {
  79.         ++$this->currentIndex;
  80.         if (isset($this->_items[$this->currentIndex])) {
  81.             throw new \RuntimeException(sprintf('Items at index %d already exist'$this->currentIndex));
  82.         }
  83.         $this->_items[$this->currentIndex] = [];
  84.     }
  85.     public function popIndex()
  86.     {
  87.         if (=== $this->currentIndex) {
  88.             throw new \OutOfBoundsException('Current index is already at 0');
  89.         }
  90.         if (isset($this->_items[$this->currentIndex])) {
  91.             unset($this->_items[$this->currentIndex]);
  92.         }
  93.         --$this->currentIndex;
  94.     }
  95.     /**
  96.      * createContainer
  97.      *
  98.      * @param  string $key
  99.      * @param  array $value
  100.      *
  101.      * @return Container
  102.      */
  103.     public function createContainer($key, array $value = [])
  104.     {
  105.         $key = (string) $key;
  106.         $this->_items[$this->currentIndex][$key] = new Container($value);
  107.         return $this->_items[$this->currentIndex][$key];
  108.     }
  109.     /**
  110.      * Retrieve a placeholder container
  111.      *
  112.      * @param  string $key
  113.      *
  114.      * @return Container
  115.      */
  116.     public function getContainer($key)
  117.     {
  118.         $key = (string) $key;
  119.         if (isset($this->_items[$this->currentIndex][$key])) {
  120.             return $this->_items[$this->currentIndex][$key];
  121.         }
  122.         $container $this->createContainer($key);
  123.         return $container;
  124.     }
  125.     /**
  126.      * Does a particular container exist?
  127.      *
  128.      * @param  string $key
  129.      *
  130.      * @return bool
  131.      */
  132.     public function containerExists($key)
  133.     {
  134.         $key = (string) $key;
  135.         $return array_key_exists($key$this->_items[$this->currentIndex]);
  136.         return $return;
  137.     }
  138.     /**
  139.      * Set the container for an item in the registry
  140.      *
  141.      * @param  string $key
  142.      * @param  Container $container
  143.      *
  144.      * @return ContainerService
  145.      */
  146.     public function setContainer($keyContainer $container)
  147.     {
  148.         $key = (string) $key;
  149.         $this->_items[$this->currentIndex][$key] = $container;
  150.         return $this;
  151.     }
  152.     /**
  153.      * Delete a container
  154.      *
  155.      * @param  string $key
  156.      *
  157.      * @return bool
  158.      */
  159.     public function deleteContainer($key)
  160.     {
  161.         $key = (string) $key;
  162.         if (isset($this->_items[$this->currentIndex][$key])) {
  163.             unset($this->_items[$this->currentIndex][$key]);
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168. }