ExtensionManager.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. $valid_chars = array('_');
  76. return !(empty($meta['name']) ||
  77. empty($meta['entrypoint']) ||
  78. !ctype_alnum(str_replace($valid_chars, '', $meta['entrypoint'])));
  79. }
  80. /**
  81. * Load the extension source code based on info metadata.
  82. *
  83. * @param $info an array containing information about extension.
  84. * @return an extension inheriting from Minz_Extension.
  85. */
  86. public static function load($info) {
  87. $entry_point_filename = $info['path'] . '/' . self::$ext_entry_point;
  88. $ext_class_name = $info['entrypoint'] . 'Extension';
  89. include($entry_point_filename);
  90. // Test if the given extension class exists.
  91. if (!class_exists($ext_class_name)) {
  92. Minz_Log::warning('`' . $ext_class_name .
  93. '` cannot be found in `' . $entry_point_filename . '`');
  94. return null;
  95. }
  96. // Try to load the class.
  97. $extension = null;
  98. try {
  99. $extension = new $ext_class_name($info);
  100. } catch (Minz_ExtensionException $e) {
  101. // We cannot load the extension? Invalid!
  102. Minz_Log::warning('In `' . $metadata_filename . '`: ' . $e->getMessage());
  103. return null;
  104. }
  105. // Test if class is correct.
  106. if (!($extension instanceof Minz_Extension)) {
  107. Minz_Log::warning('`' . $ext_class_name .
  108. '` is not an instance of `Minz_Extension`');
  109. return null;
  110. }
  111. return $extension;
  112. }
  113. /**
  114. * Add the extension to the list of the known extensions ($ext_list).
  115. *
  116. * If the extension is present in $ext_auto_enabled and if its type is "system",
  117. * it will be enabled in the same time.
  118. *
  119. * @param $ext a valid extension.
  120. */
  121. public static function register($ext) {
  122. $name = $ext->getName();
  123. self::$ext_list[$name] = $ext;
  124. if ($ext->getType() === 'system' &&
  125. in_array($name, self::$ext_auto_enabled)) {
  126. self::enable($ext->getName());
  127. }
  128. self::$ext_to_hooks[$name] = array();
  129. }
  130. /**
  131. * Enable an extension so it will be called when necessary.
  132. *
  133. * The extension init() method will be called.
  134. *
  135. * @param $ext_name is the name of a valid extension present in $ext_list.
  136. */
  137. public static function enable($ext_name) {
  138. if (isset(self::$ext_list[$ext_name])) {
  139. $ext = self::$ext_list[$ext_name];
  140. self::$ext_list_enabled[$ext_name] = $ext;
  141. $ext->enable();
  142. $ext->init();
  143. }
  144. }
  145. /**
  146. * Enable a list of extensions.
  147. *
  148. * @param $ext_list the names of extensions we want to load.
  149. */
  150. public static function enable_by_list($ext_list) {
  151. foreach ($ext_list as $ext_name) {
  152. self::enable($ext_name);
  153. }
  154. }
  155. /**
  156. * Return a list of extensions.
  157. *
  158. * @param $only_enabled if true returns only the enabled extensions (false by default).
  159. * @return an array of extensions.
  160. */
  161. public static function list_extensions($only_enabled = false) {
  162. if ($only_enabled) {
  163. return self::$ext_list_enabled;
  164. } else {
  165. return self::$ext_list;
  166. }
  167. }
  168. /**
  169. * Return an extension by its name.
  170. *
  171. * @param $ext_name the name of the extension.
  172. * @return the corresponding extension or null if it doesn't exist.
  173. */
  174. public static function find_extension($ext_name) {
  175. if (!isset(self::$ext_list[$ext_name])) {
  176. return null;
  177. }
  178. return self::$ext_list[$ext_name];
  179. }
  180. /**
  181. * Add a hook function to a given hook.
  182. *
  183. * The hook name must be a valid one. For the valid list, see self::$hook_list
  184. * array keys.
  185. *
  186. * @param $hook_name the hook name (must exist).
  187. * @param $hook_function the function name to call (must be callable).
  188. * @param $ext the extension which register the hook.
  189. */
  190. public static function addHook($hook_name, $hook_function, $ext) {
  191. if (isset(self::$hook_list[$hook_name]) && is_callable($hook_function)) {
  192. self::$hook_list[$hook_name][] = $hook_function;
  193. self::$ext_to_hooks[$ext->getName()][] = $hook_name;
  194. }
  195. }
  196. /**
  197. * Call functions related to a given hook.
  198. *
  199. * The hook name must be a valid one. For the valid list, see self::$hook_list
  200. * array keys.
  201. *
  202. * @param $hook_name the hook to call.
  203. * @param additionnal parameters (for signature, please see self::$hook_list comments)
  204. * @todo hook functions will have different signatures. So the $res = func($args);
  205. * $args = $res; will not work for all of them in the future. We must
  206. * find a better way to call hooks.
  207. */
  208. public static function callHook($hook_name) {
  209. $args = func_get_args();
  210. unset($args[0]);
  211. $result = $args[1];
  212. foreach (self::$hook_list[$hook_name] as $function) {
  213. $result = call_user_func_array($function, $args);
  214. if (is_null($result)) {
  215. break;
  216. }
  217. $args = $result;
  218. }
  219. return $result;
  220. }
  221. }