bundle.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
  2. class Bundle {
  3. /**
  4. * All of the application's bundles.
  5. *
  6. * @var array
  7. */
  8. protected static $bundles;
  9. /**
  10. * A cache of the parsed bundle elements.
  11. *
  12. * @var array
  13. */
  14. protected static $elements = array();
  15. /**
  16. * All of the bundles that have been started.
  17. *
  18. * @var array
  19. */
  20. protected static $started = array();
  21. /**
  22. * Load a bundle by running it's start-up script.
  23. *
  24. * If the bundle has already been started, no action will be taken.
  25. *
  26. * @param string $bundle
  27. * @return void
  28. */
  29. public static function start($bundle)
  30. {
  31. if (static::started($bundle)) return;
  32. if ($bundle !== DEFAULT_BUNDLE and ! static::exists($bundle))
  33. {
  34. throw new \Exception("Bundle [$bundle] has not been installed.");
  35. }
  36. // Each bundle may have a "start" script which is responsible for preparing
  37. // the bundle for use by the application. The start script may register any
  38. // classes the bundle uses with the auto-loader, or perhaps will start any
  39. // dependent bundles so that they are available.
  40. if (file_exists($path = static::path($bundle).'bundle'.EXT))
  41. {
  42. require $path;
  43. }
  44. // Each bundle may also have a "routes" file which is responsible for
  45. // registering the bundle's routes. This is kept separate from the
  46. // start script for reverse routing efficiency purposes.
  47. static::routes($bundle);
  48. static::$started[] = strtolower($bundle);
  49. }
  50. /**
  51. * Load the "routes" file for a given bundle.
  52. *
  53. * @param string $bundle
  54. * @return void
  55. */
  56. public static function routes($bundle)
  57. {
  58. if (static::started($bundle)) return;
  59. if (file_exists($path = static::path($bundle).'routes'.EXT))
  60. {
  61. require $path;
  62. }
  63. }
  64. /**
  65. * Determine if the given bundle is "routable".
  66. *
  67. * A bundle is considered routable if it has a controller directory or a routes file.
  68. *
  69. * @param string $bundle
  70. * @return bool
  71. */
  72. public static function routable($bundle)
  73. {
  74. $path = static::path($bundle);
  75. return is_dir($path.'controllers/') or file_exists($path.'routes'.EXT);
  76. }
  77. /**
  78. * Deteremine if a bundle exists within the bundles directory.
  79. *
  80. * @param string $bundle
  81. * @return bool
  82. */
  83. public static function exists($bundle)
  84. {
  85. return in_array(strtolower($bundle), static::all());
  86. }
  87. /**
  88. * Determine if a given bundle has been started for the request.
  89. *
  90. * @param string $bundle
  91. * @return void
  92. */
  93. public static function started($bundle)
  94. {
  95. return in_array(strtolower($bundle), static::$started);
  96. }
  97. /**
  98. * Get the identifier prefix for the bundle.
  99. *
  100. * @param string $bundle
  101. * @return string
  102. */
  103. public static function prefix($bundle)
  104. {
  105. return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
  106. }
  107. /**
  108. * Get the class prefix for a given bundle.
  109. *
  110. * @param string $bundle
  111. * @return string
  112. */
  113. public static function class_prefix($bundle)
  114. {
  115. return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
  116. }
  117. /**
  118. * Return the root bundle path for a given bundle.
  119. *
  120. * <code>
  121. * // Returns the bundle path for the "admin" bundle
  122. * $path = Bundle::path('admin');
  123. *
  124. * // Returns the APP_PATH constant as the default bundle
  125. * $path = Bundle::path('application');
  126. * </code>
  127. *
  128. * @param string $bundle
  129. * @return string
  130. */
  131. public static function path($bundle)
  132. {
  133. return ($bundle != DEFAULT_BUNDLE) ? BUNDLE_PATH.strtolower($bundle).DS : APP_PATH;
  134. }
  135. /**
  136. * Return the root asset path for the given bundle.
  137. *
  138. * @param string $bundle
  139. * @return string
  140. */
  141. public static function assets($bundle)
  142. {
  143. return ($bundle != DEFAULT_BUNDLE) ? PUBLIC_PATH."bundles/{$bundle}/" : PUBLIC_PATH;
  144. }
  145. /**
  146. * Get the bundle name from a given identifier.
  147. *
  148. * <code>
  149. * // Returns "admin" as the bundle name for the identifier
  150. * $bundle = Bundle::name('admin::home.index');
  151. * </code>
  152. *
  153. * @param string $identifier
  154. * @return string
  155. */
  156. public static function name($identifier)
  157. {
  158. list($bundle, $element) = static::parse($identifier);
  159. return $bundle;
  160. }
  161. /**
  162. * Get the element name from a given identifier.
  163. *
  164. * <code>
  165. * // Returns "home.index" as the element name for the identifier
  166. * $bundle = Bundle::bundle('admin::home.index');
  167. * </code>
  168. *
  169. * @param string $identifier
  170. * @return string
  171. */
  172. public static function element($identifier)
  173. {
  174. list($bundle, $element) = static::parse($identifier);
  175. return $element;
  176. }
  177. /**
  178. * Reconstruct an identifier from a given bundle and element.
  179. *
  180. * <code>
  181. * // Returns "admin::home.index"
  182. * $identifier = Bundle::identifier('admin', 'home.index');
  183. *
  184. * // Returns "home.index"
  185. * $identifier = Bundle::identifier('application', 'home.index');
  186. * </code>
  187. *
  188. * @param string $bundle
  189. * @param string $element
  190. * @return string
  191. */
  192. public static function identifier($bundle, $element)
  193. {
  194. return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
  195. }
  196. /**
  197. * Return the bundle name if it exists, else return the default bundle.
  198. *
  199. * @param string $bundle
  200. * @return string
  201. */
  202. public static function resolve($bundle)
  203. {
  204. return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
  205. }
  206. /**
  207. * Parse a element identifier and return the bundle name and element.
  208. *
  209. * <code>
  210. * // Returns array(null, 'admin.user')
  211. * $element = Bundle::parse('admin.user');
  212. *
  213. * // Parses "admin::user" and returns array('admin', 'user')
  214. * $element = Bundle::parse('admin::user');
  215. * </code>
  216. *
  217. * @param string $identifier
  218. * @return array
  219. */
  220. public static function parse($identifier)
  221. {
  222. // The parsed elements are cached so we don't have to reparse them on each
  223. // subsequent request for the parsed element. So, if we've already parsed
  224. // the given element, we'll just return the cached copy.
  225. if (isset(static::$elements[$identifier]))
  226. {
  227. return static::$elements[$identifier];
  228. }
  229. if (strpos($identifier, '::') !== false)
  230. {
  231. $element = explode('::', strtolower($identifier));
  232. }
  233. // If no bundle is in the identifier, we will insert the default bundle
  234. // since classes like Config and Lang organize their items by bundle.
  235. // The "application" folder essentially behaves as a bundle.
  236. else
  237. {
  238. $element = array(DEFAULT_BUNDLE, strtolower($identifier));
  239. }
  240. return static::$elements[$identifier] = $element;
  241. }
  242. /**
  243. * Detect all of the existing bundles in the application.
  244. *
  245. * The names of the bundles are cached so this operation will be only be
  246. * performed once and then the same array will be returned on each later
  247. * request for the bundle names.
  248. *
  249. * @return array
  250. */
  251. public static function all()
  252. {
  253. if (is_array(static::$bundles)) return static::$bundles;
  254. $bundles = array();
  255. foreach (array_filter(glob(BUNDLE_PATH.'*'), 'is_dir') as $bundle)
  256. {
  257. $bundles[] = basename($bundle);
  258. }
  259. return static::$bundles = $bundles;
  260. }
  261. }