Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX use composer runtime api #11157

Merged
merged 16 commits into from
Feb 27, 2024
Merged
42 changes: 26 additions & 16 deletions src/Core/Manifest/VersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VersionProvider
use Injectable;

/**
* @var array
* @var array<string,string>
*/
private static $modules = [
'silverstripe/framework' => 'Framework',
Expand All @@ -50,10 +50,10 @@ public function getVersion()
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)) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
$version = $lockModules[$module];
Expand Down Expand Up @@ -145,14 +145,14 @@ private function setCacheValue(string $key, string $value): void
* cwp/cwp-core => ['CWP', '4.4.4']
* ]
*
* @param array $modules
* @return array
* @param array<string,mixed> $modules
* @return array<string,mixed>
lekoala marked this conversation as resolved.
Show resolved Hide resolved
*/
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];
Expand All @@ -169,7 +169,7 @@ private function filterModules(array $modules)
/**
* Gets the configured core modules to use for the SilverStripe application version
*
* @return array
* @return array<string,string>
*/
public function getModules()
{
Expand All @@ -180,16 +180,23 @@ public function getModules()
/**
* Tries to obtain version number from composer.lock if it exists
*
* @param array $modules
* @return array
* @param array<int,string> $modules
lekoala marked this conversation as resolved.
Show resolved Hide resolved
* @return array<int|string,mixed>
lekoala marked this conversation as resolved.
Show resolved Hide resolved
*/
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'];
}
}
Expand All @@ -201,34 +208,37 @@ public function getModuleVersionFromComposer($modules = [])
* Load composer.lock's contents and return it
*
* @param bool $cache
* @return array
* @return array<mixed>
lekoala marked this conversation as resolved.
Show resolved Hide resolved
*/
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;
}

Expand Down
Loading