Extension.php 4.9 KB

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