1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sfneal\Helpers\Laravel; |
4
|
|
|
|
5
|
|
|
use ErrorException; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Sfneal\Helpers\Strings\StringHelpers; |
8
|
|
|
|
9
|
|
|
class AppInfo |
10
|
|
|
{ |
11
|
|
|
// todo: add config values for version? |
12
|
|
|
// todo: add invalidate cache methods |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Redis Cache Key prefix. |
16
|
|
|
*/ |
17
|
|
|
private const CACHE_PREFIX = 'app'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Retrieve the Application's version. |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public static function version() |
25
|
|
|
{ |
26
|
|
|
return Cache::rememberForever(self::cacheKey('version'), function () { |
27
|
|
|
// toto: refactor environment method to AppInfo |
28
|
|
|
return trim(config('app-info.version')).(self::isEnvDevelopment() ? ' (dev)' : ''); |
29
|
|
|
}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Retrieve an array of changes made to a particular application version. |
34
|
|
|
* |
35
|
|
|
* @param string|null $version |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public static function versionChanges(string $version = null) |
40
|
|
|
{ |
41
|
|
|
// Get current version changes if $version wasn't passed |
42
|
|
|
$version = $version ?? self::version(); |
43
|
|
|
|
44
|
|
|
return Cache::rememberForever(self::cacheKey('changelog', $version), function () use ($version) { |
45
|
|
|
try { |
46
|
|
|
return self::changelog()[$version]; |
47
|
|
|
} catch (ErrorException $exception) { |
|
|
|
|
48
|
|
|
} |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Retrieve the Application's changelog. |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public static function changelog() |
58
|
|
|
{ |
59
|
|
|
return Cache::rememberForever(self::cacheKey('changelog'), function () { |
60
|
|
|
// Read the changelog |
61
|
|
|
$file_lines = file(config('app-info.changelog_path')); |
62
|
|
|
$changes = []; |
63
|
|
|
|
64
|
|
|
for ($row = 0; $row < count($file_lines); $row++) { |
|
|
|
|
65
|
|
|
// Check if line starts with 'version' |
66
|
|
|
if (substr(strtolower($file_lines[$row]), 0, strlen('version')) == 'version') { |
67
|
|
|
$version_date = explode(', ', $file_lines[$row]); |
68
|
|
|
|
69
|
|
|
// Extract version and date |
70
|
|
|
$date = $version_date[1]; |
71
|
|
|
$version = explode(' ', $version_date[0])[1]; |
72
|
|
|
$changes[$version] = ['date' => trim($date), 'changes' => []]; |
73
|
|
|
|
74
|
|
|
// Skip ahead two lines to skip sep line |
75
|
|
|
$row += 2; |
76
|
|
|
|
77
|
|
|
// Keep iterating over rows until we get to a blank line |
78
|
|
|
while ($row < count($file_lines) && strlen(trim($file_lines[$row])) > 1) { |
79
|
|
|
if (substr(trim($file_lines[$row]), 0, 1) == '-') { |
80
|
|
|
$changes[$version]['changes'][] = str_replace('- ', '', trim($file_lines[$row])); |
81
|
|
|
$row++; |
82
|
|
|
} else { |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $changes; |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Determine if a particular Version is running. |
95
|
|
|
* |
96
|
|
|
* @param string $version |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public static function isVersion(string $version): bool |
101
|
|
|
{ |
102
|
|
|
return self::version() == $version; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determine if a particular Version is running. |
107
|
|
|
* |
108
|
|
|
* @param string $version |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public static function isNotVersion(string $version): bool |
113
|
|
|
{ |
114
|
|
|
return ! self::isVersion($version); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine if a 'beta' version is running. |
119
|
|
|
* |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public static function isVersionTagBeta(): bool |
123
|
|
|
{ |
124
|
|
|
return self::isVersionTag('beta'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Determine if a 'dev' version is running. |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public static function isVersionTagDev(): bool |
133
|
|
|
{ |
134
|
|
|
return self::isVersionTag('dev'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Determine if a particular tag version is running (beta, dev, staging, etc..). |
139
|
|
|
* |
140
|
|
|
* @param string $tag |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public static function isVersionTag(string $tag): bool |
145
|
|
|
{ |
146
|
|
|
return Cache::rememberForever(self::cacheKey('version', "is-{$tag}"), function () use ($tag) { |
147
|
|
|
return (new StringHelpers(self::version()))->inString($tag); |
148
|
|
|
}); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Determine if the Application is running in a 'production' environment. |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public static function isEnvProduction(): bool |
157
|
|
|
{ |
158
|
|
|
return self::isEnv('production'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Determine if the Application is running in a 'development' environment. |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public static function isEnvDevelopment(): bool |
167
|
|
|
{ |
168
|
|
|
return self::isEnv('development'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Determine if the application is in a particular environment. |
173
|
|
|
* |
174
|
|
|
* @param string $env |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public static function isEnv(string $env): bool |
178
|
|
|
{ |
179
|
|
|
return config('app.env') == $env; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Retrieve a cache key for a particular service item. |
184
|
|
|
* |
185
|
|
|
* @param string $item |
186
|
|
|
* @param string|null $identifier |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
private static function cacheKey(string $item, string $identifier = null): string |
191
|
|
|
{ |
192
|
|
|
return self::CACHE_PREFIX.':'.$item.(isset($identifier) ? '#'.$identifier : ''); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|