33
44use Twig_Extension ;
55use Twig_Function_Method ;
6- use InvalidArgumentException ;
76
87class AssetRevTwigExtension extends Twig_Extension
98{
10- static protected $ settings ;
11- static protected $ manifest ;
12-
139 /**
1410 * Returns the name of the extension.
1511 *
@@ -32,24 +28,6 @@ public function getFunctions()
3228 ];
3329 }
3430
35- /**
36- * Builds an array of settings for the plugin
37- *
38- * @return array
39- */
40- protected function settings ()
41- {
42- if (is_null (self ::$ settings ))
43- {
44- self ::$ settings = array (
45- 'manifestPath ' => craft ()->config ->get ('manifestPath ' , 'assetrev ' ),
46- 'assetsBasePath ' => craft ()->config ->get ('assetsBasePath ' , 'assetrev ' ),
47- );
48- }
49-
50- return self ::$ settings ;
51- }
52-
5331 /**
5432 * Get the filename of a asset
5533 *
@@ -59,73 +37,6 @@ protected function settings()
5937 */
6038 public function getAssetFilename ($ file )
6139 {
62- $ settings = $ this ->settings ();
63- $ manifestPath = $ settings ['manifestPath ' ];
64-
65- if (empty ($ settings ['manifestPath ' ]))
66- {
67- throw new InvalidArgumentException ("Manifest path `manifestPath` not set in plugin configuration. " );
68- }
69-
70- // Allow for a relative path
71- if (strpos ($ manifestPath , DIRECTORY_SEPARATOR ) !== 0 ) {
72- $ manifestPath = CRAFT_BASE_PATH .$ manifestPath ;
73- }
74-
75- // If the manifest file can't be found, we'll just return the original filename
76- if (!$ this ->manifestExists ($ manifestPath ))
77- {
78- return $ this ->buildAssetUrl ($ settings ['assetsBasePath ' ], $ file );
79- }
80-
81- return $ this ->buildAssetUrl ($ settings ['assetsBasePath ' ], $ this ->getAssetRevisionFilename ($ manifestPath , $ file ));
82- }
83-
84- /**
85- * Build an asset's URL
86- *
87- * @param string $basePath Base path to assets as defined in the plugin settings
88- * @param string $file Asset filename
89- *
90- * @return string Path to the asset - environment variables having been replaced with their values.
91- */
92- protected function buildAssetUrl ($ basePath , $ file )
93- {
94- return craft ()->config ->parseEnvironmentString ($ basePath ) . $ file ;
95- }
96-
97- /**
98- * Check if the requested manifest file exists
99- *
100- * @param $manifest
101- *
102- * @return bool
103- */
104- protected function manifestExists ($ manifest )
105- {
106- return is_file ($ manifest );
107- }
108-
109- /**
110- * Get the filename of an asset revision from the asset manifest
111- *
112- * @param $manifestPath
113- * @param $file
114- *
115- * @return mixed
116- */
117- protected function getAssetRevisionFilename ($ manifestPath , $ file )
118- {
119- if (is_null (self ::$ manifest ))
120- {
121- self ::$ manifest = json_decode (file_get_contents ($ manifestPath ), true );
122- }
123-
124- if (!isset (self ::$ manifest [$ file ]))
125- {
126- throw new InvalidArgumentException ("File {$ file } not found in assets manifest " );
127- }
128-
129- return self ::$ manifest [$ file ];
40+ return craft ()->assetRev ->getAssetFilename ($ file );
13041 }
13142}
0 commit comments