1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 OpenSky Project Inc
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Assetic\Extension\Twig;
13
14use Assetic\Factory\AssetFactory;
15use Assetic\ValueSupplierInterface;
16
17class AsseticExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
18{
19    protected $factory;
20    protected $functions;
21    protected $valueSupplier;
22
23    public function __construct(AssetFactory $factory, $functions = array(), ValueSupplierInterface $valueSupplier = null)
24    {
25        $this->factory = $factory;
26        $this->functions = array();
27        $this->valueSupplier = $valueSupplier;
28
29        foreach ($functions as $function => $options) {
30            if (is_integer($function) && is_string($options)) {
31                $this->functions[$options] = array('filter' => $options);
32            } else {
33                $this->functions[$function] = $options + array('filter' => $function);
34            }
35        }
36    }
37
38    public function getTokenParsers()
39    {
40        return array(
41            new AsseticTokenParser($this->factory, 'javascripts', 'js/*.js'),
42            new AsseticTokenParser($this->factory, 'stylesheets', 'css/*.css'),
43            new AsseticTokenParser($this->factory, 'image', 'images/*', true),
44        );
45    }
46
47    public function getFunctions()
48    {
49        $functions = array();
50        foreach ($this->functions as $function => $filter) {
51            $functions[] = new AsseticFilterFunction($function);
52        }
53
54        return $functions;
55    }
56
57    public function getGlobals()
58    {
59        return array(
60            'assetic' => array(
61                'debug' => $this->factory->isDebug(),
62                'vars'  => null !== $this->valueSupplier ? new ValueContainer($this->valueSupplier) : array(),
63            ),
64        );
65    }
66
67    public function getFilterInvoker($function)
68    {
69        return new AsseticFilterInvoker($this->factory, $this->functions[$function]);
70    }
71
72    public function getName()
73    {
74        return 'assetic';
75    }
76}
77