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