bundle.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 any
  72. // 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. return ($bundle == DEFAULT_BUNDLE) ? path('app') : static::location($bundle);
  212. }
  213. /**
  214. * Return the root asset path for the given bundle.
  215. *
  216. * @param string $bundle
  217. * @return string
  218. */
  219. public static function assets($bundle)
  220. {
  221. return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
  222. }
  223. /**
  224. * Get the bundle name from a given identifier.
  225. *
  226. * <code>
  227. * // Returns "admin" as the bundle name for the identifier
  228. * $bundle = Bundle::name('admin::home.index');
  229. * </code>
  230. *
  231. * @param string $identifier
  232. * @return string
  233. */
  234. public static function name($identifier)
  235. {
  236. list($bundle, $element) = static::parse($identifier);
  237. return $bundle;
  238. }
  239. /**
  240. * Get the element name from a given identifier.
  241. *
  242. * <code>
  243. * // Returns "home.index" as the element name for the identifier
  244. * $bundle = Bundle::bundle('admin::home.index');
  245. * </code>
  246. *
  247. * @param string $identifier
  248. * @return string
  249. */
  250. public static function element($identifier)
  251. {
  252. list($bundle, $element) = static::parse($identifier);
  253. return $element;
  254. }
  255. /**
  256. * Reconstruct an identifier from a given bundle and element.
  257. *
  258. * <code>
  259. * // Returns "admin::home.index"
  260. * $identifier = Bundle::identifier('admin', 'home.index');
  261. *
  262. * // Returns "home.index"
  263. * $identifier = Bundle::identifier('application', 'home.index');
  264. * </code>
  265. *
  266. * @param string $bundle
  267. * @param string $element
  268. * @return string
  269. */
  270. public static function identifier($bundle, $element)
  271. {
  272. return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
  273. }
  274. /**
  275. * Return the bundle name if it exists, else return the default bundle.
  276. *
  277. * @param string $bundle
  278. * @return string
  279. */
  280. public static function resolve($bundle)
  281. {
  282. return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
  283. }
  284. /**
  285. * Parse a element identifier and return the bundle name and element.
  286. *
  287. * <code>
  288. * // Returns array(null, 'admin.user')
  289. * $element = Bundle::parse('admin.user');
  290. *
  291. * // Parses "admin::user" and returns array('admin', 'user')
  292. * $element = Bundle::parse('admin::user');
  293. * </code>
  294. *
  295. * @param string $identifier
  296. * @return array
  297. */
  298. public static function parse($identifier)
  299. {
  300. // The parsed elements are cached so we don't have to reparse them on each
  301. // subsequent request for the parsed element. So, if we've already parsed
  302. // the given element, we'll just return the cached copy.
  303. if (isset(static::$elements[$identifier]))
  304. {
  305. return static::$elements[$identifier];
  306. }
  307. if (strpos($identifier, '::') !== false)
  308. {
  309. $element = explode('::', strtolower($identifier));
  310. }
  311. // If no bundle is in the identifier, we will insert the default bundle
  312. // since classes like Config and Lang organize their items by bundle.
  313. // The "application" folder essentially behaves as a bundle.
  314. else
  315. {
  316. $element = array(DEFAULT_BUNDLE, strtolower($identifier));
  317. }
  318. return static::$elements[$identifier] = $element;
  319. }
  320. /**
  321. * Get the information for a given bundle.
  322. *
  323. * @param string $bundle
  324. * @return object
  325. */
  326. public static function get($bundle)
  327. {
  328. return array_get(static::$bundles, $bundle);
  329. }
  330. /**
  331. * Get an option for a given bundle.
  332. *
  333. * @param string $bundle
  334. * @param string $option
  335. * @return mixed
  336. */
  337. public static function option($bundle, $option)
  338. {
  339. $bundle = static::get($bundle);
  340. if ( ! is_null($bundle)) return array_get($bundle, $option);
  341. }
  342. /**
  343. * Get all of the installed bundles for the application.
  344. *
  345. * @return array
  346. */
  347. public static function all()
  348. {
  349. return static::$bundles;
  350. }
  351. /**
  352. * Get all of the installed bundle names.
  353. *
  354. * @return array
  355. */
  356. public static function names()
  357. {
  358. return array_keys(static::$bundles);
  359. }
  360. }