Skip to content

Commit

Permalink
Implement manifest url revving. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leevigraham committed Jun 13, 2017
1 parent fb575f1 commit e3a5774
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
## 0.0.1 - 2017.06.05
### Added
- Initial release

## 0.0.2 - 2017.06.13
### Added
- Added manifest.json parsing
70 changes: 65 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "newism/craft3-asset-rev",
"description": "Rev asset urls with timestamps",
"type": "craft-plugin",
"version": "0.0.1",
"version": "0.0.2",
"keywords": [
"craft",
"cms",
Expand Down
5 changes: 5 additions & 0 deletions src/NsmAssetRev.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace newism\assetrev;

use newism\assetrev\models\Settings;
use newism\assetrev\twigextensions\NsmAssetRevTwigExtension;

use Craft;
Expand Down Expand Up @@ -111,4 +112,8 @@ function (PluginEvent $event) {
// Protected Methods
// =========================================================================

protected function createSettingsModel()
{
return new Settings();
}
}
41 changes: 41 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* NSM Asset Rev plugin for Craft CMS 3.x
*
* Various fields for CraftCMS
*
* @link http://newism.com.au
* @copyright Copyright (c) 2017 Leevi Graham
*/

/**
* NSM Asset Rev config.php
*
* Completely optional configuration settings for NSM Fields if you want to customize some
* of its more esoteric behavior, or just want specific control over things.
*
* Don't edit this file, instead copy it to 'craft/config' as 'nsmassetrev.php' and make
* your changes there.
*
* Once copied to 'craft/config', this file will be multi-environment aware as well, so you can
* have different settings groups for each environment, just as you do for 'general.php'
*/
return array(

'*' => [

/**
* 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' => '',
],
);
14 changes: 14 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace newism\assetrev\models;

class Settings extends \craft\base\Model
{
public $manifestPath;
public $assetUrlPrefix;

public function rules()
{
return [];
}
}
60 changes: 59 additions & 1 deletion src/twigextensions/NsmAssetRevTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace newism\assetrev\twigextensions;

use craft\elements\Asset;
use craft\helpers\UrlHelper;
use newism\assetrev\NsmAssetRev;

/**
* Twig can be extended in many ways; you can add extra tags, filters, tests, operators,
Expand All @@ -25,6 +27,13 @@
*/
class NsmAssetRevTwigExtension extends \Twig_Extension
{

/**
* @var array
*/
static protected $manifest;


// Public Methods
// =========================================================================

Expand All @@ -48,12 +57,28 @@ public function getName(): string
public function getFunctions(): array
{
return [
new \Twig_Function('nsm_rev_asset', [$this, 'revAsset']),
new \Twig_Function('nsm_rev_url', [$this, 'revUrl']),
new \Twig_Function('nsm_rev_asset_url', [$this, 'revAssetUrl']),
new \Twig_Function('nsm_rev_manifest_url', [$this, 'revManifestUrl']),
];
}

/**
* Helper function that accepts either an Asset or url
*
* @return string
*/
public function revUrl(): string
{
$args = func_get_args();
$method = ($args[0] instanceof Asset) ? 'revAssetUrl' : 'revManifestUrl';

return $this->$method(...$args);
}

/**
* Rev an asset url and optional transform
*
* @param Asset $asset
* @param null $transform
* @return string
Expand All @@ -70,6 +95,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
Expand Down

0 comments on commit e3a5774

Please sign in to comment.