]> Raphaƫl G. Git Repositories - packbundle/blob - Twig/PackExtension.php
Add asset path package class
[packbundle] / Twig / PackExtension.php
1 <?php
2 // src/Rapsys/PackBundle/Twig/PackExtension.php
3 namespace Rapsys\PackBundle\Twig;
4
5 use Symfony\Component\HttpKernel\Config\FileLocator;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Symfony\Component\Asset\PackageInterface;
8 use Twig\Extension\AbstractExtension;
9
10 class PackExtension extends AbstractExtension {
11 //The config
12 private $config;
13
14 //The output
15 private $output;
16
17 //The filter
18 private $filters;
19
20 //The file locator
21 protected $locator;
22
23 //The assets package
24 protected $package;
25
26 public function __construct(FileLocator $locator, ContainerInterface $container, PackageInterface $package) {
27 //Set file locator
28 $this->locator = $locator;
29
30 //Set assets packages
31 $this->package = $package;
32
33 //Retrieve bundle config
34 if ($parameters = $container->getParameter($this->getAlias())) {
35 //Set config, output and filters arrays
36 foreach(['config', 'output', 'filters'] as $k) {
37 $this->$k = $parameters[$k];
38 }
39 }
40 }
41
42 public function getTokenParsers() {
43 return [
44 new PackTokenParser($this->locator, $this->package, $this->config, 'stylesheet', $this->output['css'], $this->filters['css']),
45 new PackTokenParser($this->locator, $this->package, $this->config, 'javascript', $this->output['js'], $this->filters['js']),
46 new PackTokenParser($this->locator, $this->package, $this->config, 'image', $this->output['img'], $this->filters['img'])
47 ];
48 }
49
50 /**
51 * {@inheritdoc}
52 */
53 public function getAlias() {
54 return 'rapsys_pack';
55 }
56 }