-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.php
More file actions
188 lines (162 loc) · 5.76 KB
/
update.php
File metadata and controls
188 lines (162 loc) · 5.76 KB
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
<?php //-->
/**
* This file is part of the Cradle PHP Library.
* (c) 2016-2018 Openovate Labs
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
use Cradle\Framework\CommandLine;
use Cradle\Storm\SqlFactory;
use Cradle\Event\EventHandler;
/**
* CLI faucet installation
*
* @param Request $request
* @param Response $response
*/
return function ($request, $response) {
//schema should be writeable
$folder = $this->package('global')->path('config') . '/schema';
if (!is_dir($folder) || !is_writable($folder)) {
CommandLine::error(sprintf(
'%s is not writable Try `chmod -R 777 %s` first',
$folder,
$folder
));
}
//listen for install and update
$schemas = [];
$events = ['system-schema-create', 'system-schema-update'];
$this->on($events, function($request, $response) use (&$schemas) {
$schemas[] = $request->getStage('name');
});
//these are all the active packages
$active = $this->getPackages();
//these are the installed packages
$installed = $this->package('global')->config('packages');
$hasErrors = false;
foreach ($active as $name => $package) {
$type = $package->getPackageType();
//skip pseudo packages
if ($type === 'pseudo') {
continue;
}
//determine author/package
//if a vendor package
if ($type === 'vendor') {
list($vendor, $package) = explode('/', $name, 2);
} else {
//it's a root package
list($vendor, $package) = explode('/', substr($name, 1), 2);
}
//determine action
$action = 'install';
//if it's installed
if (isset($installed[$name]['version'])) {
$action = 'update';
}
if ($action === 'install') {
CommandLine::system(sprintf('Installing %s', $name));
} else {
CommandLine::system(sprintf('Updating %s', $name));
}
//trigger event
$event = sprintf('%s-%s-%s', $vendor, $package, $action);
$this->trigger($event, $request, $response);
//if no event was triggered
$status = $this->getEventHandler()->getMeta();
if($status === EventHandler::STATUS_NOT_FOUND) {
CommandLine::warning(sprintf('No actions needed on %s', $name));
continue;
}
$logs = $response->getResults('logs');
if (!empty($logs)) {
foreach ($logs as $package => $group) {
foreach ($group as $version => $messages) {
foreach ($messages as $log) {
CommandLine::$brand = '[' . $package . ' ' . $version . ']';
if (!isset($log['message'])) {
continue;
}
if (!isset($log['type'])) {
$log['type'] = 'info';
}
switch ($log['type']) {
case 'warning':
CommandLine::warning($log['message']);
break;
case 'error':
CommandLine::error($log['message'], false);
break;
case 'system':
case 'info':
default:
CommandLine::system($log['message']);
break;
}
}
}
}
CommandLine::$brand = '[cradle]';
$response->removeResults('logs');
}
//if error
if ($response->isError()) {
$message = sprintf('%s did not correctly install.', $name);
CommandLine::error($message, false);
$response->remove('json');
$hasErrors = true;
continue;
}
//if it's install
if ($action === 'install') {
$message = sprintf('Installed %s', $name);
if ($response->hasResults('version')) {
$message = sprintf(
'Installed %s to %s',
$name,
$response->getResults('version')
);
}
CommandLine::success($message, false);
continue;
}
//it's update
$message = sprintf('Updated %s', $name);
if ($response->hasResults('version')) {
$message = sprintf(
'Updated %s to %s',
$name,
$response->getResults('version')
);
}
CommandLine::success($message, false);
}
//deal with schemas without packages
$this->trigger('system-schema-search', $request, $response);
foreach ($response->getResults('rows') as $schema) {
//if this schema has already been installed/updated
if (in_array($schema['name'], $schemas)) {
//skip it
continue;
}
//run an update
$payload = $this->makePayload();
$this->method('system-schema-update', $schema, $payload['response']);
//if error
if ($payload['response']->isError()) {
CommandLine::error($payload['response']->getMessage(), false);
continue;
}
//it's updated
$message = sprintf('Updated schema %s', $schema['name']);
CommandLine::success($message, false);
}
if ($hasErrors) {
$message = 'There were some errors in the packages being installed/updated.';
CommandLine::error($message, false);
$response->setError(true, $message);
}
$response->setResults($schemas);
};