|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the m1\twig-asset-rev-extension library |
|
5
|
|
|
* |
|
6
|
|
|
* (c) m1 <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @package m1/twig-asset-rev-extension |
|
12
|
|
|
* @version 0.1.0 |
|
13
|
|
|
* @author Miles Croxford <[email protected]> |
|
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
|
15
|
|
|
* @license http://github.com/m1/TwigAssetRevExtension/blob/master/LICENSE |
|
16
|
|
|
* @link http://github.com/m1/TwigAssetRevExtension/blob/master/README.MD Documentation |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace M1\TwigAssetRevExtension; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The TwigAssetRevExtension class |
|
23
|
|
|
* |
|
24
|
|
|
* @since 0.1.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class TwigAssetRevExtension extends \Twig_Extension |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The file extensions to check if there is a minified version |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private static $minify_exts = array('css', 'js'); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The array of assets rev, raw_asset => rev_asset |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $assets; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Whether to search for minified rev'd versions of the assets |
|
44
|
|
|
* |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
private $minified; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The TwigAssetRevExtension constructor |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $assets The array of assets and rev'd assets |
|
53
|
|
|
* @param bool $minified Whether to search for minified rev'd assets |
|
54
|
|
|
*/ |
|
55
|
24 |
|
public function __construct(array $assets, $minified = true) |
|
56
|
|
|
{ |
|
57
|
24 |
|
$this->assets = $assets; |
|
58
|
24 |
|
$this->minified = $minified; |
|
59
|
24 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function getFilters() |
|
65
|
|
|
{ |
|
66
|
|
|
return array( |
|
67
|
9 |
|
new \Twig_SimpleFilter('asset_rev', array($this, 'assetRev'), array('needs_environment' => true)), |
|
68
|
9 |
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Gets the rev'd asset, |
|
73
|
|
|
* |
|
74
|
|
|
* @param \Twig_Environment $env The twig environment |
|
75
|
|
|
* @param string $asset The asset string to rev |
|
76
|
|
|
* |
|
77
|
|
|
* @return string The rev'd asset if available, else the original asset |
|
78
|
|
|
*/ |
|
79
|
24 |
|
public function assetRev(\Twig_Environment $env, $asset) |
|
80
|
|
|
{ |
|
81
|
24 |
|
$pathinfo = pathinfo($asset); |
|
82
|
|
|
|
|
83
|
24 |
|
if (!isset($pathinfo['extension'])) { |
|
84
|
6 |
|
return $asset; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
18 |
|
return ($this->minify($env, $pathinfo)) ?: ((isset($this->assets[$asset])) ? $this->assets[$asset] : $asset); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets the minified asset |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Twig_Environment $env The twig environment |
|
94
|
|
|
* @param array $pathinfo The pathinfo for the asset |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool|string The minified rev'd asset if available, else false |
|
97
|
|
|
*/ |
|
98
|
18 |
|
public function minify($env, $pathinfo) |
|
99
|
|
|
{ |
|
100
|
18 |
|
if ($this->minified && !$env->isDebug() && in_array($pathinfo['extension'], self::$minify_exts)) { |
|
101
|
12 |
|
$min = sprintf( |
|
102
|
12 |
|
"%s/%s.min.%s", |
|
103
|
12 |
|
$pathinfo['dirname'], |
|
104
|
12 |
|
$pathinfo['filename'], |
|
105
|
12 |
|
$pathinfo['extension'] |
|
106
|
12 |
|
); |
|
107
|
|
|
|
|
108
|
12 |
|
if (isset($this->assets[$min])) { |
|
109
|
6 |
|
return $this->assets[$min]; |
|
110
|
|
|
} |
|
111
|
6 |
|
} |
|
112
|
|
|
|
|
113
|
15 |
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the name of the extension. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string The extension name |
|
120
|
|
|
*/ |
|
121
|
24 |
|
public function getName() |
|
122
|
|
|
{ |
|
123
|
24 |
|
return 'asset_rev'; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|