module.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php namespace Laravel;
  2. class Module {
  3. /**
  4. * The active modules for the installation.
  5. *
  6. * This property is set in the Laravel bootstrap file, and the modules are defined
  7. * by the developer in the front controller.
  8. *
  9. * @var array
  10. */
  11. public static $modules = array();
  12. /**
  13. * All of the loaded module paths keyed by name.
  14. *
  15. * These are stored as the module paths are determined for convenient, fast access.
  16. *
  17. * @var array
  18. */
  19. private static $paths = array();
  20. /**
  21. * Parse a modularized identifier and return the module and key.
  22. *
  23. * Modular identifiers follow typically follow a {module}::{key} convention.
  24. * However, for convenience, the default module does not require a module qualifier.
  25. *
  26. * <code>
  27. * // Returns array('admin', 'test.example')
  28. * Module::parse('admin::test.example');
  29. *
  30. * // Returns array('application', 'test.example')
  31. * Module::parse('test.example');
  32. * </code>
  33. *
  34. * @param string $key
  35. * @return array
  36. */
  37. public static function parse($key)
  38. {
  39. $module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : DEFAULT_MODULE;
  40. $module = str_replace('.', '/', $module);
  41. if ($module !== DEFAULT_MODULE) $key = substr($key, strpos($key, ':') + 2);
  42. return array($module, $key);
  43. }
  44. /**
  45. * Get the path for a given module.
  46. *
  47. * Once the path has been determined, it will be cached by the class for quick access.
  48. *
  49. * @param string $module
  50. * @return string
  51. */
  52. public static function path($module)
  53. {
  54. if (array_key_exists($module, static::$paths)) return static::$paths[$module];
  55. if (in_array($module, static::$modules))
  56. {
  57. return static::$paths[$module] = MODULE_PATH.$module.'/';
  58. }
  59. }
  60. /**
  61. * Get the an array of paths to all of the modules.
  62. *
  63. * @return array
  64. */
  65. public static function paths()
  66. {
  67. return array_map(function($module) { return Laravel\Module::path($module); }, static::$modules);
  68. }
  69. }