Skip to content

Commit 0dc9b68

Browse files
Version 4 (#5)
* New version with query string fallback * Add travis config file * Update readme * Add build status badge
1 parent 7a9829c commit 0dc9b68

24 files changed

+1074
-100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tests/vendor

.travis.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
8+
env:
9+
global:
10+
- setup=basic
11+
12+
matrix:
13+
include:
14+
- php: 5.6
15+
env: setup=lowest
16+
- php: 5.6
17+
env: setup=stable
18+
19+
sudo: false
20+
21+
services:
22+
- memcached
23+
- redis-server
24+
25+
before_install:
26+
- if [[ $TRAVIS_PHP_VERSION != 7.1 ]] ; then phpenv config-rm xdebug.ini; fi
27+
- travis_retry composer self-update
28+
29+
install:
30+
- cd tests
31+
- if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist --no-suggest; fi
32+
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
33+
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi
34+
35+
script: vendor/bin/phpunit
36+
37+
matrix:
38+
allow_failures:
39+
- php: 7.1
40+
fast_finish: true

assetrev/AssetRevPlugin.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
class AssetRevPlugin extends BasePlugin
55
{
6+
public function init()
7+
{
8+
require_once __DIR__.'/vendor/autoload.php';
9+
}
10+
611
/**
712
* @return string
813
*/
@@ -11,14 +16,22 @@ public function getName()
1116
return 'Asset Rev';
1217
}
1318

19+
/**
20+
* @return string
21+
*/
22+
public function getDescription()
23+
{
24+
return 'Cache bust you asset filenames';
25+
}
26+
1427
/**
1528
* Returns the plugin’s version number.
1629
*
1730
* @return string The plugin’s version number.
1831
*/
1932
public function getVersion()
2033
{
21-
return '3.0.0';
34+
return '4.0.0';
2235
}
2336

2437
/**

assetrev/composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "clubstudioltd/club-asset-rev",
3+
"description": "Craft CMS plugin to aid cache-busting",
4+
"keywords": ["craftcms", "cache-busting"],
5+
"license": "MIT",
6+
"type": "project",
7+
"autoload": {
8+
"psr-4": {
9+
"AssetRev\\Utilities\\": "utilities/"
10+
}
11+
}
12+
}

assetrev/config.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
<?php
22
return array(
3-
'manifestPath' => 'resources/assets/assets.json',
4-
'assetsBasePath' => null,
3+
4+
// The path to your asset manifest, most likely have been generated by a
5+
// task runner such as Gulp or Grunt. Paths will be relative to your craft
6+
// base directory, unless you supply an absolute directory path.
7+
8+
'manifestPath' => 'resources/assets/assets.json',
9+
10+
// The base path to your assets. Again, this is relative to your craft base
11+
// directory, unless you supply an absolute directory path. The path will
12+
// always be appended to the filename that is passed though the `rev()`.
13+
14+
'assetsBasePath' => null,
15+
16+
// A prefix to apply to your assets when they are output from the plugin. It
17+
// would be useful to set this if the paths in your manifest file are likely
18+
// to be different to the final asset url different
19+
20+
'assetUrlPrefix' => null,
21+
522
);

assetrev/resources/icon.svg

Lines changed: 82 additions & 0 deletions
Loading

assetrev/services/AssetRevService.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace Craft;
3+
4+
use InvalidArgumentException;
5+
use AssetRev\Utilities\FilenameRev;
6+
7+
class AssetRevService extends BaseApplicationComponent
8+
{
9+
/**
10+
* Get the filename of a asset
11+
*
12+
* @param $file
13+
* @throws InvalidArgumentException
14+
* @return string
15+
*/
16+
public function getAssetFilename($file)
17+
{
18+
$revver = new FilenameRev(
19+
$this->parseEnvironmentString(craft()->config->get('manifestPath', 'assetrev')),
20+
$this->parseEnvironmentString(craft()->config->get('assetsBasePath', 'assetrev')),
21+
$this->parseEnvironmentString(craft()->config->get('assetPrefix', 'assetrev'))
22+
);
23+
24+
$revver->setBasePath(CRAFT_BASE_PATH);
25+
26+
return $revver->rev($file);
27+
}
28+
29+
/**
30+
* Build an asset's URL
31+
*
32+
* @param string $basePath Base path to assets as defined in the plugin settings
33+
* @param string $file Asset filename
34+
*
35+
* @return string Path to the asset - environment variables having been replaced with their values.
36+
*/
37+
protected function parseEnvironmentString($string)
38+
{
39+
return craft()->config->parseEnvironmentString($string);
40+
}
41+
}

assetrev/twigextensions/AssetRevTwigExtension.php

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33

44
use Twig_Extension;
55
use Twig_Function_Method;
6-
use InvalidArgumentException;
76

87
class 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

Comments
 (0)