Extension.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 getAuthor() {
  72. return $this->author;
  73. }
  74. public function getDescription() {
  75. return $this->description;
  76. }
  77. public function getVersion() {
  78. return $this->version;
  79. }
  80. public function getType() {
  81. return $this->type;
  82. }
  83. private function setType($type) {
  84. if (!in_array($type, self::$authorized_types)) {
  85. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  86. }
  87. $this->type = $type;
  88. }
  89. }