ExtensionManager.php 7.1 KB

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