-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkeletor.php
288 lines (249 loc) · 8.68 KB
/
Skeletor.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace dokuwiki\plugin\dev;
use RuntimeException;
/**
* This class holds basic information about a plugin or template and uses the skeleton files to
* create new plugin or template specific versions of them.
*
* This class does not write any files, but only provides the data for the actual file creation.
*/
class Skeletor
{
// FIXME this may change upstream we may want to update it via github action
const PLUGIN_TYPES = ['auth', 'admin', 'syntax', 'action', 'renderer', 'helper', 'remote', 'cli'];
const TYPE_PLUGIN = 'plugin';
const TYPE_TEMPLATE = 'template';
protected $type;
protected $base;
protected $author;
protected $desc;
protected $name;
protected $email;
protected $url;
protected $dir;
/** @var array The files to be created in the form of [path => content] */
protected $files = [];
/**
* Initialize the skeletor from provided data
*
* @param string $type
* @param string $base
* @param string $desc
* @param string $author
* @param string $email
* @param string $name
* @param string $url
*/
public function __construct($type, $base, $desc, $author, $email, $name = '', $url = '')
{
$this->type = $type;
$this->base = $base;
$this->desc = $desc;
$this->author = $author;
$this->email = $email;
$this->name = $name ?: ucfirst($base . ' ' . $type);
if ($type == self::TYPE_PLUGIN) {
$this->url = $url ?: 'https://www.dokuwiki.org/plugin:' . $base;
$this->dir = 'lib/plugins/' . $base;
} else {
$this->url = $url ?: 'https://www.dokuwiki.org/template:' . $base;
$this->dir = 'lib/tpl/' . $base;
}
}
/**
* Create an instance using an existing plugin or template directory
*
* @param string $dir
* @return Skeletor
*/
static public function fromDir($dir)
{
if (file_exists($dir . '/plugin.info.txt')) {
$type = self::TYPE_PLUGIN;
} elseif (file_exists($dir . '/template.info.txt')) {
$type = self::TYPE_TEMPLATE;
} else {
throw new RuntimeException('Not a plugin or template directory');
}
$data = file($dir . '/' . $type . '.info.txt', FILE_IGNORE_NEW_LINES);
$data = array_map(function ($item) {
return array_map('trim', sexplode(' ', $item, 2, ''));
}, $data);
$data = array_combine(array_column($data, 0), array_column($data, 1));
return new self($type, $data['base'], $data['desc'], $data['author'], $data['email'], $data['url']);
}
/**
* Return the files to be created
*
* @return array [path => content]
*/
public function getFiles()
{
return $this->files;
}
// region content creators
/**
* Add the basic files to the plugin
*/
public function addBasics()
{
$this->loadSkeleton('info.txt', $this->type . '.info.txt');
$this->loadSkeleton('README');
$this->loadSkeleton('LICENSE');
$this->loadSkeleton('_gitattributes', '.gitattributes');
}
/**
* Add another component to the plugin
*
* @param string $type
* @param string $component
*/
public function addComponent($type, $component = '', $options = [])
{
if ($this->type !== self::TYPE_PLUGIN) {
throw new RuntimeException('Components can only be added to plugins');
}
if (!in_array($type, self::PLUGIN_TYPES)) {
throw new RuntimeException('Invalid type ' . $type);
}
$plugin = $this->base;
if ($component) {
$path = $type . '/' . $component . '.php';
$class = $type . '_plugin_' . $plugin . '_' . $component;
$self = 'plugin_' . $plugin . '_' . $component;
} else {
$path = $type . '.php';
$class = $type . '_plugin_' . $plugin;
$self = 'plugin_' . $plugin;
}
if ($type === 'action') {
$replacements = $this->actionReplacements($options);
}
if ($type === 'renderer' && isset($options[0]) && $options[0] === 'Doku_Renderer_xhtml') {
$type = 'renderer_xhtml'; // different template then
}
$replacements['@@PLUGIN_COMPONENT_NAME@@'] = $class;
$replacements['@@SYNTAX_COMPONENT_NAME@@'] = $self;
$this->loadSkeleton($type . '.php', $path, $replacements);
}
/**
* Add test framework optionally with a specific test
*
* @param string $test Name of the Test to add
*/
public function addTest($test = '')
{
// pick a random day and time for the cron job
$cron = sprintf(
'%d %d %d * *',
random_int(0, 59),
random_int(0, 23),
random_int(1, 28)
);
$test = ucfirst($test);
$this->loadSkeleton('.github/workflows/dokuwiki.yml', '', ['@@CRON@@' => $cron]);
if ($test) {
$replacements = ['@@TEST@@' => $test];
$this->loadSkeleton('_test/StandardTest.php', '_test/' . $test . 'Test.php', $replacements);
} else {
$this->loadSkeleton('_test/GeneralTest.php');
}
}
/**
* Add configuration
*
* @param bool $translate if true the settings language file will be be added, too
*/
public function addConf($translate = false)
{
$this->loadSkeleton('conf/default.php');
$this->loadSkeleton('conf/metadata.php');
if ($translate) {
$this->loadSkeleton('lang/settings.php', 'lang/en/settings.php');
}
}
/**
* Add language
*
* Currently only english is added, theoretically this could also copy over the keys from an
* existing english language file.
*
* @param bool $conf if true the settings language file will be be added, too
*/
public function addLang($conf = false)
{
$this->loadSkeleton('lang/lang.php', 'lang/en/lang.php');
if ($conf) {
$this->loadSkeleton('lang/settings.php', 'lang/en/settings.php');
}
}
// endregion
/**
* Prepare the string replacements
*
* @param array $replacements override defaults
* @return array
*/
protected function prepareReplacements($replacements = [])
{
// defaults
$data = [
'@@AUTHOR_NAME@@' => $this->author,
'@@AUTHOR_MAIL@@' => $this->email,
'@@PLUGIN_NAME@@' => $this->base, // FIXME rename to @@PLUGIN_BASE@@
'@@PLUGIN_DESC@@' => $this->desc,
'@@PLUGIN_URL@@' => $this->url,
'@@PLUGIN_TYPE@@' => $this->type,
'@@INSTALL_DIR@@' => ($this->type == self::TYPE_PLUGIN) ? 'plugins' : 'tpl',
'@@DATE@@' => date('Y-m-d'),
];
// merge given overrides
return array_merge($data, $replacements);
}
/**
* Replacements needed for action components.
*
* @param string[] $event Event names to handle
* @return string[]
*/
protected function actionReplacements($events = [])
{
if (!$events) $events = ['EXAMPLE_EVENT'];
$register = '';
$handler = '';
$template = file_get_contents(__DIR__ . '/skel/action_handler.php');
foreach ($events as $event) {
$event = strtoupper($event);
$fn = 'handle' . str_replace('_', '', ucwords(strtolower($event), '_'));
$register .= ' $controller->register_hook(\'' . $event .
'\', \'AFTER|BEFORE\', $this, \'' . $fn . '\');' . "\n";
$handler .= str_replace(['@@EVENT@@', '@@HANDLER@@'], [$event, $fn], $template);
}
return [
'@@REGISTER@@' => rtrim($register, "\n"),
'@@HANDLERS@@' => rtrim($handler, "\n"),
];
}
/**
* Load a skeleton file, do the replacements and add it to the list of files
*
* @param string $skel Skeleton relative to the skel dir
* @param string $target File name in the final plugin/template, empty for same as skeleton
* @param array $replacements Non-default replacements to use
*/
protected function loadSkeleton($skel, $target = '', $replacements = [])
{
$replacements = $this->prepareReplacements($replacements);
if (!$target) $target = $skel;
$file = __DIR__ . '/skel/' . $skel;
if (!file_exists($file)) {
throw new RuntimeException('Skeleton file not found: ' . $skel);
}
$content = file_get_contents($file);
$this->files[$target] = str_replace(
array_keys($replacements),
array_values($replacements),
$content
);
}
}