src/Twig/ShortcodesExtension.php line 63
- <?php
- namespace App\Twig;
- use App\Utils\Util;
- use App\Controller\ShortcodesController;
- use Twig_Environment as Environment;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Twig\Extension\AbstractExtension;
- use Twig\TwigFilter;
- use Twig\TwigFunction;
- class ShortcodesExtension extends AbstractExtension
- {
- // private $twig;
- private $params;
- private $cc;
- // private $environment = null;
- //
- // public function initRuntime(Environment $environment)
- // {
- // $this->environment = $environment;
- // }
- public function __construct(ParameterBagInterface $params, ShortcodesController $contenidoController)
- {
- // $this->twig = $twig;
- $this->params = $params;
- $this->cc = $contenidoController;
- }
- public function getName()
- {
- return 'shortcode_extension';
- }
- public function getFilters()
- {
- return array(
- new TwigFilter('html', array($this, 'htmlFilter')),
- new TwigFilter('fileSize', array($this, 'fileSizeFilter')),
- );
- }
- public function getFunctions()
- {
- return array(
- new TwigFunction('doShortcode', [$this, 'doShortcode']),
- // new TwigFunction('callstatic', [$this, 'callstatic'])
- );
- }
- public function htmlFilter($html,$listashort = null)
- {
- $encontrado=false;
- // si no se especifica buscar todos los shortcodes en el contenido
- if (!$listashort)
- $listashort = $this->params->get('app.shortcodes');
- // Buscar shorcodes
- $shortcodes = Util::shortcodes($listashort,$html);
- // Procesar shortcodes
- foreach ($listashort as $ls)
- {
- if (isset($shortcodes[$ls]))
- {
- $method = $ls."SC";
- foreach($shortcodes[$ls] as $sc => $opciones)
- {
- $respuesta = $this->cc->$method($opciones);
- if ($respuesta)
- $encontrado = true;
- $html = str_replace($sc,$respuesta,html_entity_decode($html));
- }
- }
- }
- return $html;
- }
- public function doShortcode($shortcode,$opciones)
- {
- $method = $shortcode."SC";
- return $this->cc->$method($opciones);
- }
- public function fileSizeFilter($a_bytes)
- {
- if ($a_bytes < 1024) {
- return $a_bytes .' B';
- } elseif ($a_bytes < 1048576) {
- return round($a_bytes / 1024, 2) .' KiB';
- } elseif ($a_bytes < 1073741824) {
- return round($a_bytes / 1048576, 2) . ' MiB';
- } elseif ($a_bytes < 1099511627776) {
- return round($a_bytes / 1073741824, 2) . ' GiB';
- } elseif ($a_bytes < 1125899906842624) {
- return round($a_bytes / 1099511627776, 2) .' TiB';
- } elseif ($a_bytes < 1152921504606846976) {
- return round($a_bytes / 1125899906842624, 2) .' PiB';
- } elseif ($a_bytes < 1180591620717411303424) {
- return round($a_bytes / 1152921504606846976, 2) .' EiB';
- } elseif ($a_bytes < 1208925819614629174706176) {
- return round($a_bytes / 1180591620717411303424, 2) .' ZiB';
- } else {
- return round($a_bytes / 1208925819614629174706176, 2) .' YiB';
- }
- }
- }