vendor/thelia/core/lib/Thelia/Core/Hook/BaseHook.php line 90

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Core\Hook;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\Translation\TranslatorInterface;
  14. use Thelia\Core\Event\Hook\HookRenderEvent;
  15. use Thelia\Core\HttpFoundation\Request;
  16. use Thelia\Core\HttpFoundation\Session\Session;
  17. use Thelia\Core\Template\Assets\AssetResolverInterface;
  18. use Thelia\Core\Template\ParserInterface;
  19. use Thelia\Model\Cart;
  20. use Thelia\Model\Currency;
  21. use Thelia\Model\Customer;
  22. use Thelia\Model\Lang;
  23. use Thelia\Model\Order;
  24. use Thelia\Module\BaseModule;
  25. /**
  26.  * The base class for hook. If you provide hooks in your module you have to extends
  27.  * this class.
  28.  *
  29.  * These class provides some helper functions to retrieve object from the current session
  30.  * of the current user. It also provides a render function that allows you to get the right
  31.  * template file from different locations and allows you to override templates in your current
  32.  * template.
  33.  *
  34.  * Class BaseHook
  35.  *
  36.  * @author  Julien Chanséaume <jchanseaume@openstudio.fr>
  37.  */
  38. abstract class BaseHook
  39. {
  40.     public const INJECT_TEMPLATE_METHOD_NAME 'insertTemplate';
  41.     /** @var BaseModule */
  42.     public $module;
  43.     /** @var array list of templates automatically injected */
  44.     protected $templates = [];
  45.     /** @var \Thelia\Core\Template\ParserInterface */
  46.     public $parser;
  47.     /** @var TranslatorInterface */
  48.     public $translator;
  49.     /** @var AssetResolverInterface */
  50.     public $assetsResolver;
  51.     /** @var EventDispatcherInterface */
  52.     public $dispatcher;
  53.     /** @var Request */
  54.     protected $request;
  55.     /** @var Session */
  56.     protected $session;
  57.     /** @var Customer */
  58.     protected $customer;
  59.     /** @var Cart */
  60.     protected $cart;
  61.     /** @var Order */
  62.     protected $order;
  63.     /** @var Lang */
  64.     protected $lang;
  65.     /** @var Currency */
  66.     protected $currency;
  67.     /**
  68.      * This function is called when hook uses the automatic insert template.
  69.      *
  70.      * @param string $code
  71.      */
  72.     public function insertTemplate(HookRenderEvent $event$code): void
  73.     {
  74.         if (\array_key_exists($code$this->templates)) {
  75.             $templates explode(';'$this->templates[$code]);
  76.             // Concatenate arguments and template variables,
  77.             // giving the precedence to arguments.
  78.             $allArguments $event->getTemplateVars() + $event->getArguments();
  79.             foreach ($templates as $template) {
  80.                 [$type$filepath] = $this->getTemplateParams($template);
  81.                 if ('render' === $type) {
  82.                     $event->add($this->render($filepath$allArguments));
  83.                     continue;
  84.                 }
  85.                 if ('dump' === $type) {
  86.                     $event->add($this->render($filepath));
  87.                     continue;
  88.                 }
  89.                 if ('css' === $type) {
  90.                     $event->add($this->addCSS($filepath));
  91.                     continue;
  92.                 }
  93.                 if ('js' === $type) {
  94.                     $event->add($this->addJS($filepath));
  95.                     continue;
  96.                 }
  97.                 if (method_exists($this$type)) {
  98.                     $this->{$type}($filepath$allArguments);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.     /**
  104.      * helper function allowing you to render a template using a template engine.
  105.      *
  106.      * @param string $templateName the template path of the template
  107.      * @param array  $parameters   an array of parameters to assign to a template engine
  108.      *
  109.      * @return string the content generated by a template engine
  110.      */
  111.     public function render($templateName, array $parameters = [])
  112.     {
  113.         $templateDir $this->assetsResolver->resolveAssetSourcePath($this->module->getCode(), false$templateName$this->parser);
  114.         if (null !== $templateDir) {
  115.             // retrieve the template
  116.             $content $this->parser->render($templateDir.DS.$templateName$parameters);
  117.         } else {
  118.             $content sprintf('ERR: Unknown template %s for module %s'$templateName$this->module->getCode());
  119.         }
  120.         return $content;
  121.     }
  122.     /**
  123.      * helper function allowing you to get the content of a file.
  124.      *
  125.      * @param string $fileName the template path of the template
  126.      *
  127.      * @return string the content of the file
  128.      */
  129.     public function dump($fileName)
  130.     {
  131.         $fileDir $this->assetsResolver->resolveAssetSourcePath($this->module->getCode(), false$fileName$this->parser);
  132.         if (null !== $fileDir) {
  133.             $content file_get_contents($fileDir.DS.$fileName);
  134.             if (false === $content) {
  135.                 $content '';
  136.             }
  137.         } else {
  138.             $content sprintf('ERR: Unknown file %s for module %s'$fileName$this->module->getCode());
  139.         }
  140.         return $content;
  141.     }
  142.     /**
  143.      * helper function allowing you to generate the HTML link tag.
  144.      *
  145.      * @param string $fileName   the path to the css file
  146.      * @param array  $attributes the attributes of the tag
  147.      * @param array  $filters    an array of assets processing filters (less, sass, etc.)
  148.      *
  149.      * @return string the link tag
  150.      */
  151.     public function addCSS($fileName$attributes = [], $filters = [])
  152.     {
  153.         $tag '';
  154.         $url $this->assetsResolver->resolveAssetURL($this->module->getCode(), $fileName'css'$this->parser$filters);
  155.         if ('' !== $url) {
  156.             $tags = [];
  157.             $tags[] = '<link rel="stylesheet" type="text/css" ';
  158.             $tags[] = ' href="'.$url.'" ';
  159.             foreach ($attributes as $name => $val) {
  160.                 if (\is_string($name) && !\in_array($name, ['href''rel''type'])) {
  161.                     $tags[] = $name.'="'.$val.'" ';
  162.                 }
  163.             }
  164.             $tags[] = '/>';
  165.             $tag implode(''$tags);
  166.         }
  167.         return $tag;
  168.     }
  169.     /**
  170.      * helper function allowing you to generate the HTML script tag.
  171.      *
  172.      * @param string $fileName   the path to the js file
  173.      * @param array  $attributes the attributes of the tag
  174.      * @param array  $filters    an array of assets processing filters (cofeescript, compress, etc.)
  175.      *
  176.      * @return string the script tag
  177.      */
  178.     public function addJS($fileName$attributes = [], $filters = [])
  179.     {
  180.         $tag '';
  181.         $url $this->assetsResolver->resolveAssetURL($this->module->getCode(), $fileName'js'$this->parser$filters);
  182.         if ('' !== $url) {
  183.             $tags = [];
  184.             $tags[] = '<script type="text/javascript" ';
  185.             $tags[] = ' src="'.$url.'" ';
  186.             foreach ($attributes as $name => $val) {
  187.                 if (\is_string($name) && !\in_array($name, ['src''type'])) {
  188.                     $tags[] = $name.'="'.$val.'" ';
  189.                 }
  190.             }
  191.             $tags[] = '></script>';
  192.             $tag implode(''$tags);
  193.         }
  194.         return $tag;
  195.     }
  196.     /**
  197.      * @param \Thelia\Module\BaseModule $module
  198.      */
  199.     public function setModule($module): void
  200.     {
  201.         $this->module $module;
  202.     }
  203.     /**
  204.      * @return \Thelia\Module\BaseModule
  205.      */
  206.     public function getModule()
  207.     {
  208.         return $this->module;
  209.     }
  210.     public function setParser(ParserInterface $parser): void
  211.     {
  212.         $this->parser $parser;
  213.     }
  214.     /**
  215.      * @return \Thelia\Core\Template\ParserInterface
  216.      */
  217.     public function getParser()
  218.     {
  219.         return $this->parser;
  220.     }
  221.     /**
  222.      * Translates the given message.
  223.      *
  224.      * @param string $id         The message id (may also be an object that can be cast to string)
  225.      * @param array  $parameters An array of parameters for the message
  226.      * @param string $domain     The domain for the message
  227.      * @param string $locale     The locale
  228.      *
  229.      * @return string The translated string
  230.      *
  231.      * @api
  232.      */
  233.     protected function trans($id, array $parameters = [], $domain null$locale null)
  234.     {
  235.         return $this->translator->trans($id$parameters$domain$locale);
  236.     }
  237.     /**
  238.      * get the request.
  239.      *
  240.      * @return Request
  241.      */
  242.     protected function getRequest()
  243.     {
  244.         if (null === $this->request) {
  245.             $this->request $this->getParser()->getRequest();
  246.         }
  247.         return $this->request;
  248.     }
  249.     /**
  250.      * get the session.
  251.      *
  252.      * @return Session
  253.      */
  254.     protected function getSession()
  255.     {
  256.         if (null === $this->session) {
  257.             if (null !== $this->getRequest()) {
  258.                 $this->session $this->request->getSession();
  259.             }
  260.         }
  261.         return $this->session;
  262.     }
  263.     /**
  264.      * Get the view argument for the request.
  265.      *
  266.      * It allows you to identify the page currently displayed. eg: index, category, ...
  267.      *
  268.      * @return string the current view
  269.      */
  270.     protected function getView()
  271.     {
  272.         $ret '';
  273.         if (null !== $this->getRequest()) {
  274.             $ret $this->getRequest()->attributes->get('_view''');
  275.         }
  276.         return $ret;
  277.     }
  278.     /**
  279.      * Get the cart from the session.
  280.      *
  281.      * @return \Thelia\Model\Cart|null
  282.      */
  283.     protected function getCart()
  284.     {
  285.         if (null === $this->cart) {
  286.             $this->cart $this->getSession() ? $this->getSession()->getSessionCart($this->dispatcher) : null;
  287.         }
  288.         return $this->cart;
  289.     }
  290.     /**
  291.      * Get the order from the session.
  292.      *
  293.      * @return \Thelia\Model\Order|null
  294.      */
  295.     protected function getOrder()
  296.     {
  297.         if (null === $this->order) {
  298.             $this->order $this->getSession() ? $this->getSession()->getOrder() : null;
  299.         }
  300.         return $this->order;
  301.     }
  302.     /**
  303.      * Get the current currency used or if not present the default currency for the shop.
  304.      *
  305.      * @return \Thelia\Model\Currency
  306.      */
  307.     protected function getCurrency()
  308.     {
  309.         if (null === $this->currency) {
  310.             $this->currency $this->getSession() ? $this->getSession()->getCurrency(true) : Currency::getDefaultCurrency();
  311.         }
  312.         return $this->currency;
  313.     }
  314.     /**
  315.      * Get the current customer logged in. If no customer is logged return null.
  316.      *
  317.      * @return \Thelia\Model\Customer|null
  318.      */
  319.     protected function getCustomer()
  320.     {
  321.         if (null === $this->customer) {
  322.             $this->customer $this->getSession() ? $this->getSession()->getCustomerUser() : null;
  323.         }
  324.         return $this->customer;
  325.     }
  326.     /**
  327.      * Get the current lang used or if not present the default lang for the shop.
  328.      *
  329.      * @return \Thelia\Model\Lang
  330.      */
  331.     protected function getLang()
  332.     {
  333.         if (null === $this->lang) {
  334.             $this->lang $this->getSession() ? $this->getSession()->getLang(true) : $this->lang Lang::getDefaultLanguage();
  335.         }
  336.         return $this->lang;
  337.     }
  338.     /**
  339.      * Add a new template for automatic render.
  340.      *
  341.      * @param string $hookCode the code of the hook (the name of the event used to render) : 'hook.{type}.{hook code}'
  342.      * @param string $value    list of the template to render or add.
  343.      *                         eg: 'render:mytemplate.html;css:assets/css/mycss.css;js:assets/js/myjs.js'
  344.      */
  345.     public function addTemplate($hookCode$value): void
  346.     {
  347.         if (\array_key_exists($hookCode$this->templates)) {
  348.             throw new \InvalidArgumentException(sprintf("The hook '%s' is already used in this class."$hookCode));
  349.         }
  350.         $this->templates[$hookCode] = $value;
  351.     }
  352.     /**
  353.      * @return array templates
  354.      */
  355.     public function getTemplates()
  356.     {
  357.         return $this->templates;
  358.     }
  359.     /**
  360.      * @param $template
  361.      *
  362.      * @return array
  363.      */
  364.     protected function getTemplateParams($template)
  365.     {
  366.         $templateParams explode(':'$template);
  367.         if (\count($templateParams) > 1) {
  368.             $type $templateParams[0];
  369.             $filepath $templateParams[1];
  370.         } else {
  371.             $type 'render';
  372.             $filepath $templateParams[0];
  373.         }
  374.         return [$type$filepath];
  375.     }
  376. }