ExtensionManager.php 7.0 KB

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