Extension.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * The extension base class.
  4. */
  5. class Minz_Extension {
  6. private $name;
  7. private $entrypoint;
  8. private $path;
  9. private $author;
  10. private $description;
  11. private $version;
  12. private $type;
  13. public static $authorized_types = array(
  14. 'system',
  15. 'user',
  16. );
  17. private $is_enabled;
  18. /**
  19. * The constructor to assign specific information to the extension.
  20. *
  21. * Available fields are:
  22. * - name: the name of the extension (required).
  23. * - entrypoint: the extension class name (required).
  24. * - path: the pathname to the extension files (required).
  25. * - author: the name and / or email address of the extension author.
  26. * - description: a short description to describe the extension role.
  27. * - version: a version for the current extension.
  28. * - type: "system" or "user" (default).
  29. *
  30. * It must not be redefined by child classes.
  31. *
  32. * @param $meta_info contains information about the extension.
  33. */
  34. public function __construct($meta_info) {
  35. $this->name = $meta_info['name'];
  36. $this->entrypoint = $meta_info['entrypoint'];
  37. $this->path = $meta_info['path'];
  38. $this->author = isset($meta_info['author']) ? $meta_info['author'] : '';
  39. $this->description = isset($meta_info['description']) ? $meta_info['description'] : '';
  40. $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1';
  41. $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user');
  42. $this->is_enabled = false;
  43. }
  44. /**
  45. * Used when installing an extension (e.g. update the database scheme).
  46. *
  47. * It must be redefined by child classes.
  48. */
  49. public function install() {}
  50. /**
  51. * Used when uninstalling an extension (e.g. revert the database scheme to
  52. * cancel changes from install).
  53. *
  54. * It must be redefined by child classes.
  55. */
  56. public function uninstall() {}
  57. /**
  58. * Call at the initialization of the extension (i.e. when the extension is
  59. * enabled by the extension manager).
  60. *
  61. * It must be redefined by child classes.
  62. */
  63. public function init() {}
  64. /**
  65. * Set the current extension to enable.
  66. */
  67. public function enable() {
  68. $this->is_enabled = true;
  69. }
  70. /**
  71. * Return if the extension is currently enabled.
  72. *
  73. * @return true if extension is enabled, false else.
  74. */
  75. public function is_enabled() {
  76. return $this->is_enabled;
  77. }
  78. /**
  79. * Return the content of the configure view for the current extension.
  80. *
  81. * @return the html content from ext_dir/configure.phtml, false if it does
  82. * not exist.
  83. */
  84. public function getConfigureView() {
  85. $filename = $this->path . '/configure.phtml';
  86. if (!file_exists($filename)) {
  87. return false;
  88. }
  89. return @file_get_contents($filename);
  90. }
  91. /**
  92. * Handle the configure POST action.
  93. *
  94. * It must be redefined by child classes.
  95. */
  96. public function handleConfigureAction() {}
  97. /**
  98. * Getters and setters.
  99. */
  100. public function getName() {
  101. return $this->name;
  102. }
  103. public function getEntrypoint() {
  104. return $this->entrypoint;
  105. }
  106. public function getPath() {
  107. return $this->path;
  108. }
  109. public function getAuthor() {
  110. return $this->author;
  111. }
  112. public function getDescription() {
  113. return $this->description;
  114. }
  115. public function getVersion() {
  116. return $this->version;
  117. }
  118. public function getType() {
  119. return $this->type;
  120. }
  121. private function setType($type) {
  122. if (!in_array($type, self::$authorized_types)) {
  123. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  124. }
  125. $this->type = $type;
  126. }
  127. /**
  128. * Return the url for a given file.
  129. *
  130. * @param $filename name of the file to serve.
  131. * @param $type the type (js or css) of the file to serve.
  132. * @return the url corresponding to the file.
  133. */
  134. public function getFileUrl($filename, $type) {
  135. $dir = end(explode('/', $this->path));
  136. $file_name_url = urlencode($dir . '/static/' . $filename);
  137. $absolute_path = $this->path . '/static/' . $filename;
  138. $mtime = @filemtime($absolute_path);
  139. $url = '/ext.php?f=' . $file_name_url .
  140. '&amp;t=' . $type .
  141. '&amp;' . $mtime;
  142. return Minz_Url::display($url);
  143. }
  144. /**
  145. * Register a controller in the Dispatcher.
  146. *
  147. * @param @base_name the base name of the controller. Final name will be:
  148. * FreshExtension_<base_name>_Controller.
  149. */
  150. public function registerController($base_name) {
  151. Minz_Dispatcher::registerController($base_name, $this->path);
  152. }
  153. /**
  154. * Register the views in order to be accessible by the application.
  155. */
  156. public function registerViews() {
  157. Minz_View::addBasePathname($this->path);
  158. }
  159. /**
  160. * Register a new hook.
  161. *
  162. * @param $hook_name the hook name (must exist).
  163. * @param $hook_function the function name to call (must be callable).
  164. */
  165. public function registerHook($hook_name, $hook_function) {
  166. Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
  167. }
  168. }