forked from silverstripe/silverstripe-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionProvider.php
More file actions
249 lines (229 loc) · 7.02 KB
/
VersionProvider.php
File metadata and controls
249 lines (229 loc) · 7.02 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
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
<?php
namespace SilverStripe\Core\Manifest;
use InvalidArgumentException;
use Composer\InstalledVersions;
use SilverStripe\Dev\Deprecation;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
/**
* The version provider will look up configured modules and examine the composer.lock file
* to find the current version installed for each. This is used for the logo title in the CMS
* via {@link LeftAndMain::CMSVersion()}
*
* Example configuration:
*
* <code>
* SilverStripe\Core\Manifest\VersionProvider:
* modules:
* # package/name: Package Title
* silverstripe/framework: Framework
* silverstripe/cms: CMS
* </code>
*/
class VersionProvider
{
use Configurable;
use Injectable;
/**
* @var array<string,string>
*/
private static $modules = [
'silverstripe/framework' => 'Framework',
'silverstripe/recipe-core' => 'Core Recipe',
];
/**
* Gets a comma delimited string of package titles and versions
*
* @return string
*/
public function getVersion()
{
$key = preg_replace("/[^A-Za-z0-9]/", '_', $this->getComposerLockPath() . '_all');
$version = $this->getCachedValue($key);
if ($version) {
return $version;
}
$modules = $this->getModules();
$lockModules = $this->getModuleVersionFromComposer(array_keys($modules));
$moduleVersions = [];
foreach ($modules as $module => $title) {
if (!array_key_exists($module, $lockModules)) {
continue;
}
$version = $lockModules[$module];
$moduleVersions[$module] = [$title, $version];
}
$moduleVersions = $this->filterModules($moduleVersions);
$ret = [];
foreach ($moduleVersions as $module => $value) {
list($title, $version) = $value;
$ret[] = "$title: $version";
}
$version = implode(', ', $ret);
if ($version) {
$this->setCacheValue($key, $version);
}
return $version;
}
/**
* Get the version of a specific module
*
* @param string $module - e.g. silverstripe/framework
* @return string - e.g. 4.11
*/
public function getModuleVersion(string $module): string
{
$key = preg_replace("/[^A-Za-z0-9]/", '_', $this->getComposerLockPath() . '_' . $module);
$version = $this->getCachedValue($key);
if ($version) {
return $version;
}
$version = $this->getModuleVersionFromComposer([$module])[$module] ?? '';
if ($version) {
$this->setCacheValue($key, $version);
}
return $version;
}
/**
* @return CacheInterface
*/
private function getCache(): CacheInterface
{
return Injector::inst()->get(CacheInterface::class . '.VersionProvider');
}
/**
* @param string $key
*
* @return string
*/
private function getCachedValue(string $key): string
{
$cache = $this->getCache();
try {
if ($cache->has($key)) {
return $cache->get($key);
}
} catch (InvalidArgumentException $e) {
}
return '';
}
/**
* @param string $key
* @param string $value
*/
private function setCacheValue(string $key, string $value): void
{
$cache = $this->getCache();
try {
$cache->set($key, $value);
} catch (InvalidArgumentException $e) {
}
}
/**
* Filter modules to only use the last module from a git repo, for example
*
* [
* silverstripe/framework => ['Framework', 1.1.1'],
* silverstripe/cms => ['CMS', 2.2.2'],
* silverstripe/recipe-cms => ['CMS Recipe', '3.3.3'],
* cwp/cwp-core => ['CWP', '4.4.4']
* ]
* =>
* [
* silverstripe/recipe-cms => ['CMS Recipe', '3.3.3'],
* cwp/cwp-core => ['CWP', '4.4.4']
* ]
*
* @param array<string,array<int,string>> $modules
* @return array<string,array<int,string>>
*/
private function filterModules(array $modules)
{
$accountModule = [];
foreach ($modules as $module => $value) {
if (!preg_match('#^([a-z0-9\-]+)/([a-z0-9\-]+)$#', $module, $m)) {
continue;
}
$account = $m[1];
$accountModule[$account] = [$module, $value];
}
$ret = [];
foreach ($accountModule as $account => $arr) {
list($module, $value) = $arr;
$ret[$module] = $value;
}
return $ret;
}
/**
* Gets the configured core modules to use for the SilverStripe application version
*
* @return array<string,string>
*/
public function getModules()
{
$modules = Config::inst()->get(VersionProvider::class, 'modules');
return !empty($modules) ? $modules : ['silverstripe/framework' => 'Framework'];
}
/**
* Tries to obtain version number from composer.lock if it exists
*
* @param array<string> $modules
* @return array<string|string>
*/
public function getModuleVersionFromComposer($modules = [])
{
$versions = [];
foreach ($modules as $module) {
if (!InstalledVersions::isInstalled($module)) {
continue;
}
$versions[$module] = InstalledVersions::getPrettyVersion($module);
}
return $versions;
}
/**
* Load composer.lock's contents and return it
*
* @deprecated 5.1.0 Has been replaced by composer-runtime-api
* @param bool $cache
* @return array
*/
protected function getComposerLock($cache = true)
{
Deprecation::notice("5.1", "Has been replaced by composer-runtime-api", Deprecation::SCOPE_METHOD);
$composerLockPath = $this->getComposerLockPath();
if (!file_exists($composerLockPath)) {
return [];
}
$lockData = [];
$jsonData = file_get_contents($composerLockPath);
$jsonData = $jsonData ? $jsonData : '';
$cacheKey = md5($jsonData);
if ($cache) {
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
if ($versions = $cache->get($cacheKey)) {
$lockData = json_decode($versions, true);
}
}
if (empty($lockData) && $jsonData) {
$lockData = json_decode($jsonData, true);
if ($cache) {
$cache->set($cacheKey, $jsonData);
}
}
$lockData = $lockData ? $lockData : [];
return $lockData;
}
/**
* @return string
* @deprecated 5.4.0 Will be removed without equivalent functionality to replace it.
*/
protected function getComposerLockPath(): string
{
Deprecation::noticeWithNoReplacment('5.4.0');
return BASE_PATH . '/composer.lock';
}
}