Extension.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  18. * The constructor to assign specific information to the extension.
  19. *
  20. * Available fields are:
  21. * - name: the name of the extension (required).
  22. * - entrypoint: the extension class name (required).
  23. * - path: the pathname to the extension files (required).
  24. * - author: the name and / or email address of the extension author.
  25. * - description: a short description to describe the extension role.
  26. * - version: a version for the current extension.
  27. * - type: "system" or "user" (default).
  28. *
  29. * It must not be redefined by child classes.
  30. *
  31. * @param $meta_info contains information about the extension.
  32. */
  33. public function __construct($meta_info) {
  34. $this->name = $meta_info['name'];
  35. $this->entrypoint = $meta_info['entrypoint'];
  36. $this->path = $meta_info['path'];
  37. $this->author = isset($meta_info['author']) ? $meta_info['author'] : '';
  38. $this->description = isset($meta_info['description']) ? $meta_info['description'] : '';
  39. $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1';
  40. $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user');
  41. }
  42. /**
  43. * Used when installing an extension (e.g. update the database scheme).
  44. *
  45. * It must be redefined by child classes.
  46. */
  47. public function install() {}
  48. /**
  49. * Used when uninstalling an extension (e.g. revert the database scheme to
  50. * cancel changes from install).
  51. *
  52. * It must be redefined by child classes.
  53. */
  54. public function uninstall() {}
  55. /**
  56. * Call at the initialization of the extension (i.e. when the extension is
  57. * enabled by the extension manager).
  58. *
  59. * It must be redefined by child classes.
  60. */
  61. public function init() {}
  62. /**
  63. * Getters and setters.
  64. */
  65. public function getName() {
  66. return $this->name;
  67. }
  68. public function getEntrypoint() {
  69. return $this->entrypoint;
  70. }
  71. public function getPath() {
  72. return $this->path;
  73. }
  74. public function getAuthor() {
  75. return $this->author;
  76. }
  77. public function getDescription() {
  78. return $this->description;
  79. }
  80. public function getVersion() {
  81. return $this->version;
  82. }
  83. public function getType() {
  84. return $this->type;
  85. }
  86. private function setType($type) {
  87. if (!in_array($type, self::$authorized_types)) {
  88. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  89. }
  90. $this->type = $type;
  91. }
  92. /**
  93. * Return the url for a given file.
  94. *
  95. * @param $filename name of the file to serve.
  96. * @param $type the type (js or css) of the file to serve.
  97. * @return the url corresponding to the file.
  98. */
  99. public function getFileUrl($filename, $type) {
  100. $dir = end(explode('/', $this->path));
  101. $file_name_url = urlencode($dir . '/static/' . $filename);
  102. $absolute_path = $this->path . '/static/' . $filename;
  103. $mtime = @filemtime($absolute_path);
  104. $url = '/ext.php?f=' . $file_name_url .
  105. '&amp;t=' . $type .
  106. '&amp;' . $mtime;
  107. return Minz_Url::display($url);
  108. }
  109. }