1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
/**
* Render Assets pipeline processor queue.
*
* @package Silla.IO
* @subpackage Core\Modules\Render
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Modules\Render;
use Core;
/**
* Class Assets.
*/
class Assets
{
/**
* Assets queue contents.
*
* @var array
* @access protected
*/
private $assets = array('styles' => array(), 'scripts' => array());
/**
* Adds an asset to the queue.
*
* @param string|array $assets Assets string paths.
*
* @uses addAsset()
*
* @return void
*/
public function add($assets)
{
if (is_array($assets)) {
foreach ($assets as $asset) {
$this->addAsset($asset);
}
} else {
$this->addAsset($assets);
}
}
/**
* Removes an asset from the queue.
*
* @param string|array $assets Assets string paths.
*
* @uses removeAsset()
*
* @return void
*/
public function remove($assets)
{
if (is_array($assets)) {
foreach ($assets as $asset) {
$this->removeAsset($asset);
}
} else {
$this->removeAsset($assets);
}
}
/**
* Verifies whether there is at least one asset in the queue.
*
* @return boolean
*/
public function any()
{
return !empty($this->assets['styles']) || !empty($this->assets['scripts']);
}
/**
* Get All assets.
*
* @return array
*/
public function all()
{
return $this->assets;
}
/**
* Adds an asset to the assets processor queue.
*
* Guarantees that the assets queue will be consistent of unique values.
*
* @param string $file Asset file to be added.
*
* @throws \InvalidArgumentException Asset should be a string representation of the path to the file.
* @access public
* @see pathinfo()
*
* @return void
*/
private function addAsset($file)
{
if (!is_string($file)) {
throw new \InvalidArgumentException('Asset should be a string representation of the path to the file');
}
$file_extension = pathinfo($file, PATHINFO_EXTENSION);
switch ($file_extension) {
case 'css':
case 'less':
case 'scss':
$type = 'styles';
break;
case 'js':
case 'coffee':
$type = 'scripts';
break;
default:
$type = 'misc';
break;
}
$this->assets[$type][$file_extension][] = $file;
$this->assets[$type][$file_extension] = array_unique($this->assets[$type][$file_extension]);
}
/**
* Removes an asset from the assets processor queue.
*
* @param string $file File to be removed.
*
* @throws \InvalidArgumentException Asset should be a string representation of the path to the file.
* @access public
*
* @return void
*/
private function removeAsset($file)
{
if (!is_string($file)) {
throw new \InvalidArgumentException('Asset should be a string representation of the path to the file');
}
foreach ($this->assets as $type => $assets) {
foreach ($assets as $ext => $asset) {
$asset_to_remove = array_search($file, $this->assets[$type][$ext], true);
if ($asset_to_remove && isset($this->assets[$type][$ext][$asset_to_remove])) {
unset($this->assets[$type][$ext][$asset_to_remove]);
}
}
}
}
}