vendor/pimcore/pimcore/lib/Templating/PhpEngine.php line 127

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\Templating;
  15. use Pimcore\Config\Config;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Model\Document\Tag;
  18. use Pimcore\Templating\Helper\Cache;
  19. use Pimcore\Templating\Helper\Glossary;
  20. use Pimcore\Templating\Helper\HeadLink;
  21. use Pimcore\Templating\Helper\HeadMeta;
  22. use Pimcore\Templating\Helper\HeadScript;
  23. use Pimcore\Templating\Helper\HeadStyle;
  24. use Pimcore\Templating\Helper\HeadTitle;
  25. use Pimcore\Templating\Helper\InlineScript;
  26. use Pimcore\Templating\Helper\Navigation;
  27. use Pimcore\Templating\Helper\Placeholder\Container;
  28. use Pimcore\Templating\Helper\WebLink;
  29. use Pimcore\Templating\HelperBroker\HelperBrokerInterface;
  30. use Pimcore\Templating\Model\ViewModel;
  31. use Pimcore\Templating\Model\ViewModelInterface;
  32. use Pimcore\Tool\DeviceDetector;
  33. use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
  34. use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
  35. use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper;
  36. use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
  37. use Symfony\Bundle\FrameworkBundle\Templating\Helper\RequestHelper;
  38. use Symfony\Bundle\FrameworkBundle\Templating\Helper\RouterHelper;
  39. use Symfony\Bundle\FrameworkBundle\Templating\Helper\SessionHelper;
  40. use Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper;
  41. use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
  42. use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine as BasePhpEngine;
  43. use Symfony\Bundle\SecurityBundle\Templating\Helper\SecurityHelper;
  44. use Symfony\Component\HttpFoundation\Request;
  45. use Symfony\Component\Templating\Helper\SlotsHelper;
  46. use Symfony\Component\Templating\Storage\Storage;
  47. /**
  48.  * Symfony PHP engine with pimcore additions:
  49.  *
  50.  *  - property access - $this->variable and $this->helper()
  51.  *  - helper brokers integrate other view helpers (ZF) on __call
  52.  *  - tag integration
  53.  *
  54.  * Defined in \Pimcore\Templating\HelperBroker\HelperShortcuts
  55.  *
  56.  * @method string getLocale()
  57.  * @method Request getRequest()
  58.  * @method SlotsHelper slots()
  59.  * @method string path($name, $parameters = array(), $relative = false)
  60.  * @method string url($name, $parameters = array(), $schemeRelative = false)
  61.  * @method string t($key, $parameters = [], $domain = null, $locale = null)
  62.  *
  63.  * Symfony core helpers
  64.  * @method ActionsHelper actions()
  65.  * @method AssetsHelper assets()
  66.  * @method CodeHelper code()
  67.  * @method FormHelper form()
  68.  * @method RequestHelper request()
  69.  * @method RouterHelper router()
  70.  * @method SecurityHelper security()
  71.  * @method SessionHelper session()
  72.  * @method StopwatchHelper stopwatch()
  73.  * @method TranslatorHelper translator()
  74.  *
  75.  * Pimcore helpers
  76.  * @method string action($action, $controller, $module, array $params = [])
  77.  * @method Cache cache($name, $lifetime = null, $force = false)
  78.  * @method DeviceDetector device($default = null)
  79.  * @method array getAllParams()
  80.  * @method mixed getParam($key, $default = null)
  81.  * @method Glossary glossary()
  82.  * @method Container placeholder($placeholderName)
  83.  * @method HeadLink headLink(array $attributes = null, $placement = Container::APPEND)
  84.  * @method HeadMeta headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Container::APPEND)
  85.  * @method HeadScript headScript($mode = HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
  86.  * @method HeadStyle headStyle($content = null, $placement = 'APPEND', $attributes = array())
  87.  * @method HeadTitle headTitle($title = null, $setType = null)
  88.  * @method string inc($include, array $params = [], $cacheEnabled = true, $editmode = null)
  89.  * @method InlineScript inlineScript($mode = HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
  90.  * @method WebLink webLink()
  91.  * @method Navigation navigation()
  92.  * @method Config|mixed websiteConfig($key = null, $default = null)
  93.  * @method string pimcoreUrl(array $urlOptions = [], $name = null, $reset = false, $encode = true, $relative = false)
  94.  * @method string translate($key, $parameters = [], $domain = null, $locale = null)
  95.  *
  96.  * Pimcore editables
  97.  * @method Tag\Area area($name, $options = [])
  98.  * @method Tag\Areablock areablock($name, $options = [])
  99.  * @method Tag\Block block($name, $options = [])
  100.  * @method Tag\Checkbox checkbox($name, $options = [])
  101.  * @method Tag\Date date($name, $options = [])
  102.  * @method Tag\Embed embed($name, $options = [])
  103.  * @method Tag\Relation relation($name, $options = [])
  104.  * @method Tag\Image image($name, $options = [])
  105.  * @method Tag\Input input($name, $options = [])
  106.  * @method Tag\Link link($name, $options = [])
  107.  * @method Tag\Relations relations($name, $options = [])
  108.  * @method Tag\Multiselect multiselect($name, $options = [])
  109.  * @method Tag\Numeric numeric($name, $options = [])
  110.  * @method Tag\Pdf pdf($name, $options = [])
  111.  * @method Tag\Renderlet renderlet($name, $options = [])
  112.  * @method Tag\Select select($name, $options = [])
  113.  * @method Tag\Snippet snippet($name, $options = [])
  114.  * @method Tag\Table table($name, $options = [])
  115.  * @method Tag\Textarea textarea($name, $options = [])
  116.  * @method Tag\Video video($name, $options = [])
  117.  * @method Tag\Wysiwyg wysiwyg($name, $options = [])
  118.  * @method Tag\Scheduledblock scheduledblock($name, $options = [])
  119.  *
  120.  * @property Document $document
  121.  * @property bool $editmode
  122.  * @property GlobalVariables $app
  123.  */
  124. class PhpEngine extends BasePhpEngine
  125. {
  126.     const PARAM_NO_PARENT '_no_parent';
  127.     /**
  128.      * @var HelperBrokerInterface[]
  129.      */
  130.     protected $helperBrokers = [];
  131.     /**
  132.      * @var ViewModelInterface[]
  133.      */
  134.     protected $viewModels = [];
  135.     /**
  136.      * @param HelperBrokerInterface $helperBroker
  137.      */
  138.     public function addHelperBroker(HelperBrokerInterface $helperBroker)
  139.     {
  140.         $this->helperBrokers[] = $helperBroker;
  141.     }
  142.     /**
  143.      * In addition to the core method, this keeps parameters in a ViewModel instance which is accessible from
  144.      * view helpers and via $this->$variable.
  145.      *
  146.      * {@inheritdoc}
  147.      */
  148.     protected function evaluate(Storage $template, array $parameters = [])
  149.     {
  150.         // disable parent with "magic" _no_parent parameter
  151.         $disableParent false;
  152.         if (isset($parameters[static::PARAM_NO_PARENT])) {
  153.             $disableParent = (bool)($parameters[static::PARAM_NO_PARENT]);
  154.             unset($parameters[static::PARAM_NO_PARENT]);
  155.         }
  156.         // create view model and push it onto the model stack
  157.         $this->viewModels[] = new ViewModel($parameters);
  158.         // render the template
  159.         $result parent::evaluate($template$parameters);
  160.         // remove current view model from stack and destroy it
  161.         $viewModel array_pop($this->viewModels);
  162.         unset($viewModel);
  163.         if ($disableParent) {
  164.             $this->parents[$this->current] = null;
  165.         }
  166.         return $result;
  167.     }
  168.     /**
  169.      * Renders template with current parameters
  170.      *
  171.      * @param $name
  172.      * @param array $parameters
  173.      *
  174.      * @return string
  175.      */
  176.     public function template($name, array $parameters = [])
  177.     {
  178.         if ($viewModel $this->getViewModel()) {
  179.             // attach current variables
  180.             $parameters array_replace($viewModel->getParameters()->all(), $parameters);
  181.         }
  182.         return $this->render($name$parameters);
  183.     }
  184.     /**
  185.      * Get the current view model
  186.      *
  187.      * @return ViewModelInterface
  188.      */
  189.     public function getViewModel()
  190.     {
  191.         $count count($this->viewModels);
  192.         if ($count 0) {
  193.             return $this->viewModels[$count 1];
  194.         }
  195.     }
  196.     /**
  197.      * Get a view model parameter
  198.      *
  199.      * @param $name
  200.      * @param null $default
  201.      *
  202.      * @return mixed|null
  203.      */
  204.     public function getViewParameter($name$default null)
  205.     {
  206.         $viewModel $this->getViewModel();
  207.         if (null !== $viewModel) {
  208.             return $viewModel->getParameters()->get($name$default);
  209.         }
  210.         return $default;
  211.     }
  212.     /**
  213.      * Magic getter reads variable from ViewModel
  214.      *
  215.      * @inheritDoc
  216.      */
  217.     public function __get($name)
  218.     {
  219.         return $this->getViewParameter($name);
  220.     }
  221.     /**
  222.      * Magic isset checks variable from ViewModel
  223.      *
  224.      * @inheritDoc
  225.      */
  226.     public function __isset($name)
  227.     {
  228.         return $this->getViewParameter($name) !== null;
  229.     }
  230.     /**
  231.      * @param string $name
  232.      * @param mixed $value
  233.      */
  234.     public function __set($name$value)
  235.     {
  236.         $viewModel $this->getViewModel();
  237.         if ($viewModel) {
  238.             $viewModel->getParameters()->set($name$value);
  239.         } else {
  240.             throw new \RuntimeException(sprintf('Can\'t set variable %s as there is no active view model'$name));
  241.         }
  242.     }
  243.     /**
  244.      * @inheritDoc
  245.      */
  246.     public function __call($method$arguments)
  247.     {
  248.         // try to run helper from helper broker (native helper, document tag, zend view, ...)
  249.         foreach ($this->helperBrokers as $helperBroker) {
  250.             if ($helperBroker->supports($this$method)) {
  251.                 return $helperBroker->helper($this$method$arguments);
  252.             }
  253.         }
  254.         throw new \InvalidArgumentException('Call to undefined method ' $method);
  255.     }
  256. }