use composer runtime api

This commit is contained in:
Thomas Portelange 2024-02-23 17:06:27 +01:00 committed by GitHub
parent d33332cb9e
commit bc09127182
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,7 +30,7 @@ class VersionProvider
use Injectable;
/**
* @var array
* @var array<string,string>
*/
private static $modules = [
'silverstripe/framework' => 'Framework',
@ -50,10 +50,10 @@ class VersionProvider
return $version;
}
$modules = $this->getModules();
$lockModules = $this->getModuleVersionFromComposer(array_keys($modules ?? []));
$lockModules = $this->getModuleVersionFromComposer(array_keys($modules));
$moduleVersions = [];
foreach ($modules as $module => $title) {
if (!array_key_exists($module, $lockModules ?? [])) {
if (!array_key_exists($module, $lockModules)) {
continue;
}
$version = $lockModules[$module];
@ -145,14 +145,14 @@ class VersionProvider
* cwp/cwp-core => ['CWP', '4.4.4']
* ]
*
* @param array $modules
* @return array
* @param array<string,mixed> $modules
* @return array<string,mixed>
*/
private function filterModules(array $modules)
{
$accountModule = [];
foreach ($modules as $module => $value) {
if (!preg_match('#^([a-z0-9\-]+)/([a-z0-9\-]+)$#', $module ?? '', $m)) {
if (!preg_match('#^([a-z0-9\-]+)/([a-z0-9\-]+)$#', $module, $m)) {
continue;
}
$account = $m[1];
@ -169,7 +169,7 @@ class VersionProvider
/**
* Gets the configured core modules to use for the SilverStripe application version
*
* @return array
* @return array<string,string>
*/
public function getModules()
{
@ -180,16 +180,23 @@ class VersionProvider
/**
* Tries to obtain version number from composer.lock if it exists
*
* @param array $modules
* @return array
* @param array<int,string> $modules
* @return array<int|string,mixed>
*/
public function getModuleVersionFromComposer($modules = [])
{
if (class_exists(\Composer\InstalledVersions::class)) {
$versions = [];
foreach ($modules as $module) {
$versions[$module] = \Composer\InstalledVersions::getPrettyVersion($module);
}
return $versions;
}
$versions = [];
$lockData = $this->getComposerLock();
if ($lockData && !empty($lockData['packages'])) {
foreach ($lockData['packages'] as $package) {
if (in_array($package['name'], $modules ?? []) && isset($package['version'])) {
if (in_array($package['name'], $modules) && isset($package['version'])) {
$versions[$package['name']] = $package['version'];
}
}
@ -201,34 +208,37 @@ class VersionProvider
* Load composer.lock's contents and return it
*
* @param bool $cache
* @return array
* @return array<mixed>
*/
protected function getComposerLock($cache = true)
{
$composerLockPath = $this->getComposerLockPath();
if (!file_exists($composerLockPath ?? '')) {
if (!file_exists($composerLockPath)) {
return [];
}
$lockData = [];
$jsonData = file_get_contents($composerLockPath ?? '');
$jsonData = file_get_contents($composerLockPath);
$jsonData = $jsonData ? $jsonData : '';
$cacheKey = md5($jsonData);
if ($cache) {
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
$cacheKey = md5($jsonData ?? '');
if ($versions = $cache->get($cacheKey)) {
$lockData = json_decode($versions ?? '', true);
$lockData = json_decode($versions, true);
}
}
if (empty($lockData) && $jsonData) {
$lockData = json_decode($jsonData ?? '', true);
$lockData = json_decode($jsonData, true);
if ($cache) {
$cache->set($cacheKey, $jsonData);
}
}
$lockData = $lockData ? $lockData : [];
return $lockData;
}