asset.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php namespace Laravel; isset($GLOBALS['APP_PATH']) or die('No direct script access.');
  2. class Asset {
  3. /**
  4. * All of the instantiated asset containers.
  5. *
  6. * @var array
  7. */
  8. public static $containers = array();
  9. /**
  10. * Get an asset container instance.
  11. *
  12. * <code>
  13. * // Get the default asset container
  14. * $container = Asset::container();
  15. *
  16. * // Get a named asset container
  17. * $container = Asset::container('footer');
  18. * </code>
  19. *
  20. * @param string $container
  21. * @return Asset_Container
  22. */
  23. public static function container($container = 'default')
  24. {
  25. if ( ! isset(static::$containers[$container]))
  26. {
  27. static::$containers[$container] = new Asset_Container($container);
  28. }
  29. return static::$containers[$container];
  30. }
  31. /**
  32. * Magic Method for calling methods on the default container.
  33. *
  34. * <code>
  35. * // Call the "styles" method on the default container
  36. * echo Asset::styles();
  37. *
  38. * // Call the "add" method on the default container
  39. * Asset::add('jquery', 'js/jquery.js');
  40. * </code>
  41. */
  42. public static function __callStatic($method, $parameters)
  43. {
  44. return call_user_func_array(array(static::container(), $method), $parameters);
  45. }
  46. }
  47. class Asset_Container {
  48. /**
  49. * The asset container name.
  50. *
  51. * @var string
  52. */
  53. public $name;
  54. /**
  55. * The bundle that the assets belong to.
  56. *
  57. * @var string
  58. */
  59. public $bundle = DEFAULT_BUNDLE;
  60. /**
  61. * All of the registered assets.
  62. *
  63. * @var array
  64. */
  65. public $assets = array();
  66. /**
  67. * Create a new asset container instance.
  68. *
  69. * @param string $name
  70. * @param HTML $html
  71. * @return void
  72. */
  73. public function __construct($name)
  74. {
  75. $this->name = $name;
  76. }
  77. /**
  78. * Add an asset to the container.
  79. *
  80. * The extension of the asset source will be used to determine the type of
  81. * asset being registered (CSS or JavaScript). When using a non-standard
  82. * extension, the style/script methods may be used to register assets.
  83. *
  84. * <code>
  85. * // Add an asset to the container
  86. * Asset::container()->add('jquery', 'js/jquery.js');
  87. *
  88. * // Add an asset that has dependencies on other assets
  89. * Asset::add('jquery', 'js/jquery.js', 'jquery-ui');
  90. *
  91. * // Add an asset that should have attributes applied to its tags
  92. * Asset::add('jquery', 'js/jquery.js', null, array('defer'));
  93. * </code>
  94. *
  95. * @param string $name
  96. * @param string $source
  97. * @param array $dependencies
  98. * @param array $attributes
  99. * @return void
  100. */
  101. public function add($name, $source, $dependencies = array(), $attributes = array())
  102. {
  103. $type = (pathinfo($source, PATHINFO_EXTENSION) == 'css') ? 'style' : 'script';
  104. return $this->$type($name, $source, $dependencies, $attributes);
  105. }
  106. /**
  107. * Add a CSS file to the registered assets.
  108. *
  109. * @param string $name
  110. * @param string $source
  111. * @param array $dependencies
  112. * @param array $attributes
  113. * @return Asset_Container
  114. */
  115. public function style($name, $source, $dependencies = array(), $attributes = array())
  116. {
  117. if ( ! array_key_exists('media', $attributes))
  118. {
  119. $attributes['media'] = 'all';
  120. }
  121. $this->register('style', $name, $source, $dependencies, $attributes);
  122. return $this;
  123. }
  124. /**
  125. * Add a JavaScript file to the registered assets.
  126. *
  127. * @param string $name
  128. * @param string $source
  129. * @param array $dependencies
  130. * @param array $attributes
  131. * @return Asset_Container
  132. */
  133. public function script($name, $source, $dependencies = array(), $attributes = array())
  134. {
  135. $this->register('script', $name, $source, $dependencies, $attributes);
  136. return $this;
  137. }
  138. /**
  139. * Returns the full-path for an asset.
  140. *
  141. * @param string $source
  142. * @return string
  143. */
  144. public function path($source)
  145. {
  146. return Bundle::assets($this->bundle).$source;
  147. }
  148. /**
  149. * Set the bundle that the container's assets belong to.
  150. *
  151. * @param string $bundle
  152. * @return Asset_Container
  153. */
  154. public function bundle($bundle)
  155. {
  156. $this->bundle = $bundle;
  157. return $this;
  158. }
  159. /**
  160. * Add an asset to the array of registered assets.
  161. *
  162. * @param string $type
  163. * @param string $name
  164. * @param string $source
  165. * @param array $dependencies
  166. * @param array $attributes
  167. * @return void
  168. */
  169. protected function register($type, $name, $source, $dependencies, $attributes)
  170. {
  171. $dependencies = (array) $dependencies;
  172. $attributes = (array) $attributes;
  173. $this->assets[$type][$name] = compact('source', 'dependencies', 'attributes');
  174. }
  175. /**
  176. * Get the links to all of the registered CSS assets.
  177. *
  178. * @return string
  179. */
  180. public function styles()
  181. {
  182. return $this->group('style');
  183. }
  184. /**
  185. * Get the links to all of the registered JavaScript assets.
  186. *
  187. * @return string
  188. */
  189. public function scripts()
  190. {
  191. return $this->group('script');
  192. }
  193. /**
  194. * Get all of the registered assets for a given type / group.
  195. *
  196. * @param string $group
  197. * @return string
  198. */
  199. protected function group($group)
  200. {
  201. if ( ! isset($this->assets[$group]) or count($this->assets[$group]) == 0) return '';
  202. $assets = '';
  203. foreach ($this->arrange($this->assets[$group]) as $name => $data)
  204. {
  205. $assets .= $this->asset($group, $name);
  206. }
  207. return $assets;
  208. }
  209. /**
  210. * Get the HTML link to a registered asset.
  211. *
  212. * @param string $group
  213. * @param string $name
  214. * @return string
  215. */
  216. protected function asset($group, $name)
  217. {
  218. if ( ! isset($this->assets[$group][$name])) return '';
  219. $asset = $this->assets[$group][$name];
  220. // If the bundle source is not a complete URL, we will go ahead and prepend
  221. // the bundle's asset path to the source provided with the asset. This will
  222. // ensure that we attach the correct path to the asset.
  223. if (filter_var($asset['source'], FILTER_VALIDATE_URL) === false)
  224. {
  225. $asset['source'] = $this->path($asset['source']);
  226. }
  227. return HTML::$group($asset['source'], $asset['attributes']);
  228. }
  229. /**
  230. * Sort and retrieve assets based on their dependencies
  231. *
  232. * @param array $assets
  233. * @return array
  234. */
  235. protected function arrange($assets)
  236. {
  237. list($original, $sorted) = array($assets, array());
  238. while (count($assets) > 0)
  239. {
  240. foreach ($assets as $asset => $value)
  241. {
  242. $this->evaluate_asset($asset, $value, $original, $sorted, $assets);
  243. }
  244. }
  245. return $sorted;
  246. }
  247. /**
  248. * Evaluate an asset and its dependencies.
  249. *
  250. * @param string $asset
  251. * @param string $value
  252. * @param array $original
  253. * @param array $sorted
  254. * @param array $assets
  255. * @return void
  256. */
  257. protected function evaluate_asset($asset, $value, $original, &$sorted, &$assets)
  258. {
  259. // If the asset has no more dependencies, we can add it to the sorted list
  260. // and remove it from the array of assets. Otherwise, we will not verify
  261. // the asset's dependencies and determine if they've been sorted.
  262. if (count($assets[$asset]['dependencies']) == 0)
  263. {
  264. $sorted[$asset] = $value;
  265. unset($assets[$asset]);
  266. }
  267. else
  268. {
  269. foreach ($assets[$asset]['dependencies'] as $key => $dependency)
  270. {
  271. if ( ! $this->dependency_is_valid($asset, $dependency, $original, $assets))
  272. {
  273. unset($assets[$asset]['dependencies'][$key]);
  274. continue;
  275. }
  276. // If the dependency has not yet been added to the sorted list, we can not
  277. // remove it from this asset's array of dependencies. We'll try again on
  278. // the next trip through the loop.
  279. if ( ! isset($sorted[$dependency])) continue;
  280. unset($assets[$asset]['dependencies'][$key]);
  281. }
  282. }
  283. }
  284. /**
  285. * Verify that an asset's dependency is valid.
  286. *
  287. * A dependency is considered valid if it exists, is not a circular reference, and is
  288. * not a reference to the owning asset itself. If the dependency doesn't exist, no
  289. * error or warning will be given. For the other cases, an exception is thrown.
  290. *
  291. * @param string $asset
  292. * @param string $dependency
  293. * @param array $original
  294. * @param array $assets
  295. * @return bool
  296. */
  297. protected function dependency_is_valid($asset, $dependency, $original, $assets)
  298. {
  299. if ( ! isset($original[$dependency]))
  300. {
  301. return false;
  302. }
  303. elseif ($dependency === $asset)
  304. {
  305. throw new \Exception("Asset [$asset] is dependent on itself.");
  306. }
  307. elseif (isset($assets[$dependency]) and in_array($asset, $assets[$dependency]['dependencies']))
  308. {
  309. throw new \Exception("Assets [$asset] and [$dependency] have a circular dependency.");
  310. }
  311. return true;
  312. }
  313. }