Skip to content

Commit

Permalink
Fix: assets manager hard reference to phalcon\tag. Use DI (#13894)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmst authored and niden committed Mar 12, 2019
1 parent fb9d812 commit 832c5a0
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The implementation offers PSR-7/PSR-17 compatible components in a different name
[#13889](https://github.com/phalcon/cphalcon/pull/13889)
- Added `Phalcon\Collection`, an object implementing `ArrayAccess`, `Countable`, `IteratorAggregate`, `JsonSerializable`, `Serializable`, offering an easy way to handle collections of data such as arrays, superglobals etc. [#13886](https://github.com/phalcon/cphalcon/issues/13886)

## Fixed
- Fixed Assets Manager hard reference to \Phalcon\Tag, should use DI [#12261](https://github.com/phalcon/cphalcon/issues/12261)


# [4.0.0-alpha.3](https://github.com/phalcon/cphalcon/releases/tag/v4.0.0-alpha.3) (2019-02-31)
## Added
Expand Down
48 changes: 42 additions & 6 deletions phalcon/assets/manager.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ use Phalcon\Assets\Asset\Js as AssetJs;
use Phalcon\Assets\Asset\Css as AssetCss;
use Phalcon\Assets\Inline\Css as InlineCss;
use Phalcon\Assets\Inline\Js as InlineJs;
use Phalcon\DiInterface;
use Phalcon\Di\InjectionAwareInterface;

/**
* Phalcon\Assets\Manager
*
* Manages collections of CSS/Javascript assets
*/
class Manager
class Manager implements InjectionAwareInterface
{

protected _dependencyInjector;

/**
* Options configure
* @var array
Expand All @@ -36,7 +40,7 @@ class Manager
protected collections;

/**
* @var bool
* @var bool
*/
protected implicitOutput = true;

Expand All @@ -48,6 +52,22 @@ class Manager
let this->options = options;
}

/**
* Sets the dependency injector
*/
public function setDI(<DiInterface> dependencyInjector)
{
let this->_dependencyInjector = dependencyInjector;
}

/**
* Returns the internal dependency injector
*/
public function getDI() -> <DiInterface>
{
return this->_dependencyInjector;
}

/**
* Sets the manager options
*/
Expand Down Expand Up @@ -763,15 +783,23 @@ class Manager
*/
public function outputCss(string collectionName = null) -> string
{
var collection;
var collection, dependencyInjector, tag, callback;

if !collectionName {
let collection = this->getCss();
} else {
let collection = this->get(collectionName);
}

return this->output(collection, ["Phalcon\\Tag", "stylesheetLink"], "css");
let callback = ["Phalcon\\Tag", "stylesheetLink"];

let dependencyInjector = this->_dependencyInjector;
if typeof dependencyInjector == "object" && dependencyInjector->has("tag") {
let tag = dependencyInjector->getShared("tag");
let callback = [tag, "stylesheetLink"];
}

return this->output(collection, callback, "css");
}

/**
Expand All @@ -795,15 +823,23 @@ class Manager
*/
public function outputJs(string collectionName = null) -> string
{
var collection;
var collection, dependencyInjector, tag, callback;

if !collectionName {
let collection = this->getJs();
} else {
let collection = this->get(collectionName);
}

return this->output(collection, ["Phalcon\\Tag", "javascriptInclude"], "js");
let callback = ["Phalcon\\Tag", "javascriptInclude"];

let dependencyInjector = this->_dependencyInjector;
if typeof dependencyInjector == "object" && dependencyInjector->has("tag") {
let tag = dependencyInjector->getShared("tag");
let callback = [tag, "javascriptInclude"];
}

return this->output(collection, callback, "js");
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/_data/fixtures/Assets/CustomTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Fixtures\Assets;

class CustomTag extends \Phalcon\Tag
{
public static function stylesheetLink($parameters = null, bool $local = true): string
{
return sprintf("<link href=\"%s\">\n", $parameters[0]);
}

public static function javascriptInclude($parameters = null, bool $local = true): string
{
return sprintf("<script src=\"%s\" type=\"application/javascript\"></script>\n", $parameters[0]);
}
}
29 changes: 29 additions & 0 deletions tests/unit/Assets/Manager/OutputCssCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Phalcon\Test\Fixtures\Assets\TrimFilter;
use Phalcon\Test\Fixtures\Assets\UppercaseFilter;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Fixtures\Assets\CustomTag;
use UnitTester;

/**
Expand Down Expand Up @@ -131,4 +132,32 @@ public function assetsManagerOutputCssFilterChainCustomFilterWithCssmin(UnitTest

$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Assets\Manager :: outputCss() - custom tag component
*
* @param UnitTester $I
*/
public function assetsManagerOutputCssCustomTag(UnitTester $I)
{
$I->wantToTest('Assets\Manager - outputCss() - custom tag component');

$di = $this->getDi();
$di->setShared('tag', CustomTag::class);

$assets = new Manager();
$assets->setDI($di);

$assets->addCss('css/style1.css');
$assets->addCss('/css/style2.css');
$assets->addAsset(new Css('/css/style.css'));

$expected = '<link href="css/style1.css">' . PHP_EOL
. '<link href="/css/style2.css">' . PHP_EOL
. '<link href="/css/style.css">' . PHP_EOL;

$assets->useImplicitOutput(false);
$actual = $assets->outputCss();
$I->assertEquals($expected, $actual);
}
}
51 changes: 51 additions & 0 deletions tests/unit/Assets/Manager/OutputJsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,34 @@
use Phalcon\Assets\Asset\Js;
use Phalcon\Assets\Manager;
use UnitTester;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Fixtures\Assets\CustomTag;

/**
* Class OutputJsCest
*/
class OutputJsCest
{
use DiTrait;

/**
* @param UnitTester $I
*/
public function _after(UnitTester $I)
{
$this->resetDi();
}

/**
* @param UnitTester $I
*/
public function _before(UnitTester $I)
{
$this->newDi();
$this->setDiEscaper();
$this->setDiUrl();
}

/**
* Tests Phalcon\Assets\Manager :: outputJs() - implicit
*
Expand Down Expand Up @@ -73,4 +95,33 @@ public function assetsManagerOutputJsNotImplicit(UnitTester $I)
$actual = ob_get_clean();
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Assets\Manager :: outputJs - custom tag component
*
* @param UnitTester $I
*/
public function assetsManagerOutputJsCustomTag(UnitTester $I)
{
$I->wantToTest('Asset/Manager - outputJs() - custom tag component');

$di = $this->getDi();
$di->setShared('tag', CustomTag::class);

$assets = new Manager();
$assets->setDI($di);

$assets->addJs('js/script1.js');
$assets->addJs('/js/script2.js');
$assets->addAsset(new Js('/js/script3.js'));

$expected = '<script src="js/script1.js" type="application/javascript"></script>' . PHP_EOL
. '<script src="/js/script2.js" type="application/javascript"></script>' . PHP_EOL
. '<script src="/js/script3.js" type="application/javascript"></script>' . PHP_EOL;

ob_start();
$assets->outputJs();
$actual = ob_get_clean();
$I->assertEquals($expected, $actual);
}
}

0 comments on commit 832c5a0

Please sign in to comment.