From 899b7dbfe39b60d25ec71da3ad445d9db0bbce1a Mon Sep 17 00:00:00 2001 From: weijiang <463661663@qq.com> Date: Wed, 8 Jan 2025 15:28:13 +0800 Subject: [PATCH] Optimized code for reading extra data from `composer.lock`. (#7223) --- src/Composer.php | 22 +++++++++++----------- tests/ComposerTest.php | 6 ++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Composer.php b/src/Composer.php index 3e19fe4..f46b60e 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -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(); @@ -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; } } } diff --git a/tests/ComposerTest.php b/tests/ComposerTest.php index 4698d75..ca04740 100644 --- a/tests/ComposerTest.php +++ b/tests/ComposerTest.php @@ -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); + } }