You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.6 KiB
PHP
168 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Artmark\Assets;
|
|
|
|
/**
|
|
* Description of AssetManager
|
|
*
|
|
* @author Andrey Pokidov <pokidov@e-traffic.ru>
|
|
*/
|
|
class AssetManager
|
|
{
|
|
/**
|
|
* @var AssetManager
|
|
*/
|
|
private static $manager = null;
|
|
|
|
/**
|
|
*
|
|
* @var Asset[]
|
|
*/
|
|
private $assets = [];
|
|
|
|
private function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return AssetManager
|
|
*/
|
|
public static function instance()
|
|
{
|
|
if (is_null(self::$manager)) {
|
|
self::$manager = new AssetManager();
|
|
}
|
|
|
|
return self::$manager;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function resetAssets()
|
|
{
|
|
$this->assets = [];
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $name
|
|
* @return $this
|
|
*/
|
|
public function loadCollection($name)
|
|
{
|
|
$assets = config('assets.' . $name);
|
|
|
|
if (!is_array($assets) || count($assets) == 0) {
|
|
return $this;
|
|
}
|
|
|
|
foreach ($assets as $name => $assetInfo) {
|
|
$this->loadAsset($name, $assetInfo);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function loadAsset($name, $assetInfo)
|
|
{
|
|
if (!is_array($assetInfo) || !array_key_exists('type', $assetInfo) || !array_key_exists('path', $assetInfo)) {
|
|
return;
|
|
}
|
|
|
|
$version = array_key_exists('version', $assetInfo) ? $assetInfo['version'] : '';
|
|
$position = array_key_exists('position', $assetInfo) ? $assetInfo['position'] : Asset::POSITION_HEAD;
|
|
|
|
$this->pushAsset(new Asset($name, $assetInfo['path'], $version, $assetInfo['type'], $position));
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $name
|
|
* @return boolean
|
|
*/
|
|
public function has($name)
|
|
{
|
|
return array_key_exists(Asset::correctName($name), $this->assets);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $name
|
|
* @return Asset
|
|
*/
|
|
public function get($name)
|
|
{
|
|
$correctedName = Asset::correctName($name);
|
|
return array_key_exists($correctedName, $this->assets) ? $this->assets[$correctedName] : null;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param Asset $asset
|
|
* @return $this
|
|
*/
|
|
public function pushAsset(Asset $asset)
|
|
{
|
|
$this->assets[$asset->name()] = $asset;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $name
|
|
* @param string $url
|
|
* @param string|int $version
|
|
* @param string $position
|
|
* @return $this
|
|
*/
|
|
public function setStyleSheet($name, $url, $version = '', $position = Asset::POSITION_HEAD)
|
|
{
|
|
$asset = new Asset($name, $url, $version, Asset::TYPE_CSS, $position);
|
|
$this->pushAsset($asset);
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $name
|
|
* @param string $url
|
|
* @param string|int $version
|
|
* @param string $position
|
|
* @return $this
|
|
*/
|
|
public function setJavaScript($name, $url, $version = '', $position = Asset::POSITION_HEAD)
|
|
{
|
|
$asset = new Asset($name, $url, $version, Asset::TYPE_JAVASCRIPT, $position);
|
|
$this->pushAsset($asset);
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $position
|
|
* @param string $type
|
|
* @return Asset[]
|
|
*/
|
|
public function getAssetsFor($position, $type)
|
|
{
|
|
if (!is_string($position) || !is_string($type)) {
|
|
return [];
|
|
}
|
|
|
|
$assets = [];
|
|
|
|
foreach ($this->assets as $asset) {
|
|
if ($asset->type() == $type && $asset->position() == $position) {
|
|
$assets[] = $asset;
|
|
}
|
|
}
|
|
|
|
return $assets;
|
|
}
|
|
}
|