bundle.php 11 KB

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