]> Raphaël G. Git Repositories - packbundle/blob - Extension/PackExtension.php
Add twig intl date, number and currency filters
[packbundle] / Extension / PackExtension.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Extension;
13
14 use Symfony\Component\Asset\PackageInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\HttpKernel\Config\FileLocator;
17 use Twig\Extension\AbstractExtension;
18
19 use Rapsys\PackBundle\Parser\TokenParser;
20 use Rapsys\PackBundle\RapsysPackBundle;
21 use Rapsys\PackBundle\Util\SluggerUtil;
22
23 /**
24 * {@inheritdoc}
25 */
26 class PackExtension extends AbstractExtension {
27 //The config
28 private $config;
29
30 //The output
31 private $output;
32
33 //The filter
34 private $filters;
35
36 //The intl util
37 protected $intl;
38
39 //The file locator
40 protected $locator;
41
42 //The assets package
43 protected $package;
44
45 //The slugger util
46 protected $slugger;
47
48 /**
49 * @link https://twig.symfony.com/doc/2.x/advanced.html
50 *
51 * {@inheritdoc}
52 */
53 public function __construct(ContainerInterface $container, IntlUtil $intl, FileLocator $locator, PackageInterface $package, SluggerUtil $slugger) {
54 //Set intl util
55 $this->intl = $intl;
56
57 //Set file locator
58 $this->locator = $locator;
59
60 //Set assets packages
61 $this->package = $package;
62
63 //Set slugger util
64 $this->slugger = $slugger;
65
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];
71 }
72 }
73 }
74
75 /**
76 * Returns a list of filters to add to the existing list.
77 *
78 * @return \Twig\TwigFilter[]
79 */
80 public function getTokenParsers(): array {
81 return [
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'])
85 ];
86 }
87
88 /**
89 * Returns a list of filters to add to the existing list.
90 *
91 * @return \Twig\TwigFilter[]
92 */
93 public function getFilters(): array {
94 return [
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'])
104 ];
105 }
106
107 /**
108 * {@inheritdoc}
109 */
110 public function getAlias(): string {
111 return RapsysPackBundle::getAlias();
112 }
113 }