1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle\Extension
;
14 use Symfony\Component\Asset\PackageInterface
;
15 use Symfony\Component\DependencyInjection\ContainerInterface
;
16 use Symfony\Component\HttpKernel\Config\FileLocator
;
17 use Twig\Extension\AbstractExtension
;
19 use Rapsys\PackBundle\Parser\TokenParser
;
20 use Rapsys\PackBundle\RapsysPackBundle
;
21 use Rapsys\PackBundle\Util\IntlUtil
;
22 use Rapsys\PackBundle\Util\SluggerUtil
;
27 class PackExtension
extends AbstractExtension
{
50 * @link https://twig.symfony.com/doc/2.x/advanced.html
54 public function __construct(ContainerInterface
$container, IntlUtil
$intl, FileLocator
$locator, PackageInterface
$package, SluggerUtil
$slugger) {
59 $this->locator
= $locator;
62 $this->package
= $package;
65 $this->slugger
= $slugger;
67 //Retrieve bundle config
68 if ($parameters = $container->getParameter(self
::getAlias())) {
69 //Set config, output and filters arrays
70 foreach(['config', 'output', 'filters'] as $k) {
71 $this->$k = $parameters[$k];
77 * Returns a list of filters to add to the existing list.
79 * @return \Twig\TwigFilter[]
81 public function getTokenParsers(): array {
83 new TokenParser($this->locator
, $this->package
, $this->config
, 'stylesheet', $this->output
['css'], $this->filters
['css']),
84 new TokenParser($this->locator
, $this->package
, $this->config
, 'javascript', $this->output
['js'], $this->filters
['js']),
85 new TokenParser($this->locator
, $this->package
, $this->config
, 'image', $this->output
['img'], $this->filters
['img'])
90 * Returns a list of filters to add to the existing list.
92 * @return \Twig\TwigFilter[]
94 public function getFilters(): array {
96 new \Twig\
TwigFilter('lcfirst', 'lcfirst'),
97 new \Twig\
TwigFilter('ucfirst', 'ucfirst'),
98 new \Twig\
TwigFilter('hash', [$this->slugger
, 'hash']),
99 new \Twig\
TwigFilter('unshort', [$this->slugger
, 'unshort']),
100 new \Twig\
TwigFilter('short', [$this->slugger
, 'short']),
101 new \Twig\
TwigFilter('slug', [$this->slugger
, 'slug']),
102 new \Twig\
TwigFilter('intldate', [$this->intl
, 'date'], ['needs_environment' => true]),
103 new \Twig\
TwigFilter('intlnumber', [$this->intl
, 'number']),
104 new \Twig\
TwigFilter('intlcurrency', [$this->intl
, 'currency']),
105 new \Twig\
TwigFilter('download', 'file_get_contents', [false, null]),
106 new \Twig\
TwigFilter('base64_encode', 'base64_encode'),
107 new \Twig\
TwigFilter('base64_decode', 'base64_decode')
114 public function getAlias(): string {
115 return RapsysPackBundle
::getAlias();