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

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\Templating;
  15. use Pimcore\Config\Config;
  16. use Pimcore\Model\Document;
  17. use Pimcore\Model\Document\Editable;
  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 array breachAttackRandomContent()
  81.  * @method mixed getParam($key, $default = null)
  82.  * @method Glossary glossary()
  83.  * @method Container placeholder($placeholderName)
  84.  * @method HeadLink headLink(array $attributes = null, $placement = Container::APPEND)
  85.  * @method HeadMeta headMeta($content = null, $keyValue = null, $keyType = 'name', $modifiers = array(), $placement = Container::APPEND)
  86.  * @method HeadScript headScript($mode = HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
  87.  * @method HeadStyle headStyle($content = null, $placement = 'APPEND', $attributes = array())
  88.  * @method HeadTitle headTitle($title = null, $setType = null)
  89.  * @method string inc($include, array $params = [], $cacheEnabled = true, $editmode = null)
  90.  * @method InlineScript inlineScript($mode = HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript')
  91.  * @method WebLink webLink()
  92.  * @method Navigation navigation()
  93.  * @method Config|mixed websiteConfig($key = null, $default = null)
  94.  * @method string pimcoreUrl(array $urlOptions = [], $name = null, $reset = false, $encode = true, $relative = false)
  95.  * @method string translate($key, $parameters = [], $domain = null, $locale = null)
  96.  *
  97.  * Pimcore editables
  98.  * @method Editable\Area area($name, $options = [])
  99.  * @method Editable\Areablock areablock($name, $options = [])
  100.  * @method Editable\Block block($name, $options = [])
  101.  * @method Editable\Checkbox checkbox($name, $options = [])
  102.  * @method Editable\Date date($name, $options = [])
  103.  * @method Editable\Embed embed($name, $options = [])
  104.  * @method Editable\Relation relation($name, $options = [])
  105.  * @method Editable\Image image($name, $options = [])
  106.  * @method Editable\Input input($name, $options = [])
  107.  * @method Editable\Link link($name, $options = [])
  108.  * @method Editable\Relations relations($name, $options = [])
  109.  * @method Editable\Multiselect multiselect($name, $options = [])
  110.  * @method Editable\Numeric numeric($name, $options = [])
  111.  * @method Editable\Pdf pdf($name, $options = [])
  112.  * @method Editable\Renderlet renderlet($name, $options = [])
  113.  * @method Editable\Select select($name, $options = [])
  114.  * @method Editable\Snippet snippet($name, $options = [])
  115.  * @method Editable\Table table($name, $options = [])
  116.  * @method Editable\Textarea textarea($name, $options = [])
  117.  * @method Editable\Video video($name, $options = [])
  118.  * @method Editable\Wysiwyg wysiwyg($name, $options = [])
  119.  * @method Editable\Scheduledblock scheduledblock($name, $options = [])
  120.  *
  121.  * @property Document $document
  122.  * @property bool $editmode
  123.  * @property GlobalVariables $app
  124.  *
  125.  * @deprecated since 6.8.0 and will be removed in Pimcore 10.
  126.  */
  127. class PhpEngine extends BasePhpEngine
  128. {
  129.     const PARAM_NO_PARENT '_no_parent';
  130.     /**
  131.      * @var HelperBrokerInterface[]
  132.      */
  133.     protected $helperBrokers = [];
  134.     /**
  135.      * @var ViewModelInterface[]
  136.      */
  137.     protected $viewModels = [];
  138.     /**
  139.      * @param HelperBrokerInterface $helperBroker
  140.      */
  141.     public function addHelperBroker(HelperBrokerInterface $helperBroker)
  142.     {
  143.         $this->helperBrokers[] = $helperBroker;
  144.     }
  145.     /**
  146.      * In addition to the core method, this keeps parameters in a ViewModel instance which is accessible from
  147.      * view helpers and via $this->$variable.
  148.      *
  149.      * {@inheritdoc}
  150.      */
  151.     protected function evaluate(Storage $template, array $parameters = [])
  152.     {
  153.         // disable parent with "magic" _no_parent parameter
  154.         $disableParent false;
  155.         if (isset($parameters[static::PARAM_NO_PARENT])) {
  156.             $disableParent = (bool)($parameters[static::PARAM_NO_PARENT]);
  157.             unset($parameters[static::PARAM_NO_PARENT]);
  158.         }
  159.         // create view model and push it onto the model stack
  160.         $this->viewModels[] = new ViewModel($parameters);
  161.         // render the template
  162.         $result parent::evaluate($template$parameters);
  163.         // remove current view model from stack and destroy it
  164.         $viewModel array_pop($this->viewModels);
  165.         unset($viewModel);
  166.         if ($disableParent) {
  167.             $this->parents[$this->current] = null;
  168.         }
  169.         return $result;
  170.     }
  171.     /**
  172.      * Renders template with current parameters
  173.      *
  174.      * @param string $name
  175.      * @param array $parameters
  176.      *
  177.      * @return string
  178.      */
  179.     public function template($name, array $parameters = [])
  180.     {
  181.         if ($viewModel $this->getViewModel()) {
  182.             // attach current variables
  183.             $parameters array_replace($viewModel->getParameters()->all(), $parameters);
  184.         }
  185.         return $this->render($name$parameters);
  186.     }
  187.     /**
  188.      * Get the current view model
  189.      *
  190.      * @return ViewModelInterface|null
  191.      *
  192.      * @deprecated
  193.      */
  194.     public function getViewModel()
  195.     {
  196.         $count count($this->viewModels);
  197.         if ($count 0) {
  198.             return $this->viewModels[$count 1];
  199.         }
  200.         return null;
  201.     }
  202.     /**
  203.      * Get a view model parameter
  204.      *
  205.      * @param string $name
  206.      * @param mixed|null $default
  207.      *
  208.      * @return mixed|null
  209.      *
  210.      * @deprecated
  211.      */
  212.     public function getViewParameter($name$default null)
  213.     {
  214.         $viewModel $this->getViewModel();
  215.         if (null !== $viewModel) {
  216.             return $viewModel->getParameters()->get($name$default);
  217.         }
  218.         return $default;
  219.     }
  220.     /**
  221.      * Magic getter reads variable from ViewModel
  222.      *
  223.      * @inheritDoc
  224.      */
  225.     public function __get($name)
  226.     {
  227.         return $this->getViewParameter($name);
  228.     }
  229.     /**
  230.      * Magic isset checks variable from ViewModel
  231.      *
  232.      * @inheritDoc
  233.      */
  234.     public function __isset($name)
  235.     {
  236.         return $this->getViewParameter($name) !== null;
  237.     }
  238.     /**
  239.      * @param string $name
  240.      * @param mixed $value
  241.      */
  242.     public function __set($name$value)
  243.     {
  244.         $viewModel $this->getViewModel();
  245.         if ($viewModel) {
  246.             $viewModel->getParameters()->set($name$value);
  247.         } else {
  248.             throw new \RuntimeException(sprintf('Can\'t set variable %s as there is no active view model'$name));
  249.         }
  250.     }
  251.     /**
  252.      * @inheritDoc
  253.      */
  254.     public function __call($method$arguments)
  255.     {
  256.         // try to run helper from helper broker (native helper, document tag, zend view, ...)
  257.         foreach ($this->helperBrokers as $helperBroker) {
  258.             if ($helperBroker->supports($this$method)) {
  259.                 return $helperBroker->helper($this$method$arguments);
  260.             }
  261.         }
  262.         throw new \InvalidArgumentException('Call to undefined method ' $method);
  263.     }
  264. }