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
{
31 protected array $filters;
36 protected array $output;
41 protected string $token;
44 * @link https://twig.symfony.com/doc/2.x/advanced.html
48 public function __construct(protected ContainerInterface
$container, protected IntlUtil
$intl, protected FileLocator
$locator, protected PackageInterface
$package, protected SluggerUtil
$slugger) {
49 //Retrieve bundle config
50 if ($parameters = $container->getParameter(RapsysPackBundle
::getAlias())) {
51 //Set filters, output arrays and token string
52 foreach(['filters', 'output', 'token'] as $k) {
53 $this->$k = $parameters[$k];
59 * Returns a filter array to add to the existing list.
61 * @return \Twig\TwigFilter[]
63 public function getTokenParsers(): array {
65 new TokenParser($this->locator
, $this->package
, $this->token
, 'stylesheet', $this->output
['css'], $this->filters
['css']),
66 new TokenParser($this->locator
, $this->package
, $this->token
, 'javascript', $this->output
['js'], $this->filters
['js']),
67 new TokenParser($this->locator
, $this->package
, $this->token
, 'image', $this->output
['img'], $this->filters
['img'])
72 * Returns a filter array to add to the existing list.
74 * @return \Twig\TwigFilter[]
76 public function getFilters(): array {
78 new \Twig\
TwigFilter('lcfirst', 'lcfirst'),
79 new \Twig\
TwigFilter('ucfirst', 'ucfirst'),
80 new \Twig\
TwigFilter('hash', [$this->slugger
, 'hash']),
81 new \Twig\
TwigFilter('unshort', [$this->slugger
, 'unshort']),
82 new \Twig\
TwigFilter('short', [$this->slugger
, 'short']),
83 new \Twig\
TwigFilter('slug', [$this->slugger
, 'slug']),
84 new \Twig\
TwigFilter('intldate', [$this->intl
, 'date'], ['needs_environment' => true]),
85 new \Twig\
TwigFilter('intlnumber', [$this->intl
, 'number']),
86 new \Twig\
TwigFilter('intlcurrency', [$this->intl
, 'currency']),
87 new \Twig\
TwigFilter('download', 'file_get_contents', [false, null]),
88 new \Twig\
TwigFilter('base64_encode', 'base64_encode'),
89 new \Twig\
TwigFilter('base64_decode', 'base64_decode')