Skip to content

Commit

Permalink
Merge branch 'master' into 3.1-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Aug 30, 2023
2 parents 59c897b + 0f6d5f5 commit 1318a41
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,67 +37,69 @@ class Composer
public static function getLockContent(): Collection
{
if (! self::$content) {
$path = self::discoverLockFile();
if (! $path) {
if (! $path = self::discoverLockFile()) {
throw new RuntimeException('composer.lock not found.');
}

self::$content = collect(json_decode(file_get_contents($path), true));
$packages = self::$content->offsetGet('packages') ?? [];
$packagesDev = self::$content->offsetGet('packages-dev') ?? [];

foreach (array_merge($packages, $packagesDev) as $package) {
$packageName = '';
foreach ($package ?? [] as $key => $value) {
if ($key === 'name') {
$packageName = $value;
continue;
}
switch ($key) {
case 'extra':
$packageName && self::$extra[$packageName] = $value;
break;
case 'scripts':
$packageName && self::$scripts[$packageName] = $value;
break;
case 'version':
$packageName && self::$versions[$packageName] = $value;
break;
}

$packageName && match ($key) {
'extra' => self::$extra[$packageName] = $value,
'scripts' => self::$scripts[$packageName] = $value,
'version' => self::$versions[$packageName] = $value,
default => null,
};
}
}
}

return self::$content;
}

public static function getJsonContent(): Collection
{
if (! self::$json) {
$path = BASE_PATH . '/composer.json';
if (! is_readable($path)) {
throw new RuntimeException('composer.json is not readable.');
}
self::$json = collect(json_decode(file_get_contents($path), true));
if (self::$json) {
return self::$json;
}

if (! is_readable($path = BASE_PATH . '/composer.json')) {
throw new RuntimeException('composer.json is not readable.');
}
return self::$json;

return self::$json = collect(json_decode(file_get_contents($path), true));
}

public static function discoverLockFile(): string
{
$path = '';
if (is_readable(BASE_PATH . '/composer.lock')) {
$path = BASE_PATH . '/composer.lock';
if (is_readable($path = BASE_PATH . '/composer.lock')) {
return $path;
}
return $path;

return '';
}

public static function getMergedExtra(string $key = null)
{
if (! self::$extra) {
self::getLockContent();
}

if ($key === null) {
return self::$extra;
}

$extra = [];

foreach (self::$extra as $project => $config) {
foreach ($config ?? [] as $configKey => $item) {
if ($key === $configKey && $item) {
Expand All @@ -111,30 +113,35 @@ public static function getMergedExtra(string $key = null)
}
}
}

return $extra;
}

public static function getLoader(): ClassLoader
{
if (! self::$classLoader) {
self::$classLoader = self::findLoader();
}
return self::$classLoader;
return self::$classLoader ??= self::findLoader();
}

public static function setLoader(ClassLoader $classLoader): ClassLoader
{
self::$classLoader = $classLoader;
return $classLoader;
return self::$classLoader = $classLoader;
}

public static function getScripts(): array
{
if (! self::$scripts) {
self::getLockContent();
}

return self::$scripts;
}

public static function getVersions(): array
{
if (! self::$versions) {
self::getLockContent();
}

return self::$versions;
}

Expand All @@ -158,6 +165,7 @@ public static function hasPackage(string $packageName): bool
private static function findLoader(): ClassLoader
{
$loaders = spl_autoload_functions();

foreach ($loaders as $loader) {
if (is_array($loader) && $loader[0] instanceof ClassLoader) {
return $loader[0];
Expand Down

0 comments on commit 1318a41

Please sign in to comment.