-
Notifications
You must be signed in to change notification settings - Fork 10
/
Templater.php
265 lines (237 loc) · 11 KB
/
Templater.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
<?php
namespace RedcapConHack\Templater;
class Templater extends \ExternalModules\AbstractExternalModule
{
const DEFAULT_FRAMEWORK_VERSION = 10;
const DEFAULT_MODULE_VERSION = "0.0.0";
function generateTemplateFromPost($twig)
{
# build $data array from $_POST array so Twig can render our files
$hookInfo = self::getHookInfo();
$data = [
'moduleName' => $_POST['moduleName'],
'className' => $_POST['className'],
'everyPageHooks' => $_POST['everyPageHooks'],
'namespace' => $_POST['namespace'],
'description' => $_POST['moduleDescription'],
'dirName' => $_POST['dirName'],
'OrgName' => $_POST['orgName'],
'frameworkVersion' => $_POST['frameworkVersion'] ?: $this::DEFAULT_FRAMEWORK_VERSION,
'authors' => [],
'controlCenterLinks' => [],
'crons' => [],
'hooks' => [],
'projectLinks' => [],
'includeGitInit' => isset($_POST['includeGitInit']) && $_POST['includeGitInit'] == 'on',
'gitOrg' => $_POST['gitOrg'],
'gitRepo' => $_POST['gitRepo'],
'includeGitIgnore' => isset($_POST['includeGitIgnore']) && $_POST['includeGitIgnore'] == 'on',
'includeEditorConfig' => isset($_POST['includeEditorConfig']) && $_POST['includeEditorConfig'] == 'on',
'Year' => date('Y')
];
if (strpos($data['namespace'], $data['className']) === false) {
$data['namespace'] = $data['namespace'] . "\\" . $data['className'];
}
$data['initialVersion'] = empty($_POST['moduleInitVersion']) ? self::DEFAULT_MODULE_VERSION : $_POST['moduleInitVersion'];
// determine directory name via given class name
if (empty($_POST['dirName'])) {
preg_match_all('/([A-Z]*[a-z]*)/', $data['className'], $matches);
array_pop($matches[0]);
$data['dirName'] = join('_', array_map('strtolower', $matches[0])) . '_v' . $data['initialVersion'];
}
# authors
$done = false;
$i = 1;
while (!$done) {
if (isset($_POST["authorsName$i"])) {
$data['authors'][$i] = [
'name' => $_POST["authorsName$i"],
'email' => $_POST["authorsEmail$i"],
'org' => $_POST["authorsOrg$i"]
];
} else {
$done = true;
}
$i++;
}
# hooks
foreach (array_merge($hookInfo['redcap'], $hookInfo['exmod']) as $hook) {
if (isset($_POST[$hook['name']])) {
array_push($data['hooks'], $hook);
}
}
# links
$done = false;
$i = 1;
while (!$done) {
if (isset($_POST["linksName$i"])) {
# add link to twig $data variable
$link = [
'name' => $_POST["linksName$i"],
'url' => $_POST["linksUrl$i"],
'icon' => $_POST["linksIcon$i"]
];
if (isset($_POST["linksNOAUTH$i"])) {
$link['NOAUTH'] = true;
}
if (isset($_POST["linksControlCenterCheckbox$i"])) {
array_push($data['controlCenterLinks'], $link);
}
if (isset($_POST["linksProjectCheckbox$i"])) {
array_push($data['projectLinks'], $link);
}
} else {
$done = true;
}
$i++;
}
# crons
$done = false;
$i = 1;
while (!$done) {
if (isset($_POST["cronsName$i"]) && ($_POST["cronsName$i"] != "")) {
$data['crons'][$i] = [
'name' => $_POST["cronsName$i"],
'desc' => $_POST["cronsDescription$i"],
'method' => $_POST["cronsMethod$i"],
'freq' => $_POST["cronsFrequency$i"],
'max' => $_POST["cronsMaxRunTime$i"]
];
} else {
$done = true;
}
$i++;
}
// // uncomment to test print a page to browser
// header('content-type: text/plain');
// // print_r($data);
// echo $twig->render('class.twig', $data);
// exit;
# render necessary files
$classFile = $twig->render('class.twig', $data);
$configFile = $twig->render('config.twig', $data);
$readmeFile = $twig->render('README.twig', $data);
# create zip file, open it, add files, close zip, and send
$zip = new \ZipArchive();
$file = tempnam(EDOC_PATH, "");
$zip->open($file, \ZipArchive::CREATE);
$zip->addFromString($data['className'] . '.php', $classFile);
$zip->addFromString('config.json', $configFile);
$zip->addFromString('README.md', $readmeFile);
# add method files for links and crons? (e.g., generate_template.php)
foreach ($data["projectLinks"] as $thisLink) {
$zip->addFromString($thisLink["url"], "<?php\nnamespace " . $data["namespace"] . ";\n/** @var \$module " . $data["className"] . " */\n");
}
foreach ($data["controlCenterLinks"] as $thisLink) {
$zip->addFromString($thisLink["url"], "<?php\nnamespace " . $data["namespace"] . ";\n/** @var \$module " . $data["className"] . " */\n");
}
# add LICENSE?
if (isset($_POST['includeLicense']) and isset($_POST['licenseText'])) {
$licenseText = htmlspecialchars($_POST['licenseText'], ENT_QUOTES);
$twigTemplate = twig_template_from_string($twig, $licenseText);
$license = $twigTemplate->render($data);
$zip->addFromString('LICENSE', $license);
}
# git init
if ($data['includeGitInit']) {
$gitInitFile = $twig->render('gitInit.twig', $data);
$zip->addFromString('gitInit.sh', $gitInitFile);
}
# add .gitignore
if ($data['includeGitIgnore']) {
$gitIgnoreFile = $twig->render('gitIgnore.twig', $data);
$zip->addFromString('.gitignore', $gitIgnoreFile);
}
# add editorconfig
if ($data['includeEditorConfig']) {
$editorConfigFile = $twig->render('editorConfig.twig', $data);
$zip->addFromString('.editorConfig', $editorConfigFile);
}
$zip->close();
$zipFileName = $data['dirName'] . '.zip';
header("Content-disposition: attachment; filename=$zipFileName");
header('Content-type: application/zip');
header('Content-transfer-encoding: binary');
header("Content-length: " . filesize($file));
header("Pragma: no-cache");
header("Expires: 0");
readfile($file);
unlink($file);
}
public static function getHookInfo()
{
# Get array of Hook methods and their attributes
$temp = \PluginDocs::getPluginMethods(\PluginDocs::HOOKS_CLASS);
$hooks = [
"redcap" => [],
"exmod" => [
"1" => [
"name" => "redcap_module_system_enable",
"description" => "Triggered when a module gets enabled on Control Center.",
"function" => "void <b>redcap_module_system_enable</b> ( <b>\$version</b> )"
],
"2" => [
"name" => "redcap_module_system_disable",
"description" => "Triggered when a module gets disabled on Control Center.",
"function" => "void <b>redcap_module_system_disable</b> ( <b>\$version</b> )"
],
"3" => [
"name" => "redcap_module_system_change_version",
"description" => "Triggered when a module version is changed.",
"function" => "void <b>redcap_module_system_change_version</b> ( <b>\$version, \$old_version</b> )"
],
"4" => [
"name" => "redcap_module_project_enable",
"description" => "Triggered when a module gets enabled on a specific project.",
"function" => "void <b>redcap_module_project_enable</b> ( <b>\$version, \$project_id</b> )"
],
"5" => [
"name" => "redcap_module_project_disable",
"description" => "Triggered when a module gets disabled on a specific project.",
"function" => "void <b>redcap_module_project_disable</b> ( <b>\$version, \$project_id</b> )"
],
"6" => [
"name" => "redcap_module_configure_button_display",
"description" => "Triggered when each enabled module defined is rendered. Return <code>null</code> if you don't want to display the Configure button and <code>true</code> to display.",
"function" => "void <b>redcap_module_configure_button_display</b> ( )"
],
"7" => [
"name" => "redcap_module_link_check_display",
"description" => "Triggered when each link defined in config.json is rendered. Override this method and return <code>null</code> if you don't want to display the link, or modify and return the <code>\$link</code> parameter as desired. This method also controls whether pages will load if users access their URLs directly.",
"function" => "void <b>redcap_module_link_check_display</b> ( <b>\$project_id, \$link</b> )"
],
"8" => [
"name" => "redcap_module_save_configuration",
"description" => "Triggered after a module configuration is saved.",
"function" => "void <b>redcap_module_save_configuration</b> ( <b>\$project_id</b> )"
]
]
];
$i = 1;
foreach ($temp as $name => $info) {
$hooks['redcap']["$i"] = [
"name" => $name,
"description" => $info['SUMMARY'],
"function" => $info['DESCRIPTION']
];
$i++;
}
$signatureFixes = array(
"void" => ""
);
foreach ($hooks as $setName => $set) {
foreach ($set as $hookName => $hook) {
preg_match('/\(.*\)/', $hook['function'], $matches);
$args = strip_tags($matches[0]);
$args = str_replace(array_keys($signatureFixes), array_values($signatureFixes), $args);
## int varName = NULL is invalid syntax, so remove
$args = preg_replace('/int (\$[a-zA-Z0-9\_]+) \= NULL/', "\\1", $args);
$args = preg_replace('/int (\$[a-zA-Z0-9\_]+) \= 1/', "\\1", $args);
$args = preg_replace('/string (\$[a-zA-Z0-9\_]+) \= NULL/', "\\1", $args);
$args = preg_replace('/array (\$[a-zA-Z0-9\_]+) \= NULL/', "\\1", $args);
$hooks[$setName][$hookName]['args'] = $args;
}
}
return $hooks;
}
}