ExtensionManager.php 7.2 KB

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