Skip to content

Commit

Permalink
Refactoring: extract root package configuration reader to separate cl…
Browse files Browse the repository at this point in the history
…ass (#172)
  • Loading branch information
vjik committed Mar 26, 2024
1 parent 956eb22 commit 7a80d5e
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/Composer/MergePlanProcess.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Options.php → src/Composer/Options.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Config;
namespace Yiisoft\Config\Composer;

use function is_array;
use function str_replace;
Expand Down
1 change: 0 additions & 1 deletion src/Composer/PackageFilesProcess.php
Expand Up @@ -6,7 +6,6 @@

use Composer\Composer;
use Yiisoft\Config\ConfigPaths;
use Yiisoft\Config\Options;

use function glob;
use function in_array;
Expand Down
50 changes: 18 additions & 32 deletions src/Composer/ProcessHelper.php
Expand Up @@ -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<string, BasePackage>
Expand All @@ -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();
}

Expand Down Expand Up @@ -169,7 +157,7 @@ public function getPackageFilename(PackageInterface $package, Options $options,
*
* @return array The package configuration.
*
* @psalm-return array<string, string|list<string>>
* @psalm-return PackageConfigurationType
* @psalm-suppress MixedReturnTypeCoercion
*/
public function getPackageConfig(PackageInterface $package): array
Expand All @@ -182,25 +170,23 @@ public function getPackageConfig(PackageInterface $package): array
*
* @return array The root package configuration.
*
* @psalm-return array<string, string|list<string>>
* @psalm-suppress MixedReturnTypeCoercion
* @psalm-return PackageConfigurationType
*/
public function getRootPackageConfig(): array
{
return (array) ($this->rootPackageExtra['config-plugin'] ?? []);
return $this->rootConfiguration->packageConfiguration();
}

/**
* Returns the environment configuration.
*
* @return array The environment configuration.
*
* @psalm-return array<string, array<string, string|string[]>>
* @psalm-suppress MixedReturnTypeCoercion
* @psalm-return EnvironmentsConfigurationType
*/
public function getEnvironmentConfig(): array
{
return (array) ($this->rootPackageExtra['config-plugin-environments'] ?? []);
return $this->rootConfiguration->environmentsConfiguration();
}

/**
Expand All @@ -220,15 +206,15 @@ public function getPaths(): ConfigPaths
*/
public function shouldBuildMergePlan(): bool
{
return $this->rootPackageOptions->buildMergePlan();
return $this->rootConfiguration->options()->buildMergePlan();
}

/**
* @return string The merge plan filepath.
*/
public function getMergePlanFile(): string
{
return $this->rootPackageOptions->mergePlanFile();
return $this->rootConfiguration->options()->mergePlanFile();
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
93 changes: 93 additions & 0 deletions src/Composer/RootConfiguration.php
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Config\Composer;

use Composer\Composer;
use Composer\Factory;

/**
* @internal
* @psalm-type PackageConfigurationType = array<string, string|list<string>>
* @psalm-type EnvironmentsConfigurationType = array<string, array<string, string|string[]>>
*/
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;
}
}
2 changes: 2 additions & 0 deletions src/Config.php
Expand Up @@ -6,6 +6,8 @@

use ErrorException;

use Yiisoft\Config\Composer\Options;

use function extract;
use function func_get_arg;
use function restore_error_handler;
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigPaths.php
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Config;

use Yiisoft\Config\Composer\Options;

use function strlen;
use function substr;
use function trim;
Expand Down
2 changes: 2 additions & 0 deletions src/FilesExtractor.php
Expand Up @@ -6,6 +6,8 @@

use ErrorException;

use Yiisoft\Config\Composer\Options;

use function array_merge;
use function glob;
use function is_file;
Expand Down
2 changes: 2 additions & 0 deletions src/MergePlan.php
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Config;

use Yiisoft\Config\Composer\Options;

/**
* @internal
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/OptionsTest.php → tests/Composer/OptionsTest.php
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigPaths/ConfigPathsTest.php
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/IntegrationTestCase.php
Expand Up @@ -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
{
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/RebuildCommand/RebuildCommandTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 7a80d5e

Please sign in to comment.