ExtensionManager.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * An extension manager to load extensions present in EXTENSIONS_PATH.
  4. */
  5. class Minz_ExtensionManager {
  6. private static $ext_metaname = 'metadata.json';
  7. private static $ext_entry_point = 'extension.php';
  8. private static $ext_list = array();
  9. private static $ext_list_enabled = array();
  10. private static $ext_auto_enabled = array();
  11. /**
  12. * Initialize the extension manager by loading extensions in EXTENSIONS_PATH.
  13. *
  14. * A valid extension is a directory containing metadata.json and
  15. * extension.php files.
  16. * metadata.json is a JSON structure where the only required fields are
  17. * `name` and `entry_point`.
  18. * extension.php should contain at least a class named <name>Extension where
  19. * <name> must match with the entry point in metadata.json. This class must
  20. * inherit from Minz_Extension class.
  21. */
  22. public static function init() {
  23. $list_potential_extensions = array_values(array_diff(
  24. scandir(EXTENSIONS_PATH),
  25. array('..', '.')
  26. ));
  27. self::$ext_auto_enabled = Minz_Configuration::extensionsEnabled();
  28. foreach ($list_potential_extensions as $ext_dir) {
  29. $ext_pathname = EXTENSIONS_PATH . '/' . $ext_dir;
  30. $metadata_filename = $ext_pathname . '/' . self::$ext_metaname;
  31. // Try to load metadata file.
  32. if (!file_exists($metadata_filename)) {
  33. // No metadata file? Invalid!
  34. continue;
  35. }
  36. $meta_raw_content = file_get_contents($metadata_filename);
  37. $meta_json = json_decode($meta_raw_content, true);
  38. if (!$meta_json || !self::is_valid_metadata($meta_json)) {
  39. // metadata.json is not a json file? Invalid!
  40. // or metadata.json is invalid (no required information), invalid!
  41. Minz_Log::warning('`' . $metadata_filename . '` is not a valid metadata file');
  42. continue;
  43. }
  44. $meta_json['path'] = $ext_pathname;
  45. // Try to load extension itself
  46. $extension = self::load($meta_json);
  47. if (!is_null($extension)) {
  48. self::register($extension);
  49. }
  50. }
  51. }
  52. /**
  53. * Indicates if the given parameter is a valid metadata array.
  54. *
  55. * Required fields are:
  56. * - `name`: the name of the extension
  57. * - `entry_point`: a class name to load the extension source code
  58. * If the extension class name is `TestExtension`, entry point will be `Test`.
  59. * `entry_point` must be composed of alphanumeric characters.
  60. *
  61. * @param $meta is an array of values.
  62. * @return true if the array is valid, false else.
  63. */
  64. public static function is_valid_metadata($meta) {
  65. return !(empty($meta['name']) ||
  66. empty($meta['entrypoint']) ||
  67. !ctype_alnum($meta['entrypoint']));
  68. }
  69. /**
  70. * Load the extension source code based on info metadata.
  71. *
  72. * @param $info an array containing information about extension.
  73. * @return an extension inheriting from Minz_Extension.
  74. */
  75. public static function load($info) {
  76. $entry_point_filename = $info['path'] . '/' . self::$ext_entry_point;
  77. $ext_class_name = $info['entrypoint'] . 'Extension';
  78. include($entry_point_filename);
  79. // Test if the given extension class exists.
  80. if (!class_exists($ext_class_name)) {
  81. Minz_Log::warning('`' . $ext_class_name .
  82. '` cannot be found in `' . $entry_point_filename . '`');
  83. return null;
  84. }
  85. // Try to load the class.
  86. $extension = null;
  87. try {
  88. $extension = new $ext_class_name($info);
  89. } catch (Minz_ExtensionException $e) {
  90. // We cannot load the extension? Invalid!
  91. Minz_Log::warning('In `' . $metadata_filename . '`: ' . $e->getMessage());
  92. return null;
  93. }
  94. // Test if class is correct.
  95. if (!($extension instanceof Minz_Extension)) {
  96. Minz_Log::warning('`' . $ext_class_name .
  97. '` is not an instance of `Minz_Extension`');
  98. return null;
  99. }
  100. return $extension;
  101. }
  102. /**
  103. * Add the extension to the list of the known extensions ($ext_list).
  104. *
  105. * If the extension is present in $ext_auto_enabled and if its type is "system",
  106. * it will be enabled in the same time.
  107. *
  108. * @param $ext a valid extension.
  109. */
  110. public static function register($ext) {
  111. $name = $ext->getName();
  112. self::$ext_list[$name] = $ext;
  113. if ($ext->getType() === 'system' &&
  114. in_array($name, self::$ext_auto_enabled)) {
  115. self::enable($ext->getName());
  116. }
  117. }
  118. /**
  119. * Enable an extension so it will be called when necessary.
  120. *
  121. * The extension init() method will be called.
  122. *
  123. * @param $ext_name is the name of a valid extension present in $ext_list.
  124. */
  125. public static function enable($ext_name) {
  126. if (isset(self::$ext_list[$ext_name])) {
  127. $ext = self::$ext_list[$ext_name];
  128. self::$ext_list_enabled[$ext_name] = $ext;
  129. $ext->init();
  130. }
  131. }
  132. }