Skip to content

Commit

Permalink
Version 4 (#5)
Browse files Browse the repository at this point in the history
* New version with query string fallback
* Add travis config file
* Update readme
* Add build status badge
  • Loading branch information
scottwakefield committed Oct 27, 2016
1 parent 7a9829c commit 0dc9b68
Show file tree
Hide file tree
Showing 24 changed files with 1,074 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests/vendor
40 changes: 40 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
language: php

php:
- 5.6
- 7.0
- 7.1

env:
global:
- setup=basic

matrix:
include:
- php: 5.6
env: setup=lowest
- php: 5.6
env: setup=stable

sudo: false

services:
- memcached
- redis-server

before_install:
- if [[ $TRAVIS_PHP_VERSION != 7.1 ]] ; then phpenv config-rm xdebug.ini; fi
- travis_retry composer self-update

install:
- cd tests
- if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist --no-suggest; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi

script: vendor/bin/phpunit

matrix:
allow_failures:
- php: 7.1
fast_finish: true
15 changes: 14 additions & 1 deletion assetrev/AssetRevPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

class AssetRevPlugin extends BasePlugin
{
public function init()
{
require_once __DIR__.'/vendor/autoload.php';
}

/**
* @return string
*/
Expand All @@ -11,14 +16,22 @@ public function getName()
return 'Asset Rev';
}

/**
* @return string
*/
public function getDescription()
{
return 'Cache bust you asset filenames';
}

/**
* Returns the plugin’s version number.
*
* @return string The plugin’s version number.
*/
public function getVersion()
{
return '3.0.0';
return '4.0.0';
}

/**
Expand Down
12 changes: 12 additions & 0 deletions assetrev/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "clubstudioltd/club-asset-rev",
"description": "Craft CMS plugin to aid cache-busting",
"keywords": ["craftcms", "cache-busting"],
"license": "MIT",
"type": "project",
"autoload": {
"psr-4": {
"AssetRev\\Utilities\\": "utilities/"
}
}
}
21 changes: 19 additions & 2 deletions assetrev/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
<?php
return array(
'manifestPath' => 'resources/assets/assets.json',
'assetsBasePath' => null,

// The path to your asset manifest, most likely have been generated by a
// task runner such as Gulp or Grunt. Paths will be relative to your craft
// base directory, unless you supply an absolute directory path.

'manifestPath' => 'resources/assets/assets.json',

// The base path to your assets. Again, this is relative to your craft base
// directory, unless you supply an absolute directory path. The path will
// always be appended to the filename that is passed though the `rev()`.

'assetsBasePath' => null,

// A prefix to apply to your assets when they are output from the plugin. It
// would be useful to set this if the paths in your manifest file are likely
// to be different to the final asset url different

'assetUrlPrefix' => null,

);
82 changes: 82 additions & 0 deletions assetrev/resources/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions assetrev/services/AssetRevService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Craft;

use InvalidArgumentException;
use AssetRev\Utilities\FilenameRev;

class AssetRevService extends BaseApplicationComponent
{
/**
* Get the filename of a asset
*
* @param $file
* @throws InvalidArgumentException
* @return string
*/
public function getAssetFilename($file)
{
$revver = new FilenameRev(
$this->parseEnvironmentString(craft()->config->get('manifestPath', 'assetrev')),
$this->parseEnvironmentString(craft()->config->get('assetsBasePath', 'assetrev')),
$this->parseEnvironmentString(craft()->config->get('assetPrefix', 'assetrev'))
);

$revver->setBasePath(CRAFT_BASE_PATH);

return $revver->rev($file);
}

/**
* Build an asset's URL
*
* @param string $basePath Base path to assets as defined in the plugin settings
* @param string $file Asset filename
*
* @return string Path to the asset - environment variables having been replaced with their values.
*/
protected function parseEnvironmentString($string)
{
return craft()->config->parseEnvironmentString($string);
}
}
91 changes: 1 addition & 90 deletions assetrev/twigextensions/AssetRevTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

use Twig_Extension;
use Twig_Function_Method;
use InvalidArgumentException;

class AssetRevTwigExtension extends Twig_Extension
{
static protected $settings;
static protected $manifest;

/**
* Returns the name of the extension.
*
Expand All @@ -32,24 +28,6 @@ public function getFunctions()
];
}

/**
* Builds an array of settings for the plugin
*
* @return array
*/
protected function settings()
{
if (is_null(self::$settings))
{
self::$settings = array(
'manifestPath' => craft()->config->get('manifestPath', 'assetrev'),
'assetsBasePath' => craft()->config->get('assetsBasePath', 'assetrev'),
);
}

return self::$settings;
}

/**
* Get the filename of a asset
*
Expand All @@ -59,73 +37,6 @@ protected function settings()
*/
public function getAssetFilename($file)
{
$settings = $this->settings();
$manifestPath = $settings['manifestPath'];

if (empty($settings['manifestPath']))
{
throw new InvalidArgumentException("Manifest path `manifestPath` not set in plugin configuration.");
}

// Allow for a relative path
if (strpos($manifestPath, DIRECTORY_SEPARATOR) !== 0) {
$manifestPath = CRAFT_BASE_PATH.$manifestPath;
}

// If the manifest file can't be found, we'll just return the original filename
if (!$this->manifestExists($manifestPath))
{
return $this->buildAssetUrl($settings['assetsBasePath'], $file);
}

return $this->buildAssetUrl($settings['assetsBasePath'], $this->getAssetRevisionFilename($manifestPath, $file));
}

/**
* Build an asset's URL
*
* @param string $basePath Base path to assets as defined in the plugin settings
* @param string $file Asset filename
*
* @return string Path to the asset - environment variables having been replaced with their values.
*/
protected function buildAssetUrl($basePath, $file)
{
return craft()->config->parseEnvironmentString($basePath) . $file;
}

/**
* Check if the requested manifest file exists
*
* @param $manifest
*
* @return bool
*/
protected function manifestExists($manifest)
{
return is_file($manifest);
}

/**
* Get the filename of an asset revision from the asset manifest
*
* @param $manifestPath
* @param $file
*
* @return mixed
*/
protected function getAssetRevisionFilename($manifestPath, $file)
{
if (is_null(self::$manifest))
{
self::$manifest = json_decode(file_get_contents($manifestPath), true);
}

if (!isset(self::$manifest[$file]))
{
throw new InvalidArgumentException("File {$file} not found in assets manifest");
}

return self::$manifest[$file];
return craft()->assetRev->getAssetFilename($file);
}
}
Loading

0 comments on commit 0dc9b68

Please sign in to comment.