vendor/pimcore/pimcore/lib/Twig/Extension/Templating/Placeholder/AbstractExtension.php line 128

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. /**
  15.  * ----------------------------------------------------------------------------------
  16.  * based on @author ZF1 Zend_View_Helper_Placeholder_Container_Standalone
  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 [email protected] 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\Twig\Extension\Templating\Placeholder;
  36. use Pimcore\Twig\Extension\Templating\Traits\HelperCharsetTrait;
  37. use Twig\Extension\RuntimeExtensionInterface;
  38. /**
  39.  * @method void set(mixed $value)
  40.  * @method void prepend(mixed $value)
  41.  * @method void append(mixed $value)
  42.  * @method Container setPrefix(string $prefix)
  43.  * @method string getPrefix()
  44.  * @method Container setPostfix(string $postfix)
  45.  * @method string getPostfix()
  46.  * @method Container setSeparator(string $separator)
  47.  * @method string getSeparator()
  48.  * @method Container setIndent(string|int $intent)
  49.  * @method string|int getIndent()
  50.  * @method string getWhitespace(string|int $indent)
  51.  * @method void captureStart($type = Container::APPEND, $key = null)
  52.  * @method void captureEnd()
  53.  *
  54.  */
  55. abstract class AbstractExtension implements \IteratorAggregate\Countable\ArrayAccessRuntimeExtensionInterface
  56. {
  57.     use HelperCharsetTrait;
  58.     /**
  59.      * @var ContainerService
  60.      */
  61.     protected $containerService;
  62.     /**
  63.      * @var Container
  64.      */
  65.     protected $_container;
  66.     /**
  67.      * Registry key under which container registers itself
  68.      *
  69.      * @var string
  70.      */
  71.     protected $_regKey;
  72.     /**
  73.      * Flag whether to automatically escape output, must also be
  74.      * enforced in the child class if __toString/toString is overwritten
  75.      *
  76.      * @var bool
  77.      */
  78.     protected $_autoEscape true;
  79.     public function __construct(ContainerService $containerService)
  80.     {
  81.         $this->containerService $containerService;
  82.     }
  83.     /**
  84.      * Set whether or not auto escaping should be used
  85.      *
  86.      * @param  bool $autoEscape whether or not to auto escape output
  87.      *
  88.      * @return AbstractExtension
  89.      */
  90.     public function setAutoEscape($autoEscape true)
  91.     {
  92.         $this->_autoEscape = ($autoEscape) ? true false;
  93.         return $this;
  94.     }
  95.     /**
  96.      * Return whether autoEscaping is enabled or disabled
  97.      *
  98.      * return bool
  99.      */
  100.     public function getAutoEscape()
  101.     {
  102.         return $this->_autoEscape;
  103.     }
  104.     /**
  105.      * Escape a string
  106.      *
  107.      * @param  string $string
  108.      *
  109.      * @return string
  110.      */
  111.     protected function _escape($string)
  112.     {
  113.         return htmlspecialchars((string) $string);
  114.     }
  115.     /**
  116.      * Set container on which to operate
  117.      *
  118.      * @param  Container $container
  119.      *
  120.      * @return AbstractExtension
  121.      */
  122.     public function setContainer(Container $container)
  123.     {
  124.         $this->containerService->setContainer($this->_regKey$container);
  125.         return $this;
  126.     }
  127.     /**
  128.      * Retrieve placeholder container
  129.      *
  130.      * @return Container
  131.      */
  132.     public function getContainer()
  133.     {
  134.         return $this->containerService->getContainer($this->_regKey);
  135.     }
  136.     /**
  137.      * Overloading: set property value
  138.      *
  139.      * @param  string $key
  140.      * @param  mixed $value
  141.      *
  142.      * @return void
  143.      */
  144.     public function __set($key$value)
  145.     {
  146.         $container $this->getContainer();
  147.         $container[$key] = $value;
  148.     }
  149.     /**
  150.      * Overloading: retrieve property
  151.      *
  152.      * @param  string $key
  153.      *
  154.      * @return mixed
  155.      */
  156.     public function __get($key)
  157.     {
  158.         $container $this->getContainer();
  159.         if (isset($container[$key])) {
  160.             return $container[$key];
  161.         }
  162.         return null;
  163.     }
  164.     /**
  165.      * Overloading: check if property is set
  166.      *
  167.      * @param  string $key
  168.      *
  169.      * @return bool
  170.      */
  171.     public function __isset($key)
  172.     {
  173.         $container $this->getContainer();
  174.         return isset($container[$key]);
  175.     }
  176.     /**
  177.      * Overloading: unset property
  178.      *
  179.      * @param  string $key
  180.      *
  181.      * @return void
  182.      */
  183.     public function __unset($key)
  184.     {
  185.         $container $this->getContainer();
  186.         if (isset($container[$key])) {
  187.             unset($container[$key]);
  188.         }
  189.     }
  190.     /**
  191.      * Overload
  192.      *
  193.      * Proxy to container methods
  194.      *
  195.      * @param  string $method
  196.      * @param  array $args
  197.      *
  198.      * @return mixed
  199.      */
  200.     public function __call($method$args)
  201.     {
  202.         $container $this->getContainer();
  203.         if (method_exists($container$method)) {
  204.             $return call_user_func_array([$container$method], $args);
  205.             if ($return === $container) {
  206.                 // If the container is returned, we really want the current object
  207.                 return $this;
  208.             }
  209.             return $return;
  210.         }
  211.         throw new Exception('Method "' $method '" does not exist');
  212.     }
  213.     /**
  214.      * String representation
  215.      *
  216.      * @return string
  217.      */
  218.     public function toString()
  219.     {
  220.         return $this->getContainer()->toString();
  221.     }
  222.     /**
  223.      * Cast to string representation
  224.      *
  225.      * @return string
  226.      */
  227.     public function __toString()
  228.     {
  229.         return $this->toString();
  230.     }
  231.     /**
  232.      * Countable
  233.      *
  234.      * @return int
  235.      */
  236.     public function count()
  237.     {
  238.         $container $this->getContainer();
  239.         return count($container);
  240.     }
  241.     /**
  242.      * ArrayAccess: offsetExists
  243.      *
  244.      * @param  string|int $offset
  245.      *
  246.      * @return bool
  247.      */
  248.     public function offsetExists($offset)
  249.     {
  250.         return $this->getContainer()->offsetExists($offset);
  251.     }
  252.     /**
  253.      * ArrayAccess: offsetGet
  254.      *
  255.      * @param  string|int $offset
  256.      *
  257.      * @return mixed
  258.      */
  259.     public function offsetGet($offset)
  260.     {
  261.         return $this->getContainer()->offsetGet($offset);
  262.     }
  263.     /**
  264.      * ArrayAccess: offsetSet
  265.      *
  266.      * @param  string|int $offset
  267.      * @param  mixed $value
  268.      *
  269.      * @return void
  270.      */
  271.     public function offsetSet($offset$value)
  272.     {
  273.         $this->getContainer()->offsetSet($offset$value);
  274.     }
  275.     /**
  276.      * ArrayAccess: offsetUnset
  277.      *
  278.      * @param  string|int $offset
  279.      *
  280.      * @return void
  281.      */
  282.     public function offsetUnset($offset)
  283.     {
  284.         $this->getContainer()->offsetUnset($offset);
  285.     }
  286.     /**
  287.      * IteratorAggregate: get Iterator
  288.      *
  289.      * @return \Iterator
  290.      */
  291.     public function getIterator()
  292.     {
  293.         return $this->getContainer()->getIterator();
  294.     }
  295. }