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