You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

179 lines
3.9 KiB

  1. <?php
  2. /**
  3. * Plugin object
  4. *
  5. * See details here: http://rockmongo.com/wiki/pluginDevelope
  6. *
  7. * @author Liu <q@yun4s.cn>
  8. */
  9. class RPlugin {
  10. private static $_plugins = array();
  11. private static $_loaded = false;
  12. public function onBefore() {
  13. }
  14. public function onAfter() {
  15. }
  16. /**
  17. * Read plugin help
  18. *
  19. */
  20. public function help() {
  21. return array(
  22. "name" => "Default Plugin",
  23. "author" => "rock",
  24. "version" => "1.0"
  25. );
  26. }
  27. /**
  28. * Register a plugin
  29. *
  30. * @param string $pluginClass plugin class name
  31. * @param integer $priority priority
  32. * @throws Exception
  33. */
  34. public static function register($pluginClass, $priority = -1) {
  35. if ($priority == -1) {
  36. $priority = count(self::$_plugins);
  37. }
  38. if (!is_subclass_of($pluginClass, "RPlugin")) {
  39. throw new Exception("plugin class '{$pluginClass}' must be inherited from RPlugin");
  40. }
  41. self::$_plugins[] = array( "obj" => new $pluginClass, "priority" => $priority );
  42. }
  43. /**
  44. * Call onBefore() method in plugin
  45. *
  46. */
  47. public static function callBefore() {
  48. $plugins = rock_array_sort(self::$_plugins, "priority");
  49. foreach ($plugins as $plugin) {
  50. $plugin["obj"]->onBefore();
  51. }
  52. }
  53. /**
  54. * Call onAfter() method in plugin
  55. *
  56. */
  57. public static function callAfter() {
  58. $plugins = rock_array_sort(self::$_plugins, "priority", false);
  59. foreach ($plugins as $plugin) {
  60. $plugin["obj"]->onAfter();
  61. }
  62. }
  63. /**
  64. * Load all of plugins
  65. *
  66. * You should put all plugins to app/plugins:
  67. * $ROCK-MONGO
  68. * apps/
  69. * plugins/
  70. * mapreduce/
  71. * ace/
  72. * systemjs/
  73. * other plugins ...
  74. *
  75. * But we also support another deploy way:
  76. * $ROCK-MONGO
  77. * apps/
  78. * plugins/
  79. * csv/
  80. * sharding/
  81. * other plugins ...
  82. */
  83. public static function load() {
  84. if (self::$_loaded) {
  85. return;
  86. }
  87. $plugins = array();
  88. require(__ROOT__ . DS . "configs" . DS . "rplugin.php");
  89. if (empty($plugins) || !is_array($plugins)) {
  90. return;
  91. }
  92. foreach ($plugins as $name => $plugin) {
  93. if ($plugin["enabled"]) {
  94. $dir = __ROOT__ . DS . "plugins" . DS . $name;
  95. if (!is_dir($dir)) {
  96. $dir = dirname(dirname(__ROOT__)) . DS . "plugins" . DS . $name;
  97. }
  98. $initFile = $dir . DS . "init.php";
  99. if (is_file($initFile)) {
  100. require $dir . DS . "init.php";
  101. }
  102. else {
  103. trigger_error("could not find initialize file '{$initFile}' for plugin '{$name}', you can disable it in app/configs/rplugin.php");
  104. }
  105. }
  106. }
  107. self::$_loaded = true;
  108. }
  109. /**
  110. * Get all plugins
  111. *
  112. * @return array
  113. * @since 1.1.6
  114. */
  115. public static function plugins() {
  116. $configPlugins = array();
  117. $plugins = array();
  118. require(__ROOT__ . DS . "configs" . DS . "rplugin.php");
  119. if (empty($plugins) || !is_array($plugins)) {
  120. return $configPlugins;
  121. }
  122. foreach ($plugins as $name => $plugin) {
  123. $dir = __ROOT__ . DS . "plugins" . DS . $name;
  124. if (!is_dir($dir)) {
  125. $dir = dirname(dirname(__ROOT__)) . DS . "plugins" . DS . $name;
  126. }
  127. $pluginConfig = array(
  128. "name" => null,
  129. "dir" => $name,
  130. "code" => null,
  131. "author" => null,
  132. "description" => null,
  133. "version" => null,
  134. "url" => null,
  135. "enabled" => isset($plugin["enabled"]) ? $plugin["enabled"] : false
  136. );
  137. $descFile = $dir . "/desc.php";
  138. if (is_file($descFile)) {
  139. $config = require($descFile);
  140. if (isset($config["name"])) {
  141. $pluginConfig["name"] = $config["name"];
  142. }
  143. if (isset($config["code"])) {
  144. $pluginConfig["code"] = $config["code"];
  145. }
  146. if (isset($config["author"])) {
  147. $pluginConfig["author"] = $config["author"];
  148. }
  149. if (isset($config["description"])) {
  150. $pluginConfig["description"] = $config["description"];
  151. }
  152. if (isset($config["version"])) {
  153. $pluginConfig["version"] = $config["version"];
  154. }
  155. if (isset($config["url"])) {
  156. $pluginConfig["url"] = $config["url"];
  157. }
  158. }
  159. $configPlugins[] = $pluginConfig;
  160. }
  161. return $configPlugins;
  162. }
  163. }
  164. ?>