Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added assets versioning #12591

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Added
- Added `Phalcon\Db\Adapter\Pdo\Postgresql::describeReferences` to implement custom Postgresql rules
- Added assets versioning `Phalcon\Assets\Manager:addCss`, `Phalcon\Assets\Manager::addJs`, `Phalcon\Assets\Collection:addCss`, `Phalcon\Assets\Collection:addJs` accepts two additional parameters - `version` and `autoVersion`

## Changed
- The `Phalcon\Mvc\Application`, `Phalcon\Mvc\Micro` and `Phalcon\Mvc\Router` now must have a URI to process [#12380](https://github.com/phalcon/cphalcon/pull/12380)
Expand Down
1,486 changes: 1,474 additions & 12 deletions CHANGELOG.md

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions phalcon/assets/collection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ class Collection implements \Countable, \Iterator

protected _sourcePath { get };

/**
* Version of resource
* @var string
*/
protected _version { get, set };

/**
* Should version be determined from file modification time
* @var bool
*/
protected _autoVersion = false { set };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to not introduce anymore _prefixed variables. Lets use autoVersion instead of _autoVersion.


protected _includedResources;

/**
Expand Down Expand Up @@ -115,7 +127,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, string version = null, boolean autoVersion = false) -> <Collection>
{
var collectionLocal, collectionAttributes;

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

this->add(new ResourceCss(path, collectionLocal, filter, collectionAttributes));
this->add(new ResourceCss(path, collectionLocal, filter, collectionAttributes, version, autoVersion));

return this;
}
Expand All @@ -158,7 +170,7 @@ class Collection implements \Countable, \Iterator
*
* @param array attributes
*/
public function addJs(string! path, boolean local = null, boolean filter = true, attributes = null) -> <Collection>
public function addJs(string! path, var local = null, boolean filter = true, attributes = null, string version = null, boolean autoVersion = false) -> <Collection>
{
var collectionLocal, collectionAttributes;

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

this->add(new ResourceJs(path, collectionLocal, filter, collectionAttributes));
this->add(new ResourceJs(path, collectionLocal, filter, collectionAttributes, version, autoVersion));

return this;
}
Expand Down Expand Up @@ -359,6 +371,14 @@ class Collection implements \Countable, \Iterator
return this;
}

/**
* Checks if collection is using auto version
*/
public function isAutoVersion() -> boolean
{
return this->_autoVersion;
}

/**
* Adds a resource or inline-code to the collection
*/
Expand Down
50 changes: 45 additions & 5 deletions phalcon/assets/manager.zep
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,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, string version = null, boolean autoVersion = false) -> <Manager>
{
this->addResourceByType("css", new ResourceCss(path, local, filter, attributes));
this->addResourceByType("css", new ResourceCss(path, local, filter, attributes, version, autoVersion));
return this;
}

Expand All @@ -112,9 +112,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, string version = null, boolean autoVersion = false) -> <Manager>
{
this->addResourceByType("js", new ResourceJs(path, local, filter, attributes));
this->addResourceByType("js", new ResourceJs(path, local, filter, attributes, version, autoVersion));
return this;
}

Expand Down Expand Up @@ -308,7 +308,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, autoVersion, modificationTime;

let useImplicitOutput = this->_implicitOutput;

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

if $resource->getVersion() === null && $resource->isAutoVersion() === null {
let version = collection->getVersion();
let autoVersion = collection->isAutoVersion();

if autoVersion && local {
let modificationTime = filemtime($resource->getRealSourcePath());
let version = version ? version . "." . modificationTime : modificationTime;
}

if version {
let prefixedPath = prefixedPath . "?ver=" . version;
}
}

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

if $resource->getVersion() === null && $resource->isAutoVersion() === null {
let version = collection->getVersion();
let autoVersion = collection->isAutoVersion();

if autoVersion {
let modificationTime = filemtime($resource->getRealSourcePath());
let version = version ? version . "." . modificationTime : modificationTime;
}

if version {
let prefixedPath = prefixedPath . "?ver=" . version;
}
}

/**
* Prepare the parameters for the callback
*/
Expand Down Expand Up @@ -663,6 +691,18 @@ class Manager
let prefixedPath = targetUri;
}

let version = collection->getVersion();
let autoVersion = collection->isAutoVersion();

if autoVersion {
let modificationTime = filemtime(completeTargetPath);
let version = version ? version . "." . modificationTime : modificationTime;
}

if version {
let prefixedPath = prefixedPath . "?ver=" . version;
}

/**
* Gets extra HTML attributes in the collection
*/
Expand Down
32 changes: 29 additions & 3 deletions phalcon/assets/resource.zep
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ class $Resource implements ResourceInterface

protected _targetUri { get };

protected _version { set, get };

protected _autoVersion = false { set };

/**
* Phalcon\Assets\Resource constructor
*/
public function __construct(string type, string path, boolean local = true, boolean filter = true, array attributes = [])
public function __construct(string type, string path, boolean local = true, boolean filter = true, attributes = null, var version = null, var autoVersion = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jurigag Do we really need to change attributes type? The Phalcon v4 will have a lot of BC incompatible changes. Could you please try to sort out how to avoid yet another

{
let this->_type = type,
this->_path = path,
this->_local = local,
this->_filter = filter,
this->_attributes = attributes;
this->_attributes = attributes,
this->_version = version,
this->_autoVersion = autoVersion;
}

/**
Expand Down Expand Up @@ -192,12 +198,24 @@ class $Resource implements ResourceInterface
*/
public function getRealTargetUri() -> string
{
var targetUri;
var targetUri, version, modificationTime;

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

let version = this->_version;

if this->_autoVersion && this->_local {
let modificationTime = filemtime(this->getRealSourcePath());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the filemtime is I/O function, which will be called for each request. right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: The results of this function are cached. See clearstatcache() for more details.
https://secure.php.net/filemtime#refsect1-function.filemtime-notes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right.

let version = version ? version . "." . modificationTime : modificationTime;
}

if version {
let targetUri = targetUri . "?ver=" . version;
}

return targetUri;
}

Expand Down Expand Up @@ -255,6 +273,14 @@ class $Resource implements ResourceInterface
return targetPath;
}

/**
* Checks if resource is using auto version
*/
public function isAutoVersion() -> boolean | null
{
return this->_autoVersion;
}

/**
* Gets the resource's key.
*/
Expand Down
4 changes: 2 additions & 2 deletions phalcon/assets/resource/css.zep
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Css extends ResourceBase
/**
* Phalcon\Assets\Resource\Css
*/
public function __construct(string! path, boolean local = true, boolean filter = true, array attributes = [])
public function __construct(string! path, boolean local = true, boolean filter = true, attributes = null, var version = null, var autoVersion = null)
{
parent::__construct("css", path, local, filter, attributes);
parent::__construct("css", path, local, filter, attributes, version, autoVersion);
}
}
4 changes: 2 additions & 2 deletions phalcon/assets/resource/js.zep
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Js extends ResourceBase
/**
* Phalcon\Assets\Resource\Js
*/
public function __construct(string! path, boolean local = true, boolean filter = true, array attributes = [])
public function __construct(path, local = true, filter = true, attributes = null, var version = null, var autoVersion = null)
{
parent::__construct("js", path, local, filter, attributes);
parent::__construct("js", path, local, filter, attributes, version, autoVersion);
}
}
3 changes: 3 additions & 0 deletions tests/_data/assets/script1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function popup() {
alert("Hello World");
}
3 changes: 3 additions & 0 deletions tests/_data/assets/script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function popup() {
alert("Hello World");
}
3 changes: 3 additions & 0 deletions tests/_data/assets/script3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function popup() {
alert("Hello World");
}
70 changes: 70 additions & 0 deletions tests/unit/Assets/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,74 @@ 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(PATH_DATA.'assets/script1.js', true, false, null, '1.0.0');
$assets->addJs(PATH_DATA.'assets/script2.js', true, false, null, '2.0.0');
$assets->addJs(PATH_DATA.'assets/script3.js', true, false, null);

$pathData = PATH_DATA;

$expected = sprintf(
"%s\n%s\n%s\n",
"<script type=\"text/javascript\" src=\"{$pathData}assets/script1.js?ver=1.0.0\"></script>",
"<script type=\"text/javascript\" src=\"{$pathData}assets/script2.js?ver=2.0.0\"></script>",
"<script type=\"text/javascript\" src=\"{$pathData}assets/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(PATH_DATA.'assets/script1.js', true, false, null, '1.0.0')
->addJs(PATH_DATA.'assets/script2.js', true, false, null)
->addJs(PATH_DATA.'assets/script3.js', true, false, null, null, false)
->setAutoVersion(true);

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

$pathData = PATH_DATA;

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

$assets->useImplicitOutput(false);

expect($assets->outputJs())->equals($expected);
}
);
}
}
41 changes: 41 additions & 0 deletions tests/unit/Assets/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,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', PATH_DATA.'assets/jquery.js', true, false, null, '1.0.0');
$actual = $resource->getRealTargetUri();
$expected = PATH_DATA.'assets/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', PATH_DATA.'assets/jquery.js', true, false, null, null, true);
$actual = $resource->getRealTargetUri();
$modificationTime = filemtime(PATH_DATA.'assets/jquery.js');
$expected = PATH_DATA.'assets/jquery.js?ver='.$modificationTime;

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