-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.php
338 lines (285 loc) · 8.14 KB
/
package.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php //-->
/**
* This file is part of the Incept Project.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
use Incept\Framework\Schema;
use UGComponents\IO\Request\RequestInterface;
use UGComponents\IO\Response\ResponseInterface;
/**
* $ incept inceptphp/packages ...
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
$this('event')->on('inceptphp/packages', function (
RequestInterface $request,
ResponseInterface $response
) {
$event = 'help';
if($request->hasStage(0)) {
$event = $request->getStage(0);
$request->removeStage(0);
}
if($request->hasStage()) {
$data = [];
$stage = $request->getStage();
foreach($stage as $key => $value) {
if(!is_numeric($key)) {
$data[$key] = $value;
} else {
$data[$key - 1] = $value;
}
$request->removeStage($key);
}
$request->setStage($data);
}
$name = 'inceptphp/packages';
$event = sprintf('%s-%s', $name, $event);
$this('event')->emit($event, $request, $response);
});
/**
* $ incept inceptphp/packages help
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
$this('event')->on('inceptphp/packages-help', function (
RequestInterface $request,
ResponseInterface $response
) {
$this('terminal')
->warning('Package Commands:')
->output(PHP_EOL)
->success('incept inceptphp/packages install')
->info(' Installs this package')
->output(PHP_EOL)
->success('incept inceptphp/packages update')
->info(' Updates this package')
->output(PHP_EOL)
->success('incept inceptphp/packages uninstall')
->info(' Removes this package')
->output(PHP_EOL);
});
/**
* $ incept inceptphp/packages install
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
$this('event')->on('inceptphp/packages-install', function (
RequestInterface $request,
ResponseInterface $response
) {
//if there's already an error
if ($response->isError()) {
//dont continue
return;
}
//load some packages
$config = $this('config');
$terminal = $this('terminal');
//custom name of this package
$name = 'inceptphp/packages';
if ($request->hasStage('name')) {
$name = $request->getStage('name');
}
//get the package
$package = $config->get('packages', $name);
//get the current version
$current = null;
// if version is set
if (is_array($package) && isset($package['version'])) {
// get the current version
$current = $package['version'];
}
//if it's already installed
if ($current) {
return $terminal->error(sprintf('%s is already installed', $name), false);
}
// install package
$version = '0.0.0';
//if there's an install package
if ($request->hasStage('install')) {
//run it through the install method
$install = $request->getStage('install');
$version = $this($name)->install($install, '0.0.0');
}
// update the config
$config->set('packages', $name, [
'version' => $version,
'active' => true,
'locked' => false
]);
$terminal->success(sprintf('%s %s installed', $name, $version));
$response->setError(false);
});
/**
* $ incept inceptphp/packages update
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
$this('event')->on('inceptphp/packages-update', function (
RequestInterface $request,
ResponseInterface $response
) {
//if there's already an error
if ($response->isError()) {
//dont continue
return;
}
//load some packages
$config = $this('config');
$terminal = $this('terminal');
//custom name of this package
$name = 'inceptphp/packages';
if ($request->hasStage('name')) {
$name = $request->getStage('name');
}
//get the current version
$current = $config->get('packages', $name);
// if version is set
if (is_array($current) && isset($current['version'])) {
// get the current version
$current = $current['version'];
} else {
$current = null;
}
//if it's not installed
if (!$current) {
$message = sprintf('%s is not installed', $name);
return $terminal->error($message, false);
}
// get available version
$version = $current;
//if there's an install package
if ($request->hasStage('install')) {
//run it through the install method
$install = $request->getStage('install');
$version = $this($name)->version($install);
//if available < current
if (version_compare($version, $current, '<')) {
$message = sprintf('%s - %s < %s', $name, $version, $current);
return $terminal->error($message, false);
//if available = current
} else if (version_compare($version, $current, '=')) {
$message = sprintf('%s - %s = %s', $name, $version, $current);
return $terminal->error($message, false);
}
// update package
$version = $this($name)->install($install, $current);
}
//if available = current
if (version_compare($version, $current, '=')) {
$message = sprintf('%s - %s = %s', $name, $version, $current);
return $terminal->error($message, false);
}
// update the config
$config->set('packages', $name, [
'version' => $version,
'active' => true,
'locked' => false
]);
$terminal->success(sprintf('%s updated to %s', $name, $version));
$response->setError(false);
});
/**
* $ incept inceptphp/packages uninstall
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
$this('event')->on('inceptphp/packages-uninstall', function (
RequestInterface $request,
ResponseInterface $response
) {
//if there's already an error
if ($response->isError()) {
//dont continue
return;
}
//load some packages
$config = $this('config');
$emitter = $this('event');
$terminal = $this('terminal');
//custom name of this package
$name = 'inceptphp/packages';
if ($request->hasStage('name')) {
$name = $request->getStage('name');
}
// if it's not installed
if (!$config->get('packages', $name)) {
$message = sprintf('%s is not installed', $name);
return $terminal->error($message, false);
}
//if there is a schema folder
if ($request->hasStage('schema')) {
$schema = $request->getStage('schema');
//scan through each file in the schema folder
foreach (scandir($schema) as $file) {
//if it's not a php file
if(substr($file, -4) !== '.php') {
//skip
continue;
}
//get the original schema data (in this package)
$original = include sprintf('%s/%s', $schema, $file);
//get the schema in the project
$file = sprintf('%s/%s.php', Schema::getFolder(), $original['name']);
//if no schema file
if (!file_exists($file)) {
//there's nothing to remove
continue;
}
//get the data from schema in the project
$data = include $file;
//if the schema in the project is different
if ($original !== $data) {
//dont remove
$terminal->error(sprintf(
'%s schema could not be removed because it has changed',
$original['name']
), false);
continue;
}
//----------------------------//
// 1. Prepare Data
//setup a new RnR
$payload = $this->makePayload();
$payload['request']
->setStage('mode', 'permanent')
->setStage('schema', $data['name']);
//----------------------------//
// 2. Process Request
$emitter->emit(
'system-schema-remove',
$payload['request'],
$payload['response']
);
//----------------------------//
// 3. Interpret Results
if ($payload['response']->isError()) {
$terminal->error(sprintf(
'%s - %s',
$data['name'],
$payload['response']->getMessage()
), false);
continue;
}
$terminal->success(sprintf('%s was removed', $data['name']));
}
}
// get package config
$packages = $config->get('packages');
// remove package from config
if (isset($packages[$name])) {
unset($packages[$name]);
// update package config
$config->set('packages', $packages);
}
$terminal->success(sprintf('%s uninstalled', $name));
$response->setError(false);
});