Skip to content

Commit

Permalink
Added assets versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Feb 1, 2017
1 parent 27dbc70 commit 356cfc7
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
- Fixes internal cache saving in `Phalcon\Mvc\Model\Binder` when no cache backend is used
- Added the ability to get original values from `Phalcon\Mvc\Model\Binder`, added `Phalcon\Mvc\Micro::getModelBinder`, `Phalcon\Dispatcher::getModelBinder`
- Added `prepend` parameter to `Phalcon\Loader::register` to specify autoloader's loading order to top most
- Added assets versioning with methods `Phalcon\Assets\Collection::getVersion`, `Phalcon\Assets\Collection::setVersion`, `Phalcon\Assets\Resource::setVersion`, `Phalcon\Assets\Resource::getVersion`

# [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (XXXX-XX-XX)
- Fixed Isnull check is not correct when the model field defaults to an empty string. [#12507](https://github.com/phalcon/cphalcon/issues/12507)
Expand Down
21 changes: 17 additions & 4 deletions phalcon/assets/collection.zep
Expand Up @@ -59,6 +59,8 @@ class Collection implements \Countable, \Iterator

protected _sourcePath { get };

protected _version { get };

/**
* Adds a resource to the collection
*/
Expand All @@ -80,7 +82,7 @@ class Collection implements \Countable, \Iterator
/**
* Adds a CSS resource to the collection
*/
public function addCss(string! path, var local = null, boolean filter = true, attributes = null) -> <Collection>
public function addCss(string! path, var local = null, boolean filter = true, attributes = null, version = null) -> <Collection>
{
var collectionLocal, collectionAttributes;

Expand All @@ -96,7 +98,7 @@ class Collection implements \Countable, \Iterator
let collectionAttributes = this->_attributes;
}

let this->_resources[] = new ResourceCss(path, collectionLocal, filter, collectionAttributes);
let this->_resources[] = new ResourceCss(path, collectionLocal, filter, collectionAttributes, version);

return this;
}
Expand Down Expand Up @@ -127,7 +129,7 @@ class Collection implements \Countable, \Iterator
* @param array attributes
* @return \Phalcon\Assets\Collection
*/
public function addJs(string! path, var local = null, boolean filter = true, attributes = null) -> <Collection>
public function addJs(string! path, var local = null, boolean filter = true, attributes = null, version = null) -> <Collection>
{
var collectionLocal, collectionAttributes;

Expand All @@ -143,7 +145,7 @@ class Collection implements \Countable, \Iterator
let collectionAttributes = this->_attributes;
}

let this->_resources[] = new ResourceJs(path, collectionLocal, filter, collectionAttributes);
let this->_resources[] = new ResourceJs(path, collectionLocal, filter, collectionAttributes, version);

return this;
}
Expand Down Expand Up @@ -288,6 +290,17 @@ class Collection implements \Countable, \Iterator
return this;
}

/**
* Sets version
*
* @param int|string|bool version
*/
public function setVersion(var version) -> <Collection>
{
let this->_version = version;
return this;
}

/**
* Sets if all filtered resources in the collection must be joined in a single result file
*/
Expand Down
37 changes: 32 additions & 5 deletions phalcon/assets/manager.zep
Expand Up @@ -93,9 +93,9 @@ class Manager
* $assets->addCss("http://bootstrap.my-cdn.com/style.css", false);
*</code>
*/
public function addCss(string! path, local = true, filter = true, var attributes = null) -> <Manager>
public function addCss(string! path, local = true, filter = true, var attributes = null, var version = null) -> <Manager>
{
this->addResourceByType("css", new ResourceCss(path, local, filter, attributes));
this->addResourceByType("css", new ResourceCss(path, local, filter, attributes, version));
return this;
}

Expand All @@ -116,9 +116,9 @@ class Manager
* $assets->addJs("http://jquery.my-cdn.com/jquery.js", false);
*</code>
*/
public function addJs(string! path, local = true, filter = true, attributes = null) -> <Manager>
public function addJs(string! path, local = true, filter = true, attributes = null, version = null) -> <Manager>
{
this->addResourceByType("js", new ResourceJs(path, local, filter, attributes));
this->addResourceByType("js", new ResourceJs(path, local, filter, attributes, version));
return this;
}

Expand Down Expand Up @@ -301,7 +301,7 @@ class Manager
collectionTargetPath, completeTargetPath, filteredJoinedContent, join,
$resource, filterNeeded, local, sourcePath, targetPath, path, prefixedPath,
attributes, parameters, html, useImplicitOutput, content, mustFilter,
filter, filteredContent, typeCss, targetUri;
filter, filteredContent, typeCss, targetUri, version;

let useImplicitOutput = this->_implicitOutput;

Expand Down Expand Up @@ -488,6 +488,15 @@ class Manager
let prefixedPath = path;
}

if $resource->getVersion() == null && fetch version, collection->getVersion() {
if version {
if version === true && local {
let version = filemtime($resource->getRealSourcePath());
}
let prefixedPath = prefixedPath . "?ver=" . version;
}
}

/**
* Gets extra HTML attributes in the resource
*/
Expand Down Expand Up @@ -607,6 +616,15 @@ class Manager
*/
let local = true;

if $resource->getVersion() == null && fetch version, collection->getVersion() {
if version {
if version === true && local {
let version = filemtime($resource->getRealSourcePath());
}
let prefixedPath = prefixedPath . "?ver=" . version;
}
}

/**
* Prepare the parameters for the callback
*/
Expand Down Expand Up @@ -656,6 +674,15 @@ class Manager
let prefixedPath = targetUri;
}

if fetch version, collection->getVersion() {
if version {
if version === true {
let version = filemtime(completeTargetPath);
}
let prefixedPath = prefixedPath . "?ver=" . version;
}
}

/**
* Gets extra HTML attributes in the collection
*/
Expand Down
30 changes: 27 additions & 3 deletions phalcon/assets/resource.zep
Expand Up @@ -61,6 +61,8 @@ class $Resource

protected _targetUri { get };

protected _version { get };

/**
* Phalcon\Assets\Resource constructor
*
Expand All @@ -69,13 +71,15 @@ class $Resource
* @param boolean local
* @param boolean filter
* @param array attributes
* @param int|string|bool version
*/
public function __construct(string type, string path, boolean local = true, boolean filter = true, attributes = null)
public function __construct(string type, string path, boolean local = true, boolean filter = true, attributes = null, version = null)
{
let this->_type = type,
this->_path = path,
this->_local = local,
this->_filter = filter;
this->_filter = filter,
this->_version = version;
if typeof attributes == "array" {
let this->_attributes = attributes;
}
Expand Down Expand Up @@ -153,6 +157,17 @@ class $Resource
return this;
}

/**
* Sets version
*
* @param int|string|bool version
*/
public function setVersion(var version) -> <$Resource>
{
let this->_version = version;
return this;
}

/**
* Returns the content of the resource as an string
* Optionally a base path where the resource is located can be set
Expand Down Expand Up @@ -200,12 +215,21 @@ class $Resource
*/
public function getRealTargetUri() -> string
{
var targetUri;
var targetUri, version;

let targetUri = this->_targetUri;
if empty targetUri {
let targetUri = this->_path;
}

let version = this->_version;
if version {
if version === true && this->_local {
let version = filemtime(this->getRealSourcePath());
}
let targetUri = targetUri . "?ver=" . version;
}

return targetUri;
}

Expand Down
66 changes: 66 additions & 0 deletions tests/unit/Assets/ManagerTest.php
Expand Up @@ -492,4 +492,70 @@ function () {
}
);
}

/**
* Tests assets versioning
*
* @author Wojciech Ślawski <jurigag@gmail.com>
* @since 2017-02-01
*/
public function testAssetsVersioning()
{
$this->specify(
"Assets versioning doesn't work correctly",
function () {
$assets = new Manager();

$assets->addJs('js/script1.js', true, false, null, '1.0.0');
$assets->addJs('js/script2.js', true, false, null, '2.0.0');
$assets->addJs('js/script3.js', true, false, null);

$expected = sprintf(
"%s\n%s\n%s\n",
'<script type="text/javascript" src="/js/script1.js?ver=1.0.0"></script>',
'<script type="text/javascript" src="/js/script2.js?ver=2.0.0"></script>',
'<script type="text/javascript" src="/js/script3.js"></script>'
);

$assets->useImplicitOutput(false);

expect($assets->outputJs())->equals($expected);
}
);
}

/**
* Tests assets auto versioning
*
* @author Wojciech Ślawski <jurigag@gmail.com>
* @since 2017-02-01
*/
public function testAssetsAutoVersioningCollection()
{
$this->specify(
"Assets auto versioning with collection doesn't work correctly",
function () {
$assets = new Manager();

$assets->collection('js')
->addJs('js/script1.js', true, false, null, '1.0.0')
->addJs('js/script2.js', true, false, null)
->addJs('js/script3.js', true, false, null, false)
->setVersion(true);

$modificationTime = filemtime('js/script2.js');

$expected = sprintf(
"%s\n%s\n%s\n",
'<script type="text/javascript" src="/js/script1.js?ver=1.0.0"></script>',
"<script type=\"text/javascript\" src=\"/js/script2.js?ver=$modificationTime\"></script>",
'<script type="text/javascript" src="/js/script3.js"></script>'
);

$assets->useImplicitOutput(false);

expect($assets->outputJs())->equals($expected);
}
);
}
}
41 changes: 41 additions & 0 deletions tests/unit/Assets/ResourceTest.php
Expand Up @@ -82,4 +82,45 @@ function () {
}
);
}

/**
* Tests resource versioning
*
* @author Wojciech Ślawski <jurigag@gmail.com>
* @since 2017-02-01
*/
public function testAssetsVersioning()
{
$this->specify(
"The resource versioning is not correct",
function () {
$resource = new Resource('js', 'js/jquery.js', true, false, null, '1.0.0');
$actual = $resource->getRealTargetUri();
$expected = 'js/jquery.js?ver=1.0.0';

expect($actual)->equals($expected);
}
);
}

/**
* Tests resource auto versioning
*
* @author Wojciech Ślawski <jurigag@gmail.com>
* @since 2017-02-01
*/
public function testAssetsAutomaticVersioning()
{
$this->specify(
"The resource auto versioning is not correct",
function () {
$resource = new Resource('js', 'js/jquery.js', true, false, null, true);
$actual = $resource->getRealTargetUri();
$modificationTime = filemtime('js/jquery.js');
$expected = 'js/jquery.js?ver='.$modificationTime;

expect($actual)->equals($expected);
}
);
}
}

0 comments on commit 356cfc7

Please sign in to comment.