bundle.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_once $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 (file_exists($path = static::path($bundle).'routes'.EXT))
  59. {
  60. require_once $path;
  61. }
  62. }
  63. /**
  64. * Determine if the given bundle is "routable".
  65. *
  66. * A bundle is considered routable if it has a controller directory or a routes file.
  67. *
  68. * @param string $bundle
  69. * @return bool
  70. */
  71. public static function routable($bundle)
  72. {
  73. $path = static::path($bundle);
  74. return is_dir($path.'controllers/') or file_exists($path.'routes'.EXT);
  75. }
  76. /**
  77. * Deteremine if a bundle exists within the bundles directory.
  78. *
  79. * @param string $bundle
  80. * @return bool
  81. */
  82. public static function exists($bundle)
  83. {
  84. return in_array(strtolower($bundle), static::all());
  85. }
  86. /**
  87. * Determine if a given bundle has been started for the request.
  88. *
  89. * @param string $bundle
  90. * @return void
  91. */
  92. public static function started($bundle)
  93. {
  94. return in_array(strtolower($bundle), static::$started);
  95. }
  96. /**
  97. * Get the identifier prefix for the bundle.
  98. *
  99. * @param string $bundle
  100. * @return string
  101. */
  102. public static function prefix($bundle)
  103. {
  104. return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
  105. }
  106. /**
  107. * Get the class prefix for a given bundle.
  108. *
  109. * @param string $bundle
  110. * @return string
  111. */
  112. public static function class_prefix($bundle)
  113. {
  114. return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
  115. }
  116. /**
  117. * Return the root bundle path for a given bundle.
  118. *
  119. * <code>
  120. * // Returns the bundle path for the "admin" bundle
  121. * $path = Bundle::path('admin');
  122. *
  123. * // Returns the APP_PATH constant as the default bundle
  124. * $path = Bundle::path('application');
  125. * </code>
  126. *
  127. * @param string $bundle
  128. * @return string
  129. */
  130. public static function path($bundle)
  131. {
  132. return ($bundle != DEFAULT_BUNDLE) ? BUNDLE_PATH.strtolower($bundle).DS : APP_PATH;
  133. }
  134. /**
  135. * Return the root asset path for the given bundle.
  136. *
  137. * @param string $bundle
  138. * @return string
  139. */
  140. public static function assets($bundle)
  141. {
  142. return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
  143. }
  144. /**
  145. * Get the bundle name from a given identifier.
  146. *
  147. * <code>
  148. * // Returns "admin" as the bundle name for the identifier
  149. * $bundle = Bundle::name('admin::home.index');
  150. * </code>
  151. *
  152. * @param string $identifier
  153. * @return string
  154. */
  155. public static function name($identifier)
  156. {
  157. list($bundle, $element) = static::parse($identifier);
  158. return $bundle;
  159. }
  160. /**
  161. * Get the element name from a given identifier.
  162. *
  163. * <code>
  164. * // Returns "home.index" as the element name for the identifier
  165. * $bundle = Bundle::bundle('admin::home.index');
  166. * </code>
  167. *
  168. * @param string $identifier
  169. * @return string
  170. */
  171. public static function element($identifier)
  172. {
  173. list($bundle, $element) = static::parse($identifier);
  174. return $element;
  175. }
  176. /**
  177. * Reconstruct an identifier from a given bundle and element.
  178. *
  179. * <code>
  180. * // Returns "admin::home.index"
  181. * $identifier = Bundle::identifier('admin', 'home.index');
  182. *
  183. * // Returns "home.index"
  184. * $identifier = Bundle::identifier('application', 'home.index');
  185. * </code>
  186. *
  187. * @param string $bundle
  188. * @param string $element
  189. * @return string
  190. */
  191. public static function identifier($bundle, $element)
  192. {
  193. return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
  194. }
  195. /**
  196. * Return the bundle name if it exists, else return the default bundle.
  197. *
  198. * @param string $bundle
  199. * @return string
  200. */
  201. public static function resolve($bundle)
  202. {
  203. return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
  204. }
  205. /**
  206. * Parse a element identifier and return the bundle name and element.
  207. *
  208. * <code>
  209. * // Returns array(null, 'admin.user')
  210. * $element = Bundle::parse('admin.user');
  211. *
  212. * // Parses "admin::user" and returns array('admin', 'user')
  213. * $element = Bundle::parse('admin::user');
  214. * </code>
  215. *
  216. * @param string $identifier
  217. * @return array
  218. */
  219. public static function parse($identifier)
  220. {
  221. // The parsed elements are cached so we don't have to reparse them on each
  222. // subsequent request for the parsed element. So, if we've already parsed
  223. // the given element, we'll just return the cached copy.
  224. if (isset(static::$elements[$identifier]))
  225. {
  226. return static::$elements[$identifier];
  227. }
  228. if (strpos($identifier, '::') !== false)
  229. {
  230. $element = explode('::', strtolower($identifier));
  231. }
  232. // If no bundle is in the identifier, we will insert the default bundle
  233. // since classes like Config and Lang organize their items by bundle.
  234. // The "application" folder essentially behaves as a bundle.
  235. else
  236. {
  237. $element = array(DEFAULT_BUNDLE, strtolower($identifier));
  238. }
  239. return static::$elements[$identifier] = $element;
  240. }
  241. /**
  242. * Detect all of the existing bundles in the application.
  243. *
  244. * @return array
  245. */
  246. public static function all()
  247. {
  248. if (is_array(static::$bundles)) return static::$bundles;
  249. $bundles = array();
  250. $files = glob(BUNDLE_PATH.'*');
  251. // When open_basedir is enabled the glob function returns false on
  252. // an empty array. We'll check for this and return an empty array
  253. // if the bundle directory doesn't have any bundles.
  254. if ($files !== false)
  255. {
  256. foreach (array_filter($files, 'is_dir') as $bundle)
  257. {
  258. $bundles[] = basename($bundle);
  259. }
  260. }
  261. return static::$bundles = $bundles;
  262. }
  263. }