]> Raphaël G. Git Repositories - packbundle/blob - Extension/PackExtension.php
Set getAlias function as static
[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 file locator
37 protected $locator;
38
39 //The slugger instance
40 protected $slugger;
41
42 //The assets package
43 protected $package;
44
45 /**
46 * @link https://twig.symfony.com/doc/2.x/advanced.html
47 *
48 * {@inheritdoc}
49 */
50 public function __construct(FileLocator $locator, ContainerInterface $container, PackageInterface $package, SluggerUtil $slugger) {
51 //Set file locator
52 $this->locator = $locator;
53
54 //Set slugger
55 $this->slugger = $slugger;
56
57 //Set assets packages
58 $this->package = $package;
59
60 //Retrieve bundle config
61 if ($parameters = $container->getParameter(self::getAlias())) {
62 //Set config, output and filters arrays
63 foreach(['config', 'output', 'filters'] as $k) {
64 $this->$k = $parameters[$k];
65 }
66 }
67 }
68
69 /**
70 * Returns a list of filters to add to the existing list.
71 *
72 * @return \Twig\TwigFilter[]
73 */
74 public function getTokenParsers(): array {
75 return [
76 new TokenParser($this->locator, $this->package, $this->config, 'stylesheet', $this->output['css'], $this->filters['css']),
77 new TokenParser($this->locator, $this->package, $this->config, 'javascript', $this->output['js'], $this->filters['js']),
78 new TokenParser($this->locator, $this->package, $this->config, 'image', $this->output['img'], $this->filters['img'])
79 ];
80 }
81
82 /**
83 * Returns a list of filters to add to the existing list.
84 *
85 * @return \Twig\TwigFilter[]
86 */
87 public function getFilters(): array {
88 return [
89 new \Twig\TwigFilter('hash', [$this->slugger, 'hash']),
90 new \Twig\TwigFilter('unshort', [$this->slugger, 'unshort']),
91 new \Twig\TwigFilter('short', [$this->slugger, 'short'])
92 ];
93 }
94
95 /**
96 * {@inheritdoc}
97 */
98 public function getAlias(): string {
99 return RapsysPackBundle::getAlias();
100 }
101 }