-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageTrait.php
195 lines (164 loc) · 4.06 KB
/
PackageTrait.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
<?php //-->
/**
* This file is part of the Incept Project.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
namespace Incept\Package;
use Incept\Framework\Framework;
/**
* General Package Methods
*
* @vendor Incept
* @package Package
* @standard PSR-2
*/
trait PackageTrait
{
/**
* Performs an install
*
* @param *string $path
* @param ?string $current
* @param ?string|null $type
*
* @return string The current version
*/
public function install(
string $path,
string $current = '0.0.0',
string $type = null
): string {
//collect and organize all the versions
$versions = [];
$files = scandir($path);
foreach ($files as $file) {
if ($file === '.' || $file === '..' || is_dir($path . '/' . $file)) {
continue;
}
//get extension
$extension = pathinfo($file, PATHINFO_EXTENSION);
//valid extensions
if (!in_array($extension, ['php', 'sh', 'sql'])) {
continue;
}
//only run updates on a following type
if ($type && $type !== $extension) {
continue;
}
//get base as version
$version = pathinfo($file, PATHINFO_FILENAME);
//validate version
if (!preg_match('/^[0-9\.]+$/', $version)
|| version_compare($version, '0.0.1', '<')
) {
continue;
}
$versions[$version][] = [
'script' => $path . '/' . $file,
'mode' => $extension
];
}
if (empty($versions)) {
return '0.0.0';
}
//sort versions
uksort($versions, 'version_compare');
//prepare incase
$key = $this->handler->package('config')->get('settings', 'pdo');
$database = $this->handler->package('pdo')->get($key);
//now run the scripts in order of version
foreach ($versions as $version => $files) {
//if 0.0.0 >= 0.0.1
if (version_compare($current, $version, '>=')) {
continue;
}
//run the scripts
foreach ($files as $file) {
switch ($file['mode']) {
case 'php':
include $file['script'];
break;
case 'sql':
$query = file_get_contents($file['script']);
$database->query($query);
break;
case 'sh':
exec($file['script']);
break;
}
}
}
//if 0.0.0 < 0.0.1
if (version_compare($current, $version, '<')) {
$current = $version;
}
return $current;
}
/**
* Either returns the current available version
* or the next version
*
* @param *string $path
* @param bool $next
*
* @return string
*/
public function version(string $path, bool $next = false): string
{
//collect and organize all the versions
$versions = [];
$files = scandir($path, 0);
foreach ($files as $file) {
if ($file === '.'
|| $file === '..'
|| is_dir($path . '/' . $file)
) {
continue;
}
//get extension
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension !== 'php'
&& $extension !== 'sh'
&& $extension !== 'sql'
) {
continue;
}
//get base as version
$version = pathinfo($file, PATHINFO_FILENAME);
//validate version
if (!preg_match('/^[0-9\.]+$/', $version)
|| version_compare($version, '0.0.1', '<')
) {
continue;
}
$versions[] = $version;
}
if (empty($versions)) {
return '0.0.0';
}
//sort versions
usort($versions, 'version_compare');
$version = array_pop($versions);
if (!$next) {
return $version;
}
$revisions = explode('.', $version);
$revisions = array_reverse($revisions);
$found = false;
foreach ($revisions as $i => $revision) {
if (!is_numeric($revision)) {
continue;
}
$revisions[$i]++;
$found = true;
break;
}
if (!$found) {
return $current . '.1';
}
$revisions = array_reverse($revisions);
return implode('.', $revisions);
}
}