]> Raphaël G. Git Repositories - packbundle/blob - Extension/PackExtension.php
Add download and base64 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\IntlUtil;
22 use Rapsys\PackBundle\Util\SluggerUtil;
23
24 /**
25 * {@inheritdoc}
26 */
27 class PackExtension extends AbstractExtension {
28 //The config
29 private $config;
30
31 //The output
32 private $output;
33
34 //The filter
35 private $filters;
36
37 //The intl util
38 protected $intl;
39
40 //The file locator
41 protected $locator;
42
43 //The assets package
44 protected $package;
45
46 //The slugger util
47 protected $slugger;
48
49 /**
50 * @link https://twig.symfony.com/doc/2.x/advanced.html
51 *
52 * {@inheritdoc}
53 */
54 public function __construct(ContainerInterface $container, IntlUtil $intl, FileLocator $locator, PackageInterface $package, SluggerUtil $slugger) {
55 //Set intl util
56 $this->intl = $intl;
57
58 //Set file locator
59 $this->locator = $locator;
60
61 //Set assets packages
62 $this->package = $package;
63
64 //Set slugger util
65 $this->slugger = $slugger;
66
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];
72 }
73 }
74 }
75
76 /**
77 * Returns a list of filters to add to the existing list.
78 *
79 * @return \Twig\TwigFilter[]
80 */
81 public function getTokenParsers(): array {
82 return [
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'])
86 ];
87 }
88
89 /**
90 * Returns a list of filters to add to the existing list.
91 *
92 * @return \Twig\TwigFilter[]
93 */
94 public function getFilters(): array {
95 return [
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')
108 ];
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function getAlias(): string {
115 return RapsysPackBundle::getAlias();
116 }
117 }