| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assetic package, an OpenSky project. |
| 5 |
* |
| 6 |
* (c) 2010-2014 OpenSky Project Inc |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Assetic; |
| 13 |
|
| 14 |
use Assetic\Asset\AssetInterface; |
| 15 |
|
| 16 |
/** |
| 17 |
* Manages assets. |
| 18 |
* |
| 19 |
* @author Kris Wallsmith <[email protected]> |
| 20 |
*/ |
| 21 |
class AssetManager |
| 22 |
{ |
| 23 |
private $assets = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Gets an asset by name. |
| 27 |
* |
| 28 |
* @param string $name The asset name |
| 29 |
* |
| 30 |
* @return AssetInterface The asset |
| 31 |
* |
| 32 |
* @throws \InvalidArgumentException If there is no asset by that name |
| 33 |
*/ |
| 34 |
public function get($name) |
| 35 |
{ |
| 36 |
if (!isset($this->assets[$name])) { |
| 37 |
throw new \InvalidArgumentException(sprintf('There is no "%s" asset.', $name)); |
| 38 |
} |
| 39 |
|
| 40 |
return $this->assets[$name]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks if the current asset manager has a certain asset. |
| 45 |
* |
| 46 |
* @param string $name an asset name |
| 47 |
* |
| 48 |
* @return Boolean True if the asset has been set, false if not |
| 49 |
*/ |
| 50 |
public function has($name) |
| 51 |
{ |
| 52 |
return isset($this->assets[$name]); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Registers an asset to the current asset manager. |
| 57 |
* |
| 58 |
* @param string $name The asset name |
| 59 |
* @param AssetInterface $asset The asset |
| 60 |
* |
| 61 |
* @throws \InvalidArgumentException If the asset name is invalid |
| 62 |
*/ |
| 63 |
public function set($name, AssetInterface $asset) |
| 64 |
{ |
| 65 |
if (!ctype_alnum(str_replace('_', '', $name))) { |
| 66 |
throw new \InvalidArgumentException(sprintf('The name "%s" is invalid.', $name)); |
| 67 |
} |
| 68 |
|
| 69 |
$this->assets[$name] = $asset; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns an array of asset names. |
| 74 |
* |
| 75 |
* @return array An array of asset names |
| 76 |
*/ |
| 77 |
public function getNames() |
| 78 |
{ |
| 79 |
return array_keys($this->assets); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Clears all assets. |
| 84 |
*/ |
| 85 |
public function clear() |
| 86 |
{ |
| 87 |
$this->assets = array(); |
| 88 |
} |
| 89 |
} |
| 90 |
|