asset.php 8.28 KB
Newer Older
1
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
2 3 4 5

class Asset {

	/**
6 7
	 * All of the instantiated asset containers.
	 *
8 9
	 * @var array
	 */
10
	public static $containers = array();
11 12 13 14

	/**
	 * Get an asset container instance.
	 *
15 16 17 18 19 20 21 22
	 * <code>
	 *		// Get the default asset container
	 *		$container = Asset::container();
	 *
	 *		// Get a named asset container
	 *		$container = Asset::container('footer');
	 * </code>
	 *
23 24
	 * @param  string            $container
	 * @return Asset_Container
25
	 */
26
	public static function container($container = 'default')
27
	{
28
		if ( ! isset(static::$containers[$container]))
29
		{
30
			static::$containers[$container] = new Asset_Container($container);
31 32
		}

33
		return static::$containers[$container];
34 35 36
	}

	/**
37
	 * Magic Method for calling methods on the default container.
38 39 40 41 42 43 44 45
	 *
	 * <code>
	 *		// Call the "styles" method on the default container
	 *		echo Asset::styles();
	 *
	 *		// Call the "add" method on the default container
	 *		Asset::add('jquery', 'js/jquery.js');
	 * </code>
46
	 */
47
	public static function __callStatic($method, $parameters)
48
	{
49
		return call_user_func_array(array(static::container(), $method), $parameters);
50 51
	}

52 53 54 55 56 57 58 59 60 61 62 63
}

class Asset_Container {

	/**
	 * The asset container name.
	 *
	 * @var string
	 */
	public $name;

	/**
64 65 66 67 68 69 70
	 * The bundle that the assets belong to.
	 *
	 * @var string
	 */
	public $bundle = DEFAULT_BUNDLE;

	/**
71 72 73 74 75 76 77 78 79 80
	 * All of the registered assets.
	 *
	 * @var array
	 */
	public $assets = array();

	/**
	 * Create a new asset container instance.
	 *
	 * @param  string  $name
Taylor Otwell committed
81
	 * @param  HTML    $html
82 83
	 * @return void
	 */
84
	public function __construct($name)
85 86 87 88 89 90 91
	{
		$this->name = $name;
	}

	/**
	 * Add an asset to the container.
	 *
92
	 * The extension of the asset source will be used to determine the type of
93 94
	 * asset being registered (CSS or JavaScript). When using a non-standard
	 * extension, the style/script methods may be used to register assets.
95
	 *
96 97 98 99 100 101 102 103 104 105 106
	 * <code>
	 *		// Add an asset to the container
	 *		Asset::container()->add('jquery', 'js/jquery.js');
	 *
	 *		// Add an asset that has dependencies on other assets
	 *		Asset::add('jquery', 'js/jquery.js', 'jquery-ui');
	 *
	 *		// Add an asset that should have attributes applied to its tags
	 *		Asset::add('jquery', 'js/jquery.js', null, array('defer'));
	 * </code>
	 *
107 108 109 110 111 112 113 114
	 * @param  string  $name
	 * @param  string  $source
	 * @param  array   $dependencies
	 * @param  array   $attributes
	 * @return void
	 */
	public function add($name, $source, $dependencies = array(), $attributes = array())
	{
115
		$type = (pathinfo($source, PATHINFO_EXTENSION) == 'css') ? 'style' : 'script';
116

117
		return $this->$type($name, $source, $dependencies, $attributes);
118 119 120
	}

	/**
121
	 * Add a CSS file to the registered assets.
122
	 *
123 124 125 126 127
	 * @param  string           $name
	 * @param  string           $source
	 * @param  array            $dependencies
	 * @param  array            $attributes
	 * @return Asset_Container
128 129 130 131 132 133 134 135 136
	 */
	public function style($name, $source, $dependencies = array(), $attributes = array())
	{
		if ( ! array_key_exists('media', $attributes))
		{
			$attributes['media'] = 'all';
		}

		$this->register('style', $name, $source, $dependencies, $attributes);
137 138

		return $this;
139 140 141
	}

	/**
142
	 * Add a JavaScript file to the registered assets.
143
	 *
144 145 146 147 148
	 * @param  string           $name
	 * @param  string           $source
	 * @param  array            $dependencies
	 * @param  array            $attributes
	 * @return Asset_Container
149 150 151 152
	 */
	public function script($name, $source, $dependencies = array(), $attributes = array())
	{
		$this->register('script', $name, $source, $dependencies, $attributes);
153 154

		return $this;
155 156 157
	}

	/**
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	 * Returns the full-path for an asset.
	 *
	 * @param  string  $source
	 * @return string
	 */
	public function path($source)
	{
		return Bundle::assets($this->bundle).$source;
	}

	/**
	 * Set the bundle that the container's assets belong to.
	 *
	 * @param  string           $bundle
	 * @return Asset_Container
	 */
	public function bundle($bundle)
	{
		$this->bundle = $bundle;
		return $this;
	}

	/**
Taylor Otwell committed
181 182
	 * Add an asset to the array of registered assets.
	 *
183 184 185 186 187 188 189
	 * @param  string  $type
	 * @param  string  $name
	 * @param  string  $source
	 * @param  array   $dependencies
	 * @param  array   $attributes
	 * @return void
	 */
Taylor Otwell committed
190
	protected function register($type, $name, $source, $dependencies, $attributes)
191 192 193
	{
		$dependencies = (array) $dependencies;

194 195
		$attributes = (array) $attributes;

196 197 198 199
		$this->assets[$type][$name] = compact('source', 'dependencies', 'attributes');
	}

	/**
200
	 * Get the links to all of the registered CSS assets.
201 202 203 204 205
	 *
	 * @return  string
	 */
	public function styles()
	{
206
		return $this->group('style');
207 208 209
	}

	/**
210
	 * Get the links to all of the registered JavaScript assets.
211 212 213 214 215
	 *
	 * @return  string
	 */
	public function scripts()
	{
216
		return $this->group('script');
217 218 219
	}

	/**
Taylor Otwell committed
220
	 * Get all of the registered assets for a given type / group.
221 222 223 224
	 *
	 * @param  string  $group
	 * @return string
	 */
225
	protected function group($group)
226 227 228 229 230 231 232
	{
		if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return '';

		$assets = '';

		foreach ($this->arrange($this->assets[$group]) as $name => $data)
		{
233
			$assets .= $this->asset($group, $name);
234 235 236 237 238 239
		}
		
		return $assets;
	}

	/**
Taylor Otwell committed
240
	 * Get the HTML link to a registered asset.
241 242 243 244 245
	 *
	 * @param  string  $group
	 * @param  string  $name
	 * @return string
	 */
246
	protected function asset($group, $name)
247
	{
Taylor Otwell committed
248
		if ( ! isset($this->assets[$group][$name])) return '';
249 250 251

		$asset = $this->assets[$group][$name];

252 253 254 255 256
		// If the bundle source is not a complete URL, we will go ahead and prepend
		// the bundle's asset path to the source provided with the asset. This will
		// ensure that we attach the correct path to the asset.
		if (filter_var($asset['source'], FILTER_VALIDATE_URL) === false)
		{
257
			$asset['source'] = $this->path($asset['source']);
258 259
		}

260
		return HTML::$group($asset['source'], $asset['attributes']);
261 262 263 264 265 266 267 268
	}

	/**
	 * Sort and retrieve assets based on their dependencies
	 *
	 * @param   array  $assets
	 * @return  array
	 */
Taylor Otwell committed
269
	protected function arrange($assets)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	{
		list($original, $sorted) = array($assets, array());

		while (count($assets) > 0)
		{
			foreach ($assets as $asset => $value)
			{
				$this->evaluate_asset($asset, $value, $original, $sorted, $assets);
			}
		}
		
		return $sorted;
	}

	/**
	 * Evaluate an asset and its dependencies.
	 *
	 * @param  string  $asset
	 * @param  string  $value
	 * @param  array   $original
	 * @param  array   $sorted
	 * @param  array   $assets
	 * @return void
	 */
Taylor Otwell committed
294
	protected function evaluate_asset($asset, $value, $original, &$sorted, &$assets)
295 296 297
	{
		// If the asset has no more dependencies, we can add it to the sorted list
		// and remove it from the array of assets. Otherwise, we will not verify
298
		// the asset's dependencies and determine if they've been sorted.
299 300 301
		if (count($assets[$asset]['dependencies']) == 0)
		{
			$sorted[$asset] = $value;
302

303 304 305 306 307 308 309 310 311
			unset($assets[$asset]);
		}
		else
		{
			foreach ($assets[$asset]['dependencies'] as $key => $dependency)
			{
				if ( ! $this->dependency_is_valid($asset, $dependency, $original, $assets))
				{
					unset($assets[$asset]['dependencies'][$key]);
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326
					continue;
				}

				// If the dependency has not yet been added to the sorted list, we can not
				// remove it from this asset's array of dependencies. We'll try again on
				// the next trip through the loop.
				if ( ! isset($sorted[$dependency])) continue;

				unset($assets[$asset]['dependencies'][$key]);
			}
		}		
	}

	/**
Taylor Otwell committed
327 328 329
	 * Verify that an asset's dependency is valid.
	 *
	 * A dependency is considered valid if it exists, is not a circular reference, and is
330 331
	 * not a reference to the owning asset itself. If the dependency doesn't exist, no
	 * error or warning will be given. For the other cases, an exception is thrown.
332 333 334 335 336 337 338
	 *
	 * @param  string  $asset
	 * @param  string  $dependency
	 * @param  array   $original
	 * @param  array   $assets
	 * @return bool
	 */
Taylor Otwell committed
339
	protected function dependency_is_valid($asset, $dependency, $original, $assets)
340
	{
341
		if ( ! isset($original[$dependency]))
342
		{
343 344 345 346 347
			return false;
		}
		elseif ($dependency === $asset)
		{
			throw new \Exception("Asset [$asset] is dependent on itself.");
348 349 350
		}
		elseif (isset($assets[$dependency]) and in_array($asset, $assets[$dependency]['dependencies']))
		{
351
			throw new \Exception("Assets [$asset] and [$dependency] have a circular dependency.");
352
		}
353 354

		return true;
355 356
	}

Phill Sparks committed
357
}