Simplify plugins' assets attachment

This commit is contained in:
Dmitriy Simushev 2014-10-14 10:56:20 +00:00
parent 1a82e11b5e
commit 397786991d

View File

@ -41,24 +41,15 @@ class AssetManager implements AssetManagerInterface
/** /**
* List of attached JS assets. * List of attached JS assets.
* *
* @var array * @var Package
*/ */
protected $jsAssets = null; protected $jsPackage = null;
/** /**
* List of attached CSS assets. * List of attached CSS assets.
* *
* @var array * @var Package
*/ */
protected $cssAssets = null; protected $cssPackage = null;
/**
* Class constructor.
*/
public function __construct()
{
$this->jsAssets = new Package();
$this->cssAssets = new Package();
}
/** /**
* Sets a request which will be used as a context. * Sets a request which will be used as a context.
@ -75,8 +66,8 @@ class AssetManager implements AssetManagerInterface
// The request has been changed thus all attaches assets are outdated // The request has been changed thus all attaches assets are outdated
// now. Clear them all. // now. Clear them all.
$this->jsAssets = new Package(); $this->jsAssets = null;
$this->cssAssets = new Package(); $this->cssAssets = null;
} }
/** /**
@ -104,7 +95,7 @@ class AssetManager implements AssetManagerInterface
*/ */
public function attachJs($content, $type = AssetManagerInterface::RELATIVE_URL, $weight = 0) public function attachJs($content, $type = AssetManagerInterface::RELATIVE_URL, $weight = 0)
{ {
$this->jsAssets->addAsset($content, $type, $weight); $this->getJsPackage()->addAsset($content, $type, $weight);
} }
/** /**
@ -112,16 +103,7 @@ class AssetManager implements AssetManagerInterface
*/ */
public function getJsAssets() public function getJsAssets()
{ {
// If plugins assets are stored in $this->jsAssets several calls to the return $this->getJsPackage()->getAssets();
// method will duplicate The temporary package is used to avoid such
// behaviour.
$combined_assets = clone $this->jsAssets;
$combined_assets->merge($this->triggerJsEvent());
$assets = $combined_assets->getAssets();
unset($combined_assets);
return $assets;
} }
/** /**
@ -129,7 +111,7 @@ class AssetManager implements AssetManagerInterface
*/ */
public function attachCss($content, $type = AssetManagerInterface::RELATIVE_URL, $weight = 0) public function attachCss($content, $type = AssetManagerInterface::RELATIVE_URL, $weight = 0)
{ {
$this->cssAssets->addAsset($content, $type, $weight); $this->getCssPackage()->addAsset($content, $type, $weight);
} }
/** /**
@ -137,16 +119,7 @@ class AssetManager implements AssetManagerInterface
*/ */
public function getCssAssets() public function getCssAssets()
{ {
// If plugins assets are stored in $this->cssAssets several calls to the return $this->getCssPackage()->getAssets();
// method will duplicate The temporary package is used to avoid such
// behaviour.
$combined_assets = clone $this->cssAssets;
$combined_assets->merge($this->triggerCssEvent());
$assets = $combined_assets->getAssets();
unset($combined_assets);
return $assets;
} }
/** /**
@ -165,6 +138,34 @@ class AssetManager implements AssetManagerInterface
return $this->request; return $this->request;
} }
/**
* Returns a package with JavaScript assets.
*
* @return Package
*/
protected function getJsPackage()
{
if (is_null($this->jsPackage)) {
$this->jsPackage = $this->triggerJsEvent();
}
return $this->jsPackage;
}
/**
* Returns a package with CSS assets.
*
* @return Package
*/
protected function getCssPackage()
{
if (is_null($this->cssPackage)) {
$this->cssPackage = $this->triggerCssEvent();
}
return $this->cssPackage;
}
/** /**
* Gets additional JS assets by triggering some events. * Gets additional JS assets by triggering some events.
* *