asset.php 8.2 KB

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