Skip to content

Instantly share code, notes, and snippets.

@jonleverrier
Last active December 14, 2018 12:38
Show Gist options
  • Save jonleverrier/0fe3707da6ea0ee1c9fe7902ce3ccb94 to your computer and use it in GitHub Desktop.
Save jonleverrier/0fe3707da6ea0ee1c9fe7902ce3ccb94 to your computer and use it in GitHub Desktop.
This MODX snippet retrieves CSS and JS files from your local file system, which is handy when you are adding a revision suffix to your build files.
<?php
/**
* getAsset
* v.0.0.2
*
* This snippet retrieves CSS and JS files from your local file system,
* which is handy when you are adding a revision suffix to your build files.
* For example: 's.min.cdbbec54.js' or 'c.min.543e2b40.css'.
*
* The script attempts to serve and look for the latest file in
* the specifed file path in the following format (any other pattern
* will be filtered out):
*
* - c.min.<8 character hash>.css
* - ie.min.<8 character hash>.css
* - s.min.<8 character hash>.js
*
* Example usage:
*
* [[getAsset? &cssPath=`/assets/css` &jsPath=`/assets/js`]]
* [[+getasset.js]]
* [[+getasset.css]]
* [[+getasset.css_ie]]
*
* Call the snippet cached!
*/
// get css asset path from snippet parameter
$cssPath = $cssPath ?: '/assets/template/c';
// get js asset path from snippet parameter
$jsPath = $jsPath ?: '/assets/template/j';
// accept file beginning with 's'
// followed by .min.
// followed by any 8 digit hash
// ending in js
$pattern_js = '/^(s).min.*\S{8}.js$/';
// accept files beginning with 'c' or 'ie'
// followed by .min.
// followed by any 8 digit hash
// ending in css
$pattern_css = '/^(c).min.*\S{8}.css$/';
$pattern_css_ie = '/^(ie).min.*\S{8}.css$/';
// define what folder to look in and apply the pattern match
$asset_array_js = preg_grep($pattern_js, scandir('.' . $jsPath));
$asset_array_css = preg_grep($pattern_css, scandir('.' . $cssPath));
$asset_array_css_ie = preg_grep($pattern_css_ie, scandir('.' . $cssPath));
$asset_array_js_sortby = array();
$asset_array_css_sortby = array();
$asset_array_css_ie_sortby = array();
// for each js asset in the folder...
foreach ($asset_array_js as $value) {
// get timestamp
$last_modified = filemtime( '.' . $jsPath . '/' . $value);
// add timestamp to new array
$asset_array_js_sortby[] = array(
'asset' => $value,
'timestamp' => $last_modified
);
}
// for each css asset in the folder...
foreach ($asset_array_css as $value) {
// get timestamp
$last_modified = filemtime( '.' . $cssPath . '/' . $value);
// add timestamp to new array
$asset_array_css_sortby[] = array(
'asset' => $value,
'timestamp' => $last_modified
);
}
// for each css ie asset in the folder...
foreach ($asset_array_css_ie as $value) {
// get timestamp
$last_modified = filemtime( '.' . $cssPath . '/' . $value);
// add timestamp to new array
$asset_array_css_ie_sortby[] = array(
'asset' => $value,
'timestamp' => $last_modified
);
}
// sort arrays based on timestamp, to ensure we get the latest asset
usort($asset_array_js_sortby, function ($a, $b) {
return $b["timestamp"] - $a["timestamp"];
});
usort($asset_array_css_sortby, function ($a, $b) {
return $b["timestamp"] - $a["timestamp"];
});
usort($asset_array_css_ie_sortby, function ($a, $b) {
return $b["timestamp"] - $a["timestamp"];
});
// take the first result in the array
$latest_css = reset($asset_array_css_sortby);
$latest_ie = reset($asset_array_css_ie_sortby);
$latest_js = reset($asset_array_js_sortby);
// take value from asset key (the filename);
$css = $latest_css['asset'];
$ie = $latest_ie['asset'];
$js = $latest_js['asset'];
// set placeholders and apply namespace
$output = $modx->setPlaceholders(array (
'css' => $css,
'css_ie' => $ie,
'js' => $js
), 'getasset.');
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment