bundle.php 7.8 KB

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