Skip to content

Commit

Permalink
Optimized code for reading extra data from composer.lock. (#7223)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbzz authored Jan 8, 2025
1 parent f401e77 commit 899b7db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function discoverLockFile(): string
return '';
}

public static function getMergedExtra(?string $key = null)
public static function getMergedExtra(?string $key = null): array
{
if (! self::$extra) {
self::getLockContent();
Expand All @@ -101,16 +101,16 @@ public static function getMergedExtra(?string $key = null)

$extra = [];

foreach (self::$extra as $project => $config) {
foreach ($config ?? [] as $configKey => $item) {
if ($key === $configKey && $item) {
foreach ($item as $k => $v) {
if (is_array($v)) {
$extra[$k] = array_merge($extra[$k] ?? [], $v);
} else {
$extra[$k][] = $v;
}
}
foreach (self::$extra as $config) {
if (! isset($config[$key])) {
continue;
}

foreach ($config[$key] as $k => $v) {
if (is_array($v)) {
$extra[$k] = array_merge($extra[$k] ?? [], $v);
} else {
$extra[$k][] = $v;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public function testHasPackage()
$this->assertTrue(Composer::hasPackage('hyperf/framework'));
$this->assertFalse(Composer::hasPackage('composer/unknown'));
}

public function testGetMergedExtra()
{
$providers = Composer::getMergedExtra('hyperf')['config'] ?? [];
$this->assertNotEmpty($providers);
}
}

0 comments on commit 899b7db

Please sign in to comment.