Extension.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * Getters and setters.
  80. */
  81. public function getName() {
  82. return $this->name;
  83. }
  84. public function getEntrypoint() {
  85. return $this->entrypoint;
  86. }
  87. public function getPath() {
  88. return $this->path;
  89. }
  90. public function getAuthor() {
  91. return $this->author;
  92. }
  93. public function getDescription() {
  94. return $this->description;
  95. }
  96. public function getVersion() {
  97. return $this->version;
  98. }
  99. public function getType() {
  100. return $this->type;
  101. }
  102. private function setType($type) {
  103. if (!in_array($type, self::$authorized_types)) {
  104. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  105. }
  106. $this->type = $type;
  107. }
  108. /**
  109. * Return the url for a given file.
  110. *
  111. * @param $filename name of the file to serve.
  112. * @param $type the type (js or css) of the file to serve.
  113. * @return the url corresponding to the file.
  114. */
  115. public function getFileUrl($filename, $type) {
  116. $dir = end(explode('/', $this->path));
  117. $file_name_url = urlencode($dir . '/static/' . $filename);
  118. $absolute_path = $this->path . '/static/' . $filename;
  119. $mtime = @filemtime($absolute_path);
  120. $url = '/ext.php?f=' . $file_name_url .
  121. '&amp;t=' . $type .
  122. '&amp;' . $mtime;
  123. return Minz_Url::display($url);
  124. }
  125. /**
  126. * Register a controller in the Dispatcher.
  127. *
  128. * @param @base_name the base name of the controller. Final name will be:
  129. * FreshExtension_<base_name>_Controller.
  130. */
  131. public function registerController($base_name) {
  132. Minz_Dispatcher::registerController($base_name, $this->path);
  133. }
  134. /**
  135. * Register the views in order to be accessible by the application.
  136. */
  137. public function registerViews() {
  138. Minz_View::addBasePathname($this->path);
  139. }
  140. /**
  141. * Register a new hook.
  142. *
  143. * @param $hook_name the hook name (must exist).
  144. * @param $hook_function the function name to call (must be callable).
  145. */
  146. public function registerHook($hook_name, $hook_function) {
  147. Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
  148. }
  149. }