1 | <?php |
||
8 | class ElixirExtension extends \Twig_Extension |
||
9 | { |
||
10 | /** @var string */ |
||
11 | protected $webDir; |
||
12 | |||
13 | /** @var string */ |
||
14 | protected $buildDir; |
||
15 | |||
16 | /** |
||
17 | * ElixirExtension constructor. |
||
18 | * |
||
19 | * @param string $webDir |
||
20 | * @param string $buildDir |
||
21 | */ |
||
22 | public function __construct($webDir, $buildDir) |
||
23 | { |
||
24 | $this->webDir = $webDir; |
||
25 | $this->buildDir = $buildDir; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @return array |
||
30 | */ |
||
31 | public function getFunctions() |
||
32 | { |
||
33 | return [ |
||
34 | new \Twig_SimpleFunction('elixir', [$this, 'getAssetVersion']), |
||
35 | ]; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param $asset |
||
40 | * |
||
41 | * @throws \Exception |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getAssetVersion($asset) |
||
46 | { |
||
47 | $asset = trim($asset, '/'); |
||
48 | |||
49 | $manifest = $this->readManifest(); |
||
50 | |||
51 | if (array_key_exists($asset, $manifest)) { |
||
52 | return sprintf( |
||
53 | '%s%s%s%s', |
||
54 | DIRECTORY_SEPARATOR, |
||
55 | $this->buildDir, |
||
56 | DIRECTORY_SEPARATOR, |
||
57 | $manifest[$asset] |
||
58 | ); |
||
59 | } |
||
60 | |||
61 | throw new \Exception(sprintf('File %s not defined in asset manifest.', $asset)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns the name of the extension. |
||
66 | * |
||
67 | * @return string The extension name |
||
68 | */ |
||
69 | public function getName() |
||
73 | |||
74 | /** |
||
75 | * @return mixed |
||
76 | */ |
||
77 | private function readManifest() |
||
95 | |||
96 | /** |
||
97 | * @throws \Exception |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | private function checkManifestFileExists() |
||
118 | } |
||
119 |