ExtensionManager.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * An extension manager to load extensions present in EXTENSIONS_PATH.
  4. *
  5. * @todo see coding style for methods!!
  6. */
  7. class Minz_ExtensionManager {
  8. private static $ext_metaname = 'metadata.json';
  9. private static $ext_entry_point = 'extension.php';
  10. private static $ext_list = array();
  11. private static $ext_list_enabled = array();
  12. private static $ext_auto_enabled = array();
  13. // List of available hooks. Please keep this list sorted.
  14. private static $hook_list = array(
  15. 'entry_before_display' => array(), // function($entry) -> Entry | null
  16. 'entry_before_insert' => array(), // function($entry) -> Entry | null
  17. 'feed_before_insert' => array(), // function($feed) -> Feed | null
  18. );
  19. private static $ext_to_hooks = array();
  20. /**
  21. * Initialize the extension manager by loading extensions in EXTENSIONS_PATH.
  22. *
  23. * A valid extension is a directory containing metadata.json and
  24. * extension.php files.
  25. * metadata.json is a JSON structure where the only required fields are
  26. * `name` and `entry_point`.
  27. * extension.php should contain at least a class named <name>Extension where
  28. * <name> must match with the entry point in metadata.json. This class must
  29. * inherit from Minz_Extension class.
  30. */
  31. public static function init() {
  32. $list_potential_extensions = array_values(array_diff(
  33. scandir(EXTENSIONS_PATH),
  34. array('..', '.')
  35. ));
  36. $system_conf = Minz_Configuration::get('system');
  37. self::$ext_auto_enabled = $system_conf->extensions_enabled;
  38. foreach ($list_potential_extensions as $ext_dir) {
  39. $ext_pathname = EXTENSIONS_PATH . '/' . $ext_dir;
  40. $metadata_filename = $ext_pathname . '/' . self::$ext_metaname;
  41. // Try to load metadata file.
  42. if (!file_exists($metadata_filename)) {
  43. // No metadata file? Invalid!
  44. continue;
  45. }
  46. $meta_raw_content = file_get_contents($metadata_filename);
  47. $meta_json = json_decode($meta_raw_content, true);
  48. if (!$meta_json || !self::is_valid_metadata($meta_json)) {
  49. // metadata.json is not a json file? Invalid!
  50. // or metadata.json is invalid (no required information), invalid!
  51. Minz_Log::warning('`' . $metadata_filename . '` is not a valid metadata file');
  52. continue;
  53. }
  54. $meta_json['path'] = $ext_pathname;
  55. // Try to load extension itself
  56. $extension = self::load($meta_json);
  57. if (!is_null($extension)) {
  58. self::register($extension);
  59. }
  60. }
  61. }
  62. /**
  63. * Indicates if the given parameter is a valid metadata array.
  64. *
  65. * Required fields are:
  66. * - `name`: the name of the extension
  67. * - `entry_point`: a class name to load the extension source code
  68. * If the extension class name is `TestExtension`, entry point will be `Test`.
  69. * `entry_point` must be composed of alphanumeric characters.
  70. *
  71. * @param $meta is an array of values.
  72. * @return true if the array is valid, false else.
  73. */
  74. public static function is_valid_metadata($meta) {
  75. return !(empty($meta['name']) ||
  76. empty($meta['entrypoint']) ||
  77. !ctype_alnum($meta['entrypoint']));
  78. }
  79. /**
  80. * Load the extension source code based on info metadata.
  81. *
  82. * @param $info an array containing information about extension.
  83. * @return an extension inheriting from Minz_Extension.
  84. */
  85. public static function load($info) {
  86. $entry_point_filename = $info['path'] . '/' . self::$ext_entry_point;
  87. $ext_class_name = $info['entrypoint'] . 'Extension';
  88. include($entry_point_filename);
  89. // Test if the given extension class exists.
  90. if (!class_exists($ext_class_name)) {
  91. Minz_Log::warning('`' . $ext_class_name .
  92. '` cannot be found in `' . $entry_point_filename . '`');
  93. return null;
  94. }
  95. // Try to load the class.
  96. $extension = null;
  97. try {
  98. $extension = new $ext_class_name($info);
  99. } catch (Minz_ExtensionException $e) {
  100. // We cannot load the extension? Invalid!
  101. Minz_Log::warning('In `' . $metadata_filename . '`: ' . $e->getMessage());
  102. return null;
  103. }
  104. // Test if class is correct.
  105. if (!($extension instanceof Minz_Extension)) {
  106. Minz_Log::warning('`' . $ext_class_name .
  107. '` is not an instance of `Minz_Extension`');
  108. return null;
  109. }
  110. return $extension;
  111. }
  112. /**
  113. * Add the extension to the list of the known extensions ($ext_list).
  114. *
  115. * If the extension is present in $ext_auto_enabled and if its type is "system",
  116. * it will be enabled in the same time.
  117. *
  118. * @param $ext a valid extension.
  119. */
  120. public static function register($ext) {
  121. $name = $ext->getName();
  122. self::$ext_list[$name] = $ext;
  123. if ($ext->getType() === 'system' &&
  124. in_array($name, self::$ext_auto_enabled)) {
  125. self::enable($ext->getName());
  126. }
  127. self::$ext_to_hooks[$name] = array();
  128. }
  129. /**
  130. * Enable an extension so it will be called when necessary.
  131. *
  132. * The extension init() method will be called.
  133. *
  134. * @param $ext_name is the name of a valid extension present in $ext_list.
  135. */
  136. public static function enable($ext_name) {
  137. if (isset(self::$ext_list[$ext_name])) {
  138. $ext = self::$ext_list[$ext_name];
  139. self::$ext_list_enabled[$ext_name] = $ext;
  140. $ext->enable();
  141. $ext->init();
  142. }
  143. }
  144. /**
  145. * Enable a list of extensions.
  146. *
  147. * @param $ext_list the names of extensions we want to load.
  148. */
  149. public static function enable_by_list($ext_list) {
  150. foreach ($ext_list as $ext_name) {
  151. self::enable($ext_name);
  152. }
  153. }
  154. /**
  155. * Return a list of extensions.
  156. *
  157. * @param $only_enabled if true returns only the enabled extensions (false by default).
  158. * @return an array of extensions.
  159. */
  160. public static function list_extensions($only_enabled = false) {
  161. if ($only_enabled) {
  162. return self::$ext_list_enabled;
  163. } else {
  164. return self::$ext_list;
  165. }
  166. }
  167. /**
  168. * Return an extension by its name.
  169. *
  170. * @param $ext_name the name of the extension.
  171. * @return the corresponding extension or null if it doesn't exist.
  172. */
  173. public static function find_extension($ext_name) {
  174. if (!isset(self::$ext_list[$ext_name])) {
  175. return null;
  176. }
  177. return self::$ext_list[$ext_name];
  178. }
  179. /**
  180. * Add a hook function to a given hook.
  181. *
  182. * The hook name must be a valid one. For the valid list, see self::$hook_list
  183. * array keys.
  184. *
  185. * @param $hook_name the hook name (must exist).
  186. * @param $hook_function the function name to call (must be callable).
  187. * @param $ext the extension which register the hook.
  188. */
  189. public static function addHook($hook_name, $hook_function, $ext) {
  190. if (isset(self::$hook_list[$hook_name]) && is_callable($hook_function)) {
  191. self::$hook_list[$hook_name][] = $hook_function;
  192. self::$ext_to_hooks[$ext->getName()][] = $hook_name;
  193. }
  194. }
  195. /**
  196. * Call functions related to a given hook.
  197. *
  198. * The hook name must be a valid one. For the valid list, see self::$hook_list
  199. * array keys.
  200. *
  201. * @param $hook_name the hook to call.
  202. * @param additionnal parameters (for signature, please see self::$hook_list comments)
  203. * @todo hook functions will have different signatures. So the $res = func($args);
  204. * $args = $res; will not work for all of them in the future. We must
  205. * find a better way to call hooks.
  206. */
  207. public static function callHook($hook_name) {
  208. $args = func_get_args();
  209. unset($args[0]);
  210. $result = $args[1];
  211. foreach (self::$hook_list[$hook_name] as $function) {
  212. $result = call_user_func_array($function, $args);
  213. if (is_null($result)) {
  214. break;
  215. }
  216. $args = $result;
  217. }
  218. return $result;
  219. }
  220. }