Extension.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * @return true if the extension has been installed or a string explaining
  50. * the problem.
  51. */
  52. public function install() {
  53. return true;
  54. }
  55. /**
  56. * Used when uninstalling an extension (e.g. revert the database scheme to
  57. * cancel changes from install).
  58. *
  59. * It must be redefined by child classes.
  60. *
  61. * @return true if the extension has been uninstalled or a string explaining
  62. * the problem.
  63. */
  64. public function uninstall() {
  65. return true;
  66. }
  67. /**
  68. * Call at the initialization of the extension (i.e. when the extension is
  69. * enabled by the extension manager).
  70. *
  71. * It must be redefined by child classes.
  72. */
  73. public function init() {}
  74. /**
  75. * Set the current extension to enable.
  76. */
  77. public function enable() {
  78. $this->is_enabled = true;
  79. }
  80. /**
  81. * Return if the extension is currently enabled.
  82. *
  83. * @return true if extension is enabled, false else.
  84. */
  85. public function isEnabled() {
  86. return $this->is_enabled;
  87. }
  88. /**
  89. * Return the content of the configure view for the current extension.
  90. *
  91. * @return the html content from ext_dir/configure.phtml, false if it does
  92. * not exist.
  93. */
  94. public function getConfigureView() {
  95. $filename = $this->path . '/configure.phtml';
  96. if (!file_exists($filename)) {
  97. return false;
  98. }
  99. ob_start();
  100. include($filename);
  101. return ob_get_clean();
  102. }
  103. /**
  104. * Handle the configure action.
  105. *
  106. * It must be redefined by child classes.
  107. */
  108. public function handleConfigureAction() {}
  109. /**
  110. * Getters and setters.
  111. */
  112. public function getName() {
  113. return $this->name;
  114. }
  115. public function getEntrypoint() {
  116. return $this->entrypoint;
  117. }
  118. public function getPath() {
  119. return $this->path;
  120. }
  121. public function getAuthor() {
  122. return $this->author;
  123. }
  124. public function getDescription() {
  125. return $this->description;
  126. }
  127. public function getVersion() {
  128. return $this->version;
  129. }
  130. public function getType() {
  131. return $this->type;
  132. }
  133. private function setType($type) {
  134. if (!in_array($type, self::$authorized_types)) {
  135. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  136. }
  137. $this->type = $type;
  138. }
  139. /**
  140. * Return the url for a given file.
  141. *
  142. * @param $filename name of the file to serve.
  143. * @param $type the type (js or css) of the file to serve.
  144. * @return the url corresponding to the file.
  145. */
  146. public function getFileUrl($filename, $type) {
  147. $dir = substr(strrchr($this->path, '/'), 1);
  148. $file_name_url = urlencode($dir . '/static/' . $filename);
  149. $absolute_path = $this->path . '/static/' . $filename;
  150. $mtime = @filemtime($absolute_path);
  151. $url = '/ext.php?f=' . $file_name_url .
  152. '&amp;t=' . $type .
  153. '&amp;' . $mtime;
  154. return Minz_Url::display($url, 'php');
  155. }
  156. /**
  157. * Register a controller in the Dispatcher.
  158. *
  159. * @param @base_name the base name of the controller. Final name will be:
  160. * FreshExtension_<base_name>_Controller.
  161. */
  162. public function registerController($base_name) {
  163. Minz_Dispatcher::registerController($base_name, $this->path);
  164. }
  165. /**
  166. * Register the views in order to be accessible by the application.
  167. */
  168. public function registerViews() {
  169. Minz_View::addBasePathname($this->path);
  170. }
  171. /**
  172. * Register i18n files from ext_dir/i18n/
  173. */
  174. public function registerTranslates() {
  175. $i18n_dir = $this->path . '/i18n';
  176. Minz_Translate::registerPath($i18n_dir);
  177. }
  178. /**
  179. * Register a new hook.
  180. *
  181. * @param $hook_name the hook name (must exist).
  182. * @param $hook_function the function name to call (must be callable).
  183. */
  184. public function registerHook($hook_name, $hook_function) {
  185. Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
  186. }
  187. }