src/Twig/ShortcodesExtension.php line 54

  1. <?php
  2. namespace App\Twig;
  3. use App\Utils\Util;
  4. use App\Controller\ShortcodesController;
  5. use Twig_Environment as Environment;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFilter;
  9. use Twig\TwigFunction;
  10. class ShortcodesExtension extends AbstractExtension
  11. {
  12. //     private $twig;
  13.     private $params
  14.     private $cc;
  15. //     private $environment = null;
  16. // 
  17. //     public function initRuntime(Environment $environment)
  18. //     {
  19. //         $this->environment = $environment;
  20. //     }    
  21.     public function __construct(ParameterBagInterface $paramsShortcodesController $contenidoController)
  22.     {
  23. //         $this->twig    = $twig;
  24.         $this->params $params
  25.         $this->cc $contenidoController;
  26.     }     
  27.     
  28.     public function getName()
  29.     {
  30.         return 'shortcode_extension';
  31.     }
  32.         
  33.     
  34.     public function getFilters()
  35.     {
  36.         return array(
  37.             new TwigFilter('html', array($this'htmlFilter')),
  38.             new TwigFilter('fileSize', array($this'fileSizeFilter')),
  39.         );
  40.     }
  41.     
  42.     public function getFunctions()
  43.     {
  44.         return array(
  45.             new TwigFunction('doShortcode', [$this'doShortcode']),
  46.              // new TwigFunction('callstatic', [$this, 'callstatic'])
  47.         );
  48.     }    
  49.     public function htmlFilter($html,$listashort null)
  50.     {
  51.         $encontrado=false;
  52.         
  53.         // si no se especifica buscar todos los shortcodes en el contenido
  54.         if (!$listashort)
  55.             $listashort =  $this->params->get('app.shortcodes');
  56.         // Buscar shorcodes
  57.         $shortcodes Util::shortcodes($listashort,$html);
  58.         // Procesar shortcodes
  59.         foreach ($listashort as $ls)
  60.         {
  61.             if (isset($shortcodes[$ls]))
  62.             {
  63.                 $method $ls."SC";
  64.                 
  65.                 foreach($shortcodes[$ls] as $sc => $opciones)
  66.                 {
  67.                     $respuesta $this->cc->$method($opciones);
  68.                     if ($respuesta)
  69.                         $encontrado true;
  70.                     $html str_replace($sc,$respuesta,html_entity_decode($html));
  71.                 }
  72.             }
  73.         }
  74.         
  75.         return $html;
  76.         
  77.     }
  78.     
  79.     public function doShortcode($shortcode,$opciones)
  80.     {
  81.         $method $shortcode."SC";
  82.         
  83.         return $this->cc->$method($opciones);
  84.     }
  85.     
  86.     public function fileSizeFilter($a_bytes)
  87.     {
  88.         if ($a_bytes 1024) {
  89.             return $a_bytes .' B';
  90.         } elseif ($a_bytes 1048576) {
  91.             return round($a_bytes 10242) .' KiB';
  92.         } elseif ($a_bytes 1073741824) {
  93.             return round($a_bytes 10485762) . ' MiB';
  94.         } elseif ($a_bytes 1099511627776) {
  95.             return round($a_bytes 10737418242) . ' GiB';
  96.         } elseif ($a_bytes 1125899906842624) {
  97.             return round($a_bytes 10995116277762) .' TiB';
  98.         } elseif ($a_bytes 1152921504606846976) {
  99.             return round($a_bytes 11258999068426242) .' PiB';
  100.         } elseif ($a_bytes 1180591620717411303424) {
  101.             return round($a_bytes 11529215046068469762) .' EiB';
  102.         } elseif ($a_bytes 1208925819614629174706176) {
  103.             return round($a_bytes 11805916207174113034242) .' ZiB';
  104.         } else {
  105.             return round($a_bytes 12089258196146291747061762) .' YiB';
  106.         }
  107.     }    
  108.       
  109. }