diff --git a/CHANGELOG.md b/CHANGELOG.md index 71c864f..5bd3e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.5.1 under development -- no changes in this release. +- Enh #172: Refactoring: extract root package configuration reader to separate class (@vjik) ## 1.5.0 December 25, 2023 diff --git a/src/Composer/MergePlanProcess.php b/src/Composer/MergePlanProcess.php index a4ad495..97a5414 100644 --- a/src/Composer/MergePlanProcess.php +++ b/src/Composer/MergePlanProcess.php @@ -8,7 +8,6 @@ use Composer\Package\PackageInterface; use Composer\Util\Filesystem; use Yiisoft\Config\MergePlan; -use Yiisoft\Config\Options; use Yiisoft\VarDumper\VarDumper; use function file_get_contents; diff --git a/src/Options.php b/src/Composer/Options.php similarity index 98% rename from src/Options.php rename to src/Composer/Options.php index 9c5bd88..906a438 100644 --- a/src/Options.php +++ b/src/Composer/Options.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Config; +namespace Yiisoft\Config\Composer; use function is_array; use function str_replace; diff --git a/src/Composer/PackageFilesProcess.php b/src/Composer/PackageFilesProcess.php index b4a7015..ef4e4a1 100644 --- a/src/Composer/PackageFilesProcess.php +++ b/src/Composer/PackageFilesProcess.php @@ -6,7 +6,6 @@ use Composer\Composer; use Yiisoft\Config\ConfigPaths; -use Yiisoft\Config\Options; use function glob; use function in_array; diff --git a/src/Composer/ProcessHelper.php b/src/Composer/ProcessHelper.php index 692baea..548c1e3 100644 --- a/src/Composer/ProcessHelper.php +++ b/src/Composer/ProcessHelper.php @@ -5,27 +5,24 @@ namespace Yiisoft\Config\Composer; use Composer\Composer; -use Composer\Factory; use Composer\Package\BasePackage; use Composer\Package\PackageInterface; use Yiisoft\Config\ConfigPaths; -use Yiisoft\Config\Options; use Yiisoft\Strings\WildcardPattern; -use function dirname; use function is_string; -use function realpath; use function str_replace; /** * @internal + * @psalm-import-type PackageConfigurationType from RootConfiguration + * @psalm-import-type EnvironmentsConfigurationType from RootConfiguration */ final class ProcessHelper { private Composer $composer; private ConfigPaths $paths; - private Options $rootPackageOptions; - private array $rootPackageExtra; + private RootConfiguration $rootConfiguration; /** * @psalm-var array @@ -38,27 +35,18 @@ final class ProcessHelper public function __construct(Composer $composer) { /** @psalm-suppress UnresolvableInclude, MixedOperand */ - require_once $composer - ->getConfig() - ->get('vendor-dir') . '/autoload.php'; - /** @psalm-suppress MixedArgument */ - $rootPath = realpath(dirname(Factory::getComposerFile())); + require_once $composer->getConfig()->get('vendor-dir') . '/autoload.php'; - $rootPackageExtra = $composer - ->getPackage() - ->getExtra(); - /** @psalm-suppress UnresolvableInclude */ - $this->rootPackageExtra = isset($rootPackageExtra['config-plugin-file']) - ? (array) (require "$rootPath/{$rootPackageExtra['config-plugin-file']}") - : $rootPackageExtra - ; + $this->rootConfiguration = RootConfiguration::fromComposerInstance($composer); $this->composer = $composer; - $this->rootPackageOptions = new Options($this->rootPackageExtra); - $this->paths = new ConfigPaths($rootPath, $this->rootPackageOptions->sourceDirectory()); + $this->paths = new ConfigPaths( + $this->rootConfiguration->path(), + $this->rootConfiguration->options()->sourceDirectory(), + ); $this->packages = (new PackagesListBuilder( $this->composer, - $this->rootPackageOptions->packageTypes() + $this->rootConfiguration->options()->packageTypes() ))->build(); } @@ -169,7 +157,7 @@ public function getPackageFilename(PackageInterface $package, Options $options, * * @return array The package configuration. * - * @psalm-return array> + * @psalm-return PackageConfigurationType * @psalm-suppress MixedReturnTypeCoercion */ public function getPackageConfig(PackageInterface $package): array @@ -182,12 +170,11 @@ public function getPackageConfig(PackageInterface $package): array * * @return array The root package configuration. * - * @psalm-return array> - * @psalm-suppress MixedReturnTypeCoercion + * @psalm-return PackageConfigurationType */ public function getRootPackageConfig(): array { - return (array) ($this->rootPackageExtra['config-plugin'] ?? []); + return $this->rootConfiguration->packageConfiguration(); } /** @@ -195,12 +182,11 @@ public function getRootPackageConfig(): array * * @return array The environment configuration. * - * @psalm-return array> - * @psalm-suppress MixedReturnTypeCoercion + * @psalm-return EnvironmentsConfigurationType */ public function getEnvironmentConfig(): array { - return (array) ($this->rootPackageExtra['config-plugin-environments'] ?? []); + return $this->rootConfiguration->environmentsConfiguration(); } /** @@ -220,7 +206,7 @@ public function getPaths(): ConfigPaths */ public function shouldBuildMergePlan(): bool { - return $this->rootPackageOptions->buildMergePlan(); + return $this->rootConfiguration->options()->buildMergePlan(); } /** @@ -228,7 +214,7 @@ public function shouldBuildMergePlan(): bool */ public function getMergePlanFile(): string { - return $this->rootPackageOptions->mergePlanFile(); + return $this->rootConfiguration->options()->mergePlanFile(); } /** @@ -272,7 +258,7 @@ private function getPackageRootDirectoryPath(PackageInterface $package): string */ private function isVendorOverridePackage(string $package): bool { - foreach ($this->rootPackageOptions->vendorOverrideLayerPackages() as $pattern) { + foreach ($this->rootConfiguration->options()->vendorOverrideLayerPackages() as $pattern) { if (!is_string($pattern)) { continue; } diff --git a/src/Composer/RootConfiguration.php b/src/Composer/RootConfiguration.php new file mode 100644 index 0000000..384f6c4 --- /dev/null +++ b/src/Composer/RootConfiguration.php @@ -0,0 +1,93 @@ +> + * @psalm-type EnvironmentsConfigurationType = array> + */ +final class RootConfiguration +{ + private Options $options; + + /** + * @psalm-var PackageConfigurationType + */ + private array $packageConfiguration; + + /** + * @psalm-var EnvironmentsConfigurationType + */ + private array $environmentsConfiguration; + + private function __construct( + private string $path, + array $composerExtra, + ) { + if (isset($composerExtra['config-plugin-file'])) { + /** + * @var array $extra + * @psalm-suppress UnresolvableInclude,MixedOperand + */ + $extra = require $this->path . '/' . $composerExtra['config-plugin-file']; + } else { + $extra = $composerExtra; + } + + $this->options = new Options($extra); + + /** @psalm-var PackageConfigurationType */ + $this->packageConfiguration = (array) ($extra['config-plugin'] ?? []); + + /** @psalm-var EnvironmentsConfigurationType */ + $this->environmentsConfiguration = $extra['config-plugin-environments'] ?? []; + } + + public static function fromComposerInstance(Composer $composer): self + { + return new self( + realpath(dirname(Factory::getComposerFile())), + $composer->getPackage()->getExtra(), + ); + } + + public function path(): string + { + return $this->path; + } + + public function options(): Options + { + return $this->options; + } + + /** + * Returns the root package configuration. + * + * @return array The root package configuration. + * + * @psalm-return PackageConfigurationType + */ + public function packageConfiguration(): array + { + return $this->packageConfiguration; + } + + /** + * Returns the environments configuration. + * + * @return array The environments configuration. + * + * @psalm-return EnvironmentsConfigurationType + */ + public function environmentsConfiguration(): array + { + return $this->environmentsConfiguration; + } +} diff --git a/src/Config.php b/src/Config.php index e5ae683..bc0a8c0 100644 --- a/src/Config.php +++ b/src/Config.php @@ -6,6 +6,8 @@ use ErrorException; +use Yiisoft\Config\Composer\Options; + use function extract; use function func_get_arg; use function restore_error_handler; diff --git a/src/ConfigPaths.php b/src/ConfigPaths.php index f9dd5e0..d5720fa 100644 --- a/src/ConfigPaths.php +++ b/src/ConfigPaths.php @@ -4,6 +4,8 @@ namespace Yiisoft\Config; +use Yiisoft\Config\Composer\Options; + use function strlen; use function substr; use function trim; diff --git a/src/FilesExtractor.php b/src/FilesExtractor.php index a40be43..fefa33c 100644 --- a/src/FilesExtractor.php +++ b/src/FilesExtractor.php @@ -6,6 +6,8 @@ use ErrorException; +use Yiisoft\Config\Composer\Options; + use function array_merge; use function glob; use function is_file; diff --git a/src/MergePlan.php b/src/MergePlan.php index f6d7ac1..bd6f848 100644 --- a/src/MergePlan.php +++ b/src/MergePlan.php @@ -4,6 +4,8 @@ namespace Yiisoft\Config; +use Yiisoft\Config\Composer\Options; + /** * @internal */ diff --git a/tests/OptionsTest.php b/tests/Composer/OptionsTest.php similarity index 97% rename from tests/OptionsTest.php rename to tests/Composer/OptionsTest.php index 3dbe7a3..eae5578 100644 --- a/tests/OptionsTest.php +++ b/tests/Composer/OptionsTest.php @@ -2,10 +2,10 @@ declare(strict_types=1); -namespace Yiisoft\Config\Tests; +namespace Yiisoft\Config\Tests\Composer; use PHPUnit\Framework\TestCase; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; final class OptionsTest extends TestCase { diff --git a/tests/ConfigPaths/ConfigPathsTest.php b/tests/ConfigPaths/ConfigPathsTest.php index 9802a55..0771875 100644 --- a/tests/ConfigPaths/ConfigPathsTest.php +++ b/tests/ConfigPaths/ConfigPathsTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Config\ConfigPaths; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; final class ConfigPathsTest extends TestCase { diff --git a/tests/Integration/IntegrationTestCase.php b/tests/Integration/IntegrationTestCase.php index 6d0ae51..2fbd893 100644 --- a/tests/Integration/IntegrationTestCase.php +++ b/tests/Integration/IntegrationTestCase.php @@ -13,7 +13,7 @@ use Yiisoft\Config\Command\ConfigCommandProvider; use Yiisoft\Config\Config; use Yiisoft\Config\ConfigPaths; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; abstract class IntegrationTestCase extends TestCase { diff --git a/tests/Integration/ProcessOnAutoloadDump/ProcessOnAutoloadDumpTest.php b/tests/Integration/ProcessOnAutoloadDump/ProcessOnAutoloadDumpTest.php index a9150fe..98d1d2a 100644 --- a/tests/Integration/ProcessOnAutoloadDump/ProcessOnAutoloadDumpTest.php +++ b/tests/Integration/ProcessOnAutoloadDump/ProcessOnAutoloadDumpTest.php @@ -4,7 +4,7 @@ namespace Yiisoft\Config\Tests\Integration\ProcessOnAutoloadDump; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; use Yiisoft\Config\Tests\Integration\IntegrationTestCase; final class ProcessOnAutoloadDumpTest extends IntegrationTestCase diff --git a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php index ceddff1..9e016b2 100644 --- a/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php +++ b/tests/Integration/ProcessWithIgnoreAdditionalDefaultEnvironment/ProcessWithIgnoreAdditionalDefaultEnvironmentTest.php @@ -4,7 +4,7 @@ namespace Yiisoft\Config\Tests\Integration\ProcessWithIgnoreAdditionalDefaultEnvironment; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; use Yiisoft\Config\Tests\Integration\IntegrationTestCase; final class ProcessWithIgnoreAdditionalDefaultEnvironmentTest extends IntegrationTestCase diff --git a/tests/Integration/ProcessWithoutMergePlanBuild/ProcessWithoutMergePlanBuildTest.php b/tests/Integration/ProcessWithoutMergePlanBuild/ProcessWithoutMergePlanBuildTest.php index f92e6f5..4044b2b 100644 --- a/tests/Integration/ProcessWithoutMergePlanBuild/ProcessWithoutMergePlanBuildTest.php +++ b/tests/Integration/ProcessWithoutMergePlanBuild/ProcessWithoutMergePlanBuildTest.php @@ -4,7 +4,7 @@ namespace Yiisoft\Config\Tests\Integration\ProcessWithoutMergePlanBuild; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; use Yiisoft\Config\Tests\Integration\IntegrationTestCase; final class ProcessWithoutMergePlanBuildTest extends IntegrationTestCase diff --git a/tests/Integration/RebuildCommand/RebuildCommandTest.php b/tests/Integration/RebuildCommand/RebuildCommandTest.php index d3abb8d..9a4a7f3 100644 --- a/tests/Integration/RebuildCommand/RebuildCommandTest.php +++ b/tests/Integration/RebuildCommand/RebuildCommandTest.php @@ -4,7 +4,7 @@ namespace Yiisoft\Config\Tests\Integration\RebuildCommand; -use Yiisoft\Config\Options; +use Yiisoft\Config\Composer\Options; use Yiisoft\Config\Tests\Integration\IntegrationTestCase; final class RebuildCommandTest extends IntegrationTestCase