]> Raphaël G. Git Repositories - packbundle/blob - Extension/PackExtension.php
Php 8.x constructor style
[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\IntlUtil;
22 use Rapsys\PackBundle\Util\SluggerUtil;
23
24 /**
25 * {@inheritdoc}
26 */
27 class PackExtension extends AbstractExtension {
28 /**
29 * The config array
30 */
31 protected array $config;
32
33 /**
34 * The output array
35 */
36 protected array $output;
37
38 /**
39 * The filter array
40 */
41 protected array $filters;
42
43 /**
44 * @link https://twig.symfony.com/doc/2.x/advanced.html
45 *
46 * {@inheritdoc}
47 */
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(self::getAlias())) {
51 //Set config, output and filters arrays
52 foreach(['config', 'output', 'filters'] as $k) {
53 $this->$k = $parameters[$k];
54 }
55 }
56 }
57
58 /**
59 * Returns a list of filters to add to the existing list.
60 *
61 * @return \Twig\TwigFilter[]
62 */
63 public function getTokenParsers(): array {
64 return [
65 new TokenParser($this->locator, $this->package, $this->config, 'stylesheet', $this->output['css'], $this->filters['css']),
66 new TokenParser($this->locator, $this->package, $this->config, 'javascript', $this->output['js'], $this->filters['js']),
67 new TokenParser($this->locator, $this->package, $this->config, 'image', $this->output['img'], $this->filters['img'])
68 ];
69 }
70
71 /**
72 * Returns a list of filters to add to the existing list.
73 *
74 * @return \Twig\TwigFilter[]
75 */
76 public function getFilters(): array {
77 return [
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')
90 ];
91 }
92
93 /**
94 * {@inheritdoc}
95 */
96 public function getAlias(): string {
97 return RapsysPackBundle::getAlias();
98 }
99 }