bundle.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php namespace Laravel; isset($GLOBALS['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. 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. throw new \Exception("Location not set for bundle [$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'] = $GLOBALS['BUNDLE_PATH'].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. static::$started[] = strtolower($bundle);
  77. }
  78. /**
  79. * Load the "routes" file for a given bundle.
  80. *
  81. * @param string $bundle
  82. * @return void
  83. */
  84. public static function routes($bundle)
  85. {
  86. if (file_exists($path = static::path($bundle).'routes'.EXT))
  87. {
  88. require_once $path;
  89. }
  90. }
  91. /**
  92. * Determine which bundle handles the given URI.
  93. *
  94. * If no bundle is assigned to handle the URI, the default bundle is returned.
  95. *
  96. * @param string $bundle
  97. * @return string
  98. */
  99. public static function handles($uri)
  100. {
  101. foreach (static::$bundles as $key => $value)
  102. {
  103. if (starts_with($uri, $value['handles'])) return $key;
  104. }
  105. return DEFAULT_BUNDLE;
  106. }
  107. /**
  108. * Deteremine if a bundle exists within the bundles directory.
  109. *
  110. * @param string $bundle
  111. * @return bool
  112. */
  113. public static function exists($bundle)
  114. {
  115. return in_array(strtolower($bundle), static::names());
  116. }
  117. /**
  118. * Determine if a given bundle has been started for the request.
  119. *
  120. * @param string $bundle
  121. * @return void
  122. */
  123. public static function started($bundle)
  124. {
  125. return in_array(strtolower($bundle), static::$started);
  126. }
  127. /**
  128. * Get the identifier prefix for the bundle.
  129. *
  130. * @param string $bundle
  131. * @return string
  132. */
  133. public static function prefix($bundle)
  134. {
  135. return ($bundle !== DEFAULT_BUNDLE) ? "{$bundle}::" : '';
  136. }
  137. /**
  138. * Get the class prefix for a given bundle.
  139. *
  140. * @param string $bundle
  141. * @return string
  142. */
  143. public static function class_prefix($bundle)
  144. {
  145. return ($bundle !== DEFAULT_BUNDLE) ? Str::classify($bundle).'_' : '';
  146. }
  147. /**
  148. * Return the root bundle path for a given bundle.
  149. *
  150. * <code>
  151. * // Returns the bundle path for the "admin" bundle
  152. * $path = Bundle::path('admin');
  153. *
  154. * // Returns the $GLOBALS['APP_PATH'] constant as the default bundle
  155. * $path = Bundle::path('application');
  156. * </code>
  157. *
  158. * @param string $bundle
  159. * @return string
  160. */
  161. public static function path($bundle)
  162. {
  163. return ($bundle == DEFAULT_BUNDLE) ? $GLOBALS['APP_PATH'] : static::$bundles[$bundle]['location'];
  164. }
  165. /**
  166. * Return the root asset path for the given bundle.
  167. *
  168. * @param string $bundle
  169. * @return string
  170. */
  171. public static function assets($bundle)
  172. {
  173. return ($bundle != DEFAULT_BUNDLE) ? URL::base()."/bundles/{$bundle}/" : URL::base().'/';
  174. }
  175. /**
  176. * Get the bundle name from a given identifier.
  177. *
  178. * <code>
  179. * // Returns "admin" as the bundle name for the identifier
  180. * $bundle = Bundle::name('admin::home.index');
  181. * </code>
  182. *
  183. * @param string $identifier
  184. * @return string
  185. */
  186. public static function name($identifier)
  187. {
  188. list($bundle, $element) = static::parse($identifier);
  189. return $bundle;
  190. }
  191. /**
  192. * Get the element name from a given identifier.
  193. *
  194. * <code>
  195. * // Returns "home.index" as the element name for the identifier
  196. * $bundle = Bundle::bundle('admin::home.index');
  197. * </code>
  198. *
  199. * @param string $identifier
  200. * @return string
  201. */
  202. public static function element($identifier)
  203. {
  204. list($bundle, $element) = static::parse($identifier);
  205. return $element;
  206. }
  207. /**
  208. * Reconstruct an identifier from a given bundle and element.
  209. *
  210. * <code>
  211. * // Returns "admin::home.index"
  212. * $identifier = Bundle::identifier('admin', 'home.index');
  213. *
  214. * // Returns "home.index"
  215. * $identifier = Bundle::identifier('application', 'home.index');
  216. * </code>
  217. *
  218. * @param string $bundle
  219. * @param string $element
  220. * @return string
  221. */
  222. public static function identifier($bundle, $element)
  223. {
  224. return (is_null($bundle) or $bundle == DEFAULT_BUNDLE) ? $element : $bundle.'::'.$element;
  225. }
  226. /**
  227. * Return the bundle name if it exists, else return the default bundle.
  228. *
  229. * @param string $bundle
  230. * @return string
  231. */
  232. public static function resolve($bundle)
  233. {
  234. return (static::exists($bundle)) ? $bundle : DEFAULT_BUNDLE;
  235. }
  236. /**
  237. * Parse a element identifier and return the bundle name and element.
  238. *
  239. * <code>
  240. * // Returns array(null, 'admin.user')
  241. * $element = Bundle::parse('admin.user');
  242. *
  243. * // Parses "admin::user" and returns array('admin', 'user')
  244. * $element = Bundle::parse('admin::user');
  245. * </code>
  246. *
  247. * @param string $identifier
  248. * @return array
  249. */
  250. public static function parse($identifier)
  251. {
  252. // The parsed elements are cached so we don't have to reparse them on each
  253. // subsequent request for the parsed element. So, if we've already parsed
  254. // the given element, we'll just return the cached copy.
  255. if (isset(static::$elements[$identifier]))
  256. {
  257. return static::$elements[$identifier];
  258. }
  259. if (strpos($identifier, '::') !== false)
  260. {
  261. $element = explode('::', strtolower($identifier));
  262. }
  263. // If no bundle is in the identifier, we will insert the default bundle
  264. // since classes like Config and Lang organize their items by bundle.
  265. // The "application" folder essentially behaves as a bundle.
  266. else
  267. {
  268. $element = array(DEFAULT_BUNDLE, strtolower($identifier));
  269. }
  270. return static::$elements[$identifier] = $element;
  271. }
  272. /**
  273. * Get the information for a given bundle.
  274. *
  275. * @param string $bundle
  276. * @return object
  277. */
  278. public static function get($bundle)
  279. {
  280. return (object) array_get(static::$bundles, $bundle);
  281. }
  282. /**
  283. * Get all of the installed bundles for the application.
  284. *
  285. * @return array
  286. */
  287. public static function all()
  288. {
  289. return static::$bundles;
  290. }
  291. /**
  292. * Get all of the installed bundle names.
  293. *
  294. * @return array
  295. */
  296. public static function names()
  297. {
  298. return array_keys(static::$bundles);
  299. }
  300. }