asset.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * The Asset class allows you to easily work with your apps assets.
  15. * It allows you to specify multiple paths to be searched for the
  16. * assets.
  17. *
  18. * You can configure the paths by copying the core/config/asset.php
  19. * config file into your app/config folder and changing the settings.
  20. *
  21. * @package Fuel
  22. * @subpackage Core
  23. */
  24. class Asset
  25. {
  26. /**
  27. * default instance
  28. *
  29. * @var array
  30. */
  31. protected static $_instance = null;
  32. /**
  33. * All the Asset instances
  34. *
  35. * @var array
  36. */
  37. protected static $_instances = array();
  38. /**
  39. * Default configuration values
  40. *
  41. * @var array
  42. */
  43. protected static $default_config = array(
  44. 'paths' => array('assets/'),
  45. 'img_dir' => 'img/',
  46. 'js_dir' => 'js/',
  47. 'css_dir' => 'css/',
  48. 'folders' => array(
  49. 'css' => array(),
  50. 'js' => array(),
  51. 'img' => array(),
  52. ),
  53. 'url' => '/',
  54. 'add_mtime' => true,
  55. 'indent_level' => 1,
  56. 'indent_with' => "\t",
  57. 'auto_render' => true,
  58. 'fail_silently' => false,
  59. );
  60. /**
  61. * This is called automatically by the Autoloader. It loads in the config
  62. *
  63. * @return void
  64. */
  65. public static function _init()
  66. {
  67. \Config::load('asset', true, false, true);
  68. }
  69. /**
  70. * Return a specific instance, or the default instance (is created if necessary)
  71. *
  72. * @param string instance name
  73. * @return Asset_Instance
  74. */
  75. public static function instance($instance = null)
  76. {
  77. if ($instance !== null)
  78. {
  79. if ( ! array_key_exists($instance, static::$_instances))
  80. {
  81. return false;
  82. }
  83. return static::$_instances[$instance];
  84. }
  85. if (static::$_instance === null)
  86. {
  87. static::$_instance = static::forge();
  88. }
  89. return static::$_instance;
  90. }
  91. /**
  92. * Gets a new instance of the Asset class.
  93. *
  94. * @param string instance name
  95. * @param array $config default config overrides
  96. * @return Asset_Instance
  97. */
  98. public static function forge($name = 'default', array $config = array())
  99. {
  100. if ($exists = static::instance($name))
  101. {
  102. \Error::notice('Asset with this name exists already, cannot be overwritten.');
  103. return $exists;
  104. }
  105. static::$_instances[$name] = new \Asset_Instance(array_merge(static::$default_config, \Config::get('asset'), $config));
  106. if ($name == 'default')
  107. {
  108. static::$_instance = static::$_instances[$name];
  109. }
  110. return static::$_instances[$name];
  111. }
  112. /**
  113. * Adds the given path to the front of the asset paths array. It adds paths
  114. * in a way so that asset paths are used First in Last Out.
  115. *
  116. * @param string the path to add
  117. * @return void
  118. */
  119. public static function add_path($path, $type = null)
  120. {
  121. static::instance()->add_path($path, $type);
  122. }
  123. /**
  124. * Removes the given path from the asset paths array
  125. *
  126. * @param string the path to remove
  127. * @return void
  128. */
  129. public static function remove_path($path, $type = null)
  130. {
  131. static::instance()->remove_path($path, $type);
  132. }
  133. /**
  134. * Renders the given group. Each tag will be separated by a line break.
  135. * You can optionally tell it to render the files raw. This means that
  136. * all CSS and JS files in the group will be read and the contents included
  137. * in the returning value.
  138. *
  139. * @param mixed the group to render
  140. * @param bool whether to return the raw file or not
  141. * @return string the group's output
  142. */
  143. public static function render($group = null, $raw = false)
  144. {
  145. return static::instance()->render($group, $raw);
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * CSS
  150. *
  151. * Either adds the stylesheet to the group, or returns the CSS tag.
  152. *
  153. * @access public
  154. * @param mixed The file name, or an array files.
  155. * @param array An array of extra attributes
  156. * @param string The asset group name
  157. * @return string
  158. */
  159. public static function css($stylesheets = array(), $attr = array(), $group = NULL, $raw = false)
  160. {
  161. return static::instance()->css($stylesheets, $attr, $group, $raw);
  162. }
  163. // --------------------------------------------------------------------
  164. /**
  165. * JS
  166. *
  167. * Either adds the javascript to the group, or returns the script tag.
  168. *
  169. * @access public
  170. * @param mixed The file name, or an array files.
  171. * @param array An array of extra attributes
  172. * @param string The asset group name
  173. * @return string
  174. */
  175. public static function js($scripts = array(), $attr = array(), $group = NULL, $raw = false)
  176. {
  177. return static::instance()->js($scripts, $attr, $group, $raw);
  178. }
  179. // --------------------------------------------------------------------
  180. /**
  181. * Img
  182. *
  183. * Either adds the image to the group, or returns the image tag.
  184. *
  185. * @access public
  186. * @param mixed The file name, or an array files.
  187. * @param array An array of extra attributes
  188. * @param string The asset group name
  189. * @return string
  190. */
  191. public static function img($images = array(), $attr = array(), $group = NULL)
  192. {
  193. return static::instance()->img($images, $attr, $group);
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Get File
  198. *
  199. * Locates a file in all the asset paths, and return it relative to the docroot
  200. *
  201. * @access public
  202. * @param string The filename to locate
  203. * @param string The sub-folder to look in (optional)
  204. * @return mixed Either the path to the file or false if not found
  205. */
  206. public static function get_file($file, $type, $folder = '')
  207. {
  208. return static::instance()->get_file($file, $type, $folder);
  209. }
  210. // --------------------------------------------------------------------
  211. /**
  212. * Find File
  213. *
  214. * Locates a file in all the asset paths.
  215. *
  216. * @access public
  217. * @param string The filename to locate
  218. * @param string The sub-folder to look in (optional)
  219. * @return mixed Either the path to the file or false if not found
  220. */
  221. public static function find_file($file, $type, $folder = '')
  222. {
  223. return static::instance()->find_file($file, $type, $folder);
  224. }
  225. }