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