asset.php 7.5 KB

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