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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"require": {
"php": "^8.1",
"composer-runtime-api": "^2.2",
lekoala marked this conversation as resolved.
Show resolved Hide resolved
"composer/installers": "^2.2",
"guzzlehttp/guzzle": "^7.5.0",
"guzzlehttp/psr7": "^2.4.0",
Expand Down
43 changes: 22 additions & 21 deletions src/Core/Manifest/VersionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;

/**
* The version provider will look up configured modules and examine the composer.lock file
Expand All @@ -30,7 +31,7 @@ class VersionProvider
use Injectable;

/**
* @var array
* @var array<string,string>
*/
private static $modules = [
'silverstripe/framework' => 'Framework',
Expand All @@ -50,10 +51,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 +146,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 +170,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,55 +181,55 @@ public function getModules()
/**
* Tries to obtain version number from composer.lock if it exists
*
* @param array $modules
* @return array
* @param array<string> $modules
* @return array<string|string>
*/
public function getModuleVersionFromComposer($modules = [])
{
$versions = [];
$lockData = $this->getComposerLock();
if ($lockData && !empty($lockData['packages'])) {
foreach ($lockData['packages'] as $package) {
if (in_array($package['name'], $modules ?? []) && isset($package['version'])) {
$versions[$package['name']] = $package['version'];
}
}
foreach ($modules as $module) {
$versions[$module] = \Composer\InstalledVersions::getPrettyVersion($module);
lekoala marked this conversation as resolved.
Show resolved Hide resolved
}
return $versions;
}

/**
* Load composer.lock's contents and return it
*
* @deprecated 5.1 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 ?? '')) {
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