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\SluggerUtil
;
26 class PackExtension
extends AbstractExtension
{
49 * @link https://twig.symfony.com/doc/2.x/advanced.html
53 public function __construct(ContainerInterface
$container, IntlUtil
$intl, FileLocator
$locator, PackageInterface
$package, SluggerUtil
$slugger) {
58 $this->locator
= $locator;
61 $this->package
= $package;
64 $this->slugger
= $slugger;
66 //Retrieve bundle config
67 if ($parameters = $container->getParameter(self
::getAlias())) {
68 //Set config, output and filters arrays
69 foreach(['config', 'output', 'filters'] as $k) {
70 $this->$k = $parameters[$k];
76 * Returns a list of filters to add to the existing list.
78 * @return \Twig\TwigFilter[]
80 public function getTokenParsers(): array {
82 new TokenParser($this->locator
, $this->package
, $this->config
, 'stylesheet', $this->output
['css'], $this->filters
['css']),
83 new TokenParser($this->locator
, $this->package
, $this->config
, 'javascript', $this->output
['js'], $this->filters
['js']),
84 new TokenParser($this->locator
, $this->package
, $this->config
, 'image', $this->output
['img'], $this->filters
['img'])
89 * Returns a list of filters to add to the existing list.
91 * @return \Twig\TwigFilter[]
93 public function getFilters(): array {
95 new \Twig\
TwigFilter('lcfirst', 'lcfirst'),
96 new \Twig\
TwigFilter('ucfirst', 'ucfirst'),
97 new \Twig\
TwigFilter('hash', [$this->slugger
, 'hash']),
98 new \Twig\
TwigFilter('unshort', [$this->slugger
, 'unshort']),
99 new \Twig\
TwigFilter('short', [$this->slugger
, 'short']),
100 new \Twig\
TwigFilter('slug', [$this->slugger
, 'slug']),
101 new \Twig\
TwigFilter('intldate', [$this->intl
, 'date'], ['needs_environment' => true]),
102 new \Twig\
TwigFilter('intlnumber', [$this->intl
, 'number']),
103 new \Twig\
TwigFilter('intlcurrency', [$this->intl
, 'currency'])
110 public function getAlias(): string {
111 return RapsysPackBundle
::getAlias();