bundle.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php namespace Laravel; defined('DS') or die('No direct script access.');
  2. use FilesystemIterator as fIterator;
  3. class Bundle {
  4. /**
  5. * All of the application's bundles.
  6. *
  7. * @var array
  8. */
  9. public static $bundles = array();
  10. /**
  11. * A cache of the parsed bundle elements.
  12. *
  13. * @var array
  14. */
  15. public static $elements = array();
  16. /**
  17. * All of the bundles that have been started.
  18. *
  19. * @var array
  20. */
  21. public static $started = array();
  22. /**
  23. * All of the bundles that have their routes files loaded.
  24. *
  25. * @var array
  26. */
  27. public static $routed = array();
  28. /**
  29. * Register the bundle for the application.
  30. *
  31. * @param string $bundle
  32. * @param array $config
  33. * @return void
  34. */
  35. public static function register($bundle, $config = array())
  36. {
  37. $defaults = array('handles' => null, 'auto' => false);
  38. // If the given configuration is actually a string, we will assume it is a
  39. // location and set the bundle name to match it. This is common for most
  40. // bundles who simply live in the root bundle directory.
  41. if (is_string($config))
  42. {
  43. $bundle = $config;
  44. $config = array('location' => $bundle);
  45. }
  46. // IF no location is set, we will set the location to match the name of
  47. // the bundle. This is for bundles who are installed to the root of
  48. // the bundle directory so a location was not set.
  49. if ( ! isset($config['location']))
  50. {
  51. $config['location'] = $bundle;
  52. }
  53. static::$bundles[$bundle] = array_merge($defaults, $config);
  54. }
  55. /**
  56. * Load a bundle by running it's start-up script.
  57. *
  58. * If the bundle has already been started, no action will be taken.
  59. *
  60. * @param string $bundle
  61. * @return void
  62. */
  63. public static function start($bundle)
  64. {
  65. if (static::started($bundle)) return;
  66. if ( ! static::exists($bundle))
  67. {
  68. throw new \Exception("Bundle [$bundle] has not been installed.");
  69. }
  70. // Each bundle may have a start script which is responsible for preparing
  71. // the bundle for use by the application. The start script may register
  72. // any classes the bundle uses with the auto-loader, etc.
  73. if (file_exists($path = static::path($bundle).'start'.EXT))
  74. {
  75. require $path;
  76. }
  77. // Each bundle may also have a "routes" file which is responsible for
  78. // registering the bundle's routes. This is kept separate from the
  79. // start script for reverse routing efficiency purposes.
  80. static::routes($bundle);
  81. Event::fire("started: {$bundle}");
  82. static::$started[] = strtolower($bundle);
  83. }
  84. /**
  85. * Load the "routes" file for a given bundle.
  86. *
  87. * @param string $bundle
  88. * @return void
  89. */
  90. public static function routes($bundle)
  91. {
  92. if (static::routed($bundle)) return;
  93. $path = static::path($bundle).'routes'.EXT;
  94. // By setting the bundle property on the router the router knows what
  95. // value to replace the (:bundle) place-holder with when the bundle
  96. // routes are added, keeping the routes flexible.
  97. Routing\Router::$bundle = static::option($bundle, 'handles');
  98. if ( ! static::routed($bundle) and file_exists($path))
  99. {
  100. static::$routed[] = $bundle;
  101. require $path;
  102. }
  103. }
  104. /**
  105. * Disable a bundle for the current request.
  106. *
  107. * @param string $bundle
  108. * @return void
  109. */
  110. public static function disable($bundle)
  111. {
  112. unset(static::$bundles[$bundle]);
  113. }
  114. /**
  115. * Determine which bundle handles the given URI.
  116. *
  117. * If no bundle is assigned to handle the URI, the default bundle is returned.
  118. *
  119. * @param string $uri
  120. * @return string
  121. */
  122. public static function handles($uri)
  123. {
  124. $uri = rtrim($uri, '/').'/';
  125. foreach (static::$bundles as $key => $value)
  126. {
  127. if (isset($value['handles']) and starts_with($uri, $value['handles'].'/'))
  128. {
  129. return $key;
  130. }
  131. }
  132. return DEFAULT_BUNDLE;
  133. }
  134. /**
  135. * Deteremine if a bundle exists within the bundles directory.
  136. *
  137. * @param string $bundle
  138. * @return bool
  139. */
  140. public static function exists($bundle)
  141. {
  142. return $bundle == DEFAULT_BUNDLE or in_array(strtolower($bundle), static::names());
  143. }
  144. /**
  145. * Get the full path location of a given bundle.
  146. *
  147. * @param string $bundle
  148. * @return string
  149. */
  150. public static function location($bundle)
  151. {
  152. $location = array_get(static::$bundles, $bundle.'.location');
  153. return path('bundle').str_finish($location, DS);
  154. }
  155. /**
  156. * Determine if a given bundle has been started for the request.
  157. *
  158. * @param string $bundle
  159. * @return void
  160. */
  161. public static function started($bundle)
  162. {
  163. return in_array(strtolower($bundle), static::$started);
  164. }
  165. /**
  166. * Determine if a given bundle has its routes file loaded.
  167. *
  168. * @param string $bundle
  169. * @return void
  170. */
  171. public static function routed($bundle)
  172. {
  173. return in_array(strtolower($bundle), static::$routed);
  174. }
  175. /**
  176. * Get the identifier prefix for the bundle.
  177. *
  178. * @param string $bundle
  179. * @return string
  180. */
  181. public static function prefix($bundle)
  182. {
  183. return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
  184. }
  185. /**
  186. * Get the class prefix for a given bundle.
  187. *
  188. * @param string $bundle
  189. * @return string
  190. */
  191. public static function class_prefix($bundle)
  192. {
  193. return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
  194. }
  195. /**
  196. * Return the root bundle path for a given bundle.
  197. *
  198. * <code>
  199. * // Returns the bundle path for the "admin" bundle
  200. * $path = Bundle::path('admin');
  201. *
  202. * // Returns the path('app') constant as the default bundle
  203. * $path = Bundle::path('application');
  204. * </code>
  205. *
  206. * @param string $bundle
  207. * @return string
  208. */
  209. public static function path($bundle)
  210. {
  211. if (is_null($bundle)) return static::path(DEFAULT_BUNDLE);
  212. return ($bundle == DEFAULT_BUNDLE) ? path('app') : static::location($bundle);
  213. }
  214. /**
  215. * Return the root asset path for the given bundle.
  216. *
  217. * @param string $bundle
  218. * @return string
  219. */
  220. public static function assets($bundle)
  221. {
  222. return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
  223. }
  224. /**
  225. * Get the bundle name from a given identifier.
  226. *
  227. * <code>
  228. * // Returns "admin" as the bundle name for the identifier
  229. * $bundle = Bundle::name('admin::home.index');
  230. * </code>
  231. *
  232. * @param string $identifier
  233. * @return string
  234. */
  235. public static function name($identifier)
  236. {
  237. list($bundle, $element) = static::parse($identifier);
  238. return $bundle;
  239. }
  240. /**
  241. * Get the element name from a given identifier.
  242. *
  243. * <code>
  244. * // Returns "home.index" as the element name for the identifier
  245. * $bundle = Bundle::bundle('admin::home.index');
  246. * </code>
  247. *
  248. * @param string $identifier
  249. * @return string
  250. */
  251. public static function element($identifier)
  252. {
  253. list($bundle, $element) = static::parse($identifier);
  254. return $element;
  255. }
  256. /**
  257. * Reconstruct an identifier from a given bundle and element.
  258. *
  259. * <code>
  260. * // Returns "admin::home.index"
  261. * $identifier = Bundle::identifier('admin', 'home.index');
  262. *
  263. * // Returns "home.index"
  264. * $identifier = Bundle::identifier('application', 'home.index');
  265. * </code>
  266. *
  267. * @param string $bundle
  268. * @param string $element
  269. * @return string
  270. */
  271. public static function identifier($bundle, $element)
  272. {
  273. return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
  274. }
  275. /**
  276. * Return the bundle name if it exists, else return the default bundle.
  277. *
  278. * @param string $bundle
  279. * @return string
  280. */
  281. public static function resolve($bundle)
  282. {
  283. return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
  284. }
  285. /**
  286. * Parse a element identifier and return the bundle name and element.
  287. *
  288. * <code>
  289. * // Returns array(null, 'admin.user')
  290. * $element = Bundle::parse('admin.user');
  291. *
  292. * // Parses "admin::user" and returns array('admin', 'user')
  293. * $element = Bundle::parse('admin::user');
  294. * </code>
  295. *
  296. * @param string $identifier
  297. * @return array
  298. */
  299. public static function parse($identifier)
  300. {
  301. // The parsed elements are cached so we don't have to reparse them on each
  302. // subsequent request for the parsed element. So, if we've already parsed
  303. // the given element, we'll just return the cached copy.
  304. if (isset(static::$elements[$identifier]))
  305. {
  306. return static::$elements[$identifier];
  307. }
  308. if (strpos($identifier, '::') !== false)
  309. {
  310. $element = explode('::', strtolower($identifier));
  311. }
  312. // If no bundle is in the identifier, we will insert the default bundle
  313. // since classes like Config and Lang organize their items by bundle.
  314. // The "application" folder essentially behaves as a bundle.
  315. else
  316. {
  317. $element = array(DEFAULT_BUNDLE, strtolower($identifier));
  318. }
  319. return static::$elements[$identifier] = $element;
  320. }
  321. /**
  322. * Get the information for a given bundle.
  323. *
  324. * @param string $bundle
  325. * @return object
  326. */
  327. public static function get($bundle)
  328. {
  329. return array_get(static::$bundles, $bundle);
  330. }
  331. /**
  332. * Get an option for a given bundle.
  333. *
  334. * @param string $bundle
  335. * @param string $option
  336. * @return mixed
  337. */
  338. public static function option($bundle, $option)
  339. {
  340. $bundle = static::get($bundle);
  341. if ( ! is_null($bundle)) return array_get($bundle, $option);
  342. }
  343. /**
  344. * Get all of the installed bundles for the application.
  345. *
  346. * @return array
  347. */
  348. public static function all()
  349. {
  350. return static::$bundles;
  351. }
  352. /**
  353. * Get all of the installed bundle names.
  354. *
  355. * @return array
  356. */
  357. public static function names()
  358. {
  359. return array_keys(static::$bundles);
  360. }
  361. }