From e2c64b026e2ea3809ccef8aeed95f758e8879ef8 Mon Sep 17 00:00:00 2001 From: Leevi Graham Date: Tue, 13 Jun 2017 18:43:08 +1000 Subject: [PATCH] Implement manifest url revving. Fixes #1 --- README.md | 70 +++++++++++++++++-- src/NsmAssetRev.php | 5 ++ src/config.php | 41 +++++++++++ src/models/Settings.php | 14 ++++ .../NsmAssetRevTwigExtension.php | 62 +++++++++++++++- 5 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 src/config.php create mode 100644 src/models/Settings.php diff --git a/README.md b/README.md index 177b7fb..875f3d1 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,48 @@ # NSM Asset Rev plugin for Craft CMS 3.x -Rev asset urls with date modified timestamps. Its simple… just call the +## Asset Models + +Rev `Asset` model urls with date modified timestamps. Its simple… just call the function and pass the asset and an optional transform. Before: Template: {{ asset.url(transform) }} - Output: https://local.craft3/uploads/hqdefault.jpg + Output: https://local.craft3/uploads/biggie.jpg After: Template: {{ nsm_rev_asset_url(asset, transform) }} - Output: https://local.craft3/uploads/hqdefault.1496670969.jpg + Output: https://local.craft3/uploads/biggie.1496670969.jpg + +## Manifest Files + +Additionally NSM Asset Rev can check a manifest file for urls and return the +marching revved url. + +Manifest: + + { "app.css": "app.1e9915c1398b2ba2fcc2.css" } + +Before: + + Template: {{ url(app.css) }} + Output: https://local.craft3/app.css + +After: + + Template: {{ nsm_rev_manifest_url('app.css') }} + Output: https://local.craft3/app.1e9915c1398b2ba2fcc2.css + +Manifest files will most likely be created from a build process. + +Here's some examples: + +* [grunt-filerev-assets](https://github.com/richardbolt/grunt-filerev-assets) +* [gulp-rev](https://github.com/sindresorhus/gulp-rev) +* [webpack-manifest-plugin](https://github.com/danethurber/webpack-manifest-plugin) + +Note: NSM Asset Rev only supports one level key: value pairs. ## Installation @@ -34,9 +65,34 @@ Install plugin in the Craft Control Panel under Settings > Plugins. ## Usage -This plugin provides a single twig function: +### Twig template functions + +This plugin provides two twig functions. + +#### Asset Models {{ nsm_rev_asset_url(asset, transform) }} + +#### Manifest Files + + {{ nsm_rev_manifest_url(url) }} + +#### Helper + +There's actually three twig functions :). The third is a helper that either calls +`nsm_rev_asset_url` or `nsm_rev_manifest_url` based on the arguments. + +If the first argument is an `Assetl` then `nsm_rev_asset_url` will be called +internally: + + Template: {{ nsm_rev_url(asset, transform) }} + Output: https://local.craft3/uploads/biggie.1496670969.jpg + +If the first argument is an `string` model then `nsm_rev_manifest_url` will be called +internally: + + Template: {{ nsm_rev_url('app.css') }} + Output: https://local.craft3/app.1e9915c1398b2ba2fcc2.css ### Server Configuration @@ -79,6 +135,10 @@ See: https://github.com/h5bp/server-configs-nginx/blob/master/h5bp/location/cach try_files $uri $1.$2; } +## Plugin Configuration + +See [./src/config.json](./src/config.json). + ## Road Map Some things to do, and ideas for potential features: @@ -89,7 +149,7 @@ Some things to do, and ideas for potential features: ### Future -* Add manfiest based revving +* ~~[Add manfiest based revving](https://github.com/newism/craft3-asset-rev/issues/1)~~ * Integrate other 3rd party image manipulation plugins as they become available ## Credits diff --git a/src/NsmAssetRev.php b/src/NsmAssetRev.php index 49b51a1..911699e 100644 --- a/src/NsmAssetRev.php +++ b/src/NsmAssetRev.php @@ -10,6 +10,7 @@ namespace newism\assetrev; +use newism\assetrev\models\Settings; use newism\assetrev\twigextensions\NsmAssetRevTwigExtension; use Craft; @@ -111,4 +112,8 @@ function (PluginEvent $event) { // Protected Methods // ========================================================================= + protected function createSettingsModel() + { + return new Settings(); + } } diff --git a/src/config.php b/src/config.php new file mode 100644 index 0000000..a8aa3b9 --- /dev/null +++ b/src/config.php @@ -0,0 +1,41 @@ + [ + + /** + * Fully qualified path to your manifest file + */ + 'manifestPath' => CRAFT_BASE_PATH.'/manifest.json', + + /** + * Prefixed to the manifest value. + * + * If the final url assetUrlPrefix + manifest url is relative the siteUrl + * will be appended. If the final qualified url is absolute or contains a + * fully qualified domain it will not be modified. + */ + 'assetUrlPrefix' => '', + ], +); diff --git a/src/models/Settings.php b/src/models/Settings.php new file mode 100644 index 0000000..51d5783 --- /dev/null +++ b/src/models/Settings.php @@ -0,0 +1,14 @@ +$method(...$args); + } + + /** + * Rev an asset url and optional transform + * * @param Asset $asset * @param null $transform * @return string @@ -70,6 +97,39 @@ public function revAssetUrl(Asset $asset, $transform = null): string } /** + * Rev a url checking the manifest + * + * @param $url + * @return string + */ + public function revManifestUrl($url): string + { + $pluginSettings = NsmAssetRev::getInstance()->getSettings(); + $manifestPath = $pluginSettings['manifestPath']; + + if (null === self::$manifest) { + try { + self::$manifest = json_decode( + file_get_contents($manifestPath), + true + ); + } catch (\Exception $exception) { + self::$manifest = []; + } + } + + $url = array_key_exists($url, self::$manifest) + ? self::$manifest[$url] + : $url; + + $url = $pluginSettings['assetUrlPrefix'].$url; + + return UrlHelper::url($url); + } + + /** + * Add a timestamp to a url + * * @param $timestamp * @param $url * @param $extension