module.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * This exception is thrown when a module cannot be found.
  15. *
  16. * @package Core
  17. */
  18. class ModuleNotFoundException extends \FuelException { }
  19. /**
  20. * Handles all the loading, unloading and management of modules.
  21. *
  22. * @package Core
  23. */
  24. class Module
  25. {
  26. /**
  27. * @var array $modules Holds all the loaded module information.
  28. */
  29. protected static $modules = array();
  30. /**
  31. * Loads the given module. If a path is not given, then 'module_paths' is used.
  32. * It also accepts an array of modules as the first parameter.
  33. *
  34. * @param string|array $package The module name or array of modules.
  35. * @param string|null $path The path to the module
  36. * @return bool True on success
  37. * @throws ModuleNotFoundException
  38. */
  39. public static function load($module, $path = null)
  40. {
  41. if (is_array($module))
  42. {
  43. foreach ($module as $mod => $path)
  44. {
  45. if (is_numeric($mod))
  46. {
  47. $mod = $path;
  48. $path = null;
  49. }
  50. static::load($mod, $path);
  51. }
  52. return false;
  53. }
  54. if (static::loaded($module))
  55. {
  56. return;
  57. }
  58. // if no path is given, try to locate the module
  59. if ($path === null)
  60. {
  61. $paths = \Config::get('module_paths', array());
  62. if ( ! empty($paths))
  63. {
  64. foreach ($paths as $modpath)
  65. {
  66. if (is_dir($path = $modpath.strtolower($module).DS))
  67. {
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. // make sure the path exists
  74. if ( ! is_dir($path))
  75. {
  76. throw new ModuleNotFoundException("Module '$module' could not be found at '".\Fuel::clean_path($path)."'");
  77. }
  78. // determine the module namespace
  79. $ns = '\\'.ucfirst($module);
  80. // add the namespace to the autoloader
  81. \Autoloader::add_namespaces(array(
  82. $ns => $path.'classes'.DS,
  83. ), true);
  84. static::$modules[$module] = $path;
  85. return true;
  86. }
  87. /**
  88. * Unloads a module from the stack.
  89. *
  90. * @param string $module The module name
  91. * @return void
  92. */
  93. public static function unload($module)
  94. {
  95. // delete all routes for this module
  96. \Router::delete($module.'/(:any)');
  97. unset(static::$modules[$module]);
  98. }
  99. /**
  100. * Checks if the given module is loaded, if no module is given then
  101. * all loaded modules are returned.
  102. *
  103. * @param string|null $module The module name or null
  104. * @return bool|array Whether the module is loaded, or all modules
  105. */
  106. public static function loaded($module = null)
  107. {
  108. if ($module === null)
  109. {
  110. return static::$modules;
  111. }
  112. return array_key_exists($module, static::$modules);
  113. }
  114. /**
  115. * Checks if the given module exists.
  116. *
  117. * @param string $module The module name
  118. * @return bool|string Path to the module found, or false if not found
  119. */
  120. public static function exists($module)
  121. {
  122. if (array_key_exists($module, static::$modules))
  123. {
  124. return static::$modules[$module];
  125. }
  126. else
  127. {
  128. $paths = \Config::get('module_paths', array());
  129. foreach ($paths as $path)
  130. {
  131. if (is_dir($path.$module))
  132. {
  133. return $path.$module.DS;
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. }