Skip to content

Commit

Permalink
Refactor: split merge plan class
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 7, 2023
1 parent f8288d9 commit db62a7b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 75 deletions.
98 changes: 98 additions & 0 deletions src/Composer/MergePlanCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Config\Composer;

use Yiisoft\Config\Options;

/**
* @internal
*/
final class MergePlanCollector
{
/**
* @psalm-var array<string, array<string, array<string, string[]>>>
*/
private array $mergePlan = [];

/**
* Adds an item to the merge plan.
*
* @param string $file The config file.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function add(
string $file,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package][] = $file;
}

/**
* Adds a multiple items to the merge plan.
*
* @param string[] $files The config files.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addMultiple(
array $files,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package] = $files;
}

/**
* Adds an empty environment item to the merge plan.
*
* @param string $environment The environment name.
*/
public function addEnvironmentWithoutConfigs(string $environment): void
{
$this->mergePlan[$environment] = [];
}

/**
* Add empty group if it not exists.
*
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): void
{
if (!isset($this->mergePlan[$environment][$group])) {
$this->mergePlan[$environment][$group] = [];
}
}

/**
* Returns the merge plan group.
*
* @param string $group The group name.
* @param string $environment The environment name.
*
* @return array<string, string[]>
*/
public function getGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): array
{
return $this->mergePlan[$environment][$group] ?? [];
}

/**
* Returns the merge plan as an array.
*
* @psalm-return array<string, array<string, array<string, string[]>>>
*/
public function generate(): array
{
return $this->mergePlan;
}
}
7 changes: 3 additions & 4 deletions src/Composer/MergePlanProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Composer\Composer;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Yiisoft\Config\MergePlan;
use Yiisoft\Config\Options;
use Yiisoft\VarDumper\VarDumper;

Expand All @@ -24,15 +23,15 @@
*/
final class MergePlanProcess
{
private MergePlan $mergePlan;
private MergePlanCollector $mergePlan;
private ProcessHelper $helper;

/**
* @param Composer $composer The composer instance.
*/
public function __construct(Composer $composer)
{
$this->mergePlan = new MergePlan();
$this->mergePlan = new MergePlanCollector();
$this->helper = new ProcessHelper($composer);

if (!$this->helper->shouldBuildMergePlan()) {
Expand Down Expand Up @@ -142,7 +141,7 @@ private function addEnvironmentsConfigsToMergePlan(): void

private function updateMergePlan(): void
{
$mergePlan = $this->mergePlan->toArray();
$mergePlan = $this->mergePlan->generate();
ksort($mergePlan);

$filePath = $this->helper->getPaths()->absolute(
Expand Down
3 changes: 1 addition & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function __construct(
) {
$environment = empty($environment) ? Options::DEFAULT_ENVIRONMENT : $environment;

/** @psalm-suppress UnresolvableInclude, MixedArgument */
$mergePlan = new MergePlan(require $paths->absolute($mergePlanFile));
$mergePlan = new MergePlan($paths->absolute($mergePlanFile));

if (!$mergePlan->hasEnvironment($environment)) {
$this->throwException(sprintf('The "%s" configuration environment does not exist.', $environment));
Expand Down
74 changes: 5 additions & 69 deletions src/MergePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,14 @@
final class MergePlan
{
/**
* @psalm-param array<string, array<string, array<string, string[]>>> $mergePlan
* @psalm-var array<string, array<string, array<string, string[]>>>
*/
public function __construct(
private array $mergePlan = [],
) {
}
private array $mergePlan;

/**
* Adds an item to the merge plan.
*
* @param string $file The config file.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function add(
string $file,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package][] = $file;
}

/**
* Adds a multiple items to the merge plan.
*
* @param string[] $files The config files.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addMultiple(
array $files,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package] = $files;
}

/**
* Adds an empty environment item to the merge plan.
*
* @param string $environment The environment name.
*/
public function addEnvironmentWithoutConfigs(string $environment): void
{
$this->mergePlan[$environment] = [];
}

/**
* Add empty group if it not exists.
*
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): void
public function __construct(string $file)
{
if (!isset($this->mergePlan[$environment][$group])) {
$this->mergePlan[$environment][$group] = [];
}
/** @psalm-suppress UnresolvableInclude */
$this->mergePlan = require $file;
}

/**
Expand All @@ -87,16 +33,6 @@ public function getGroup(string $group, string $environment = Options::DEFAULT_E
return $this->mergePlan[$environment][$group] ?? [];
}

/**
* Returns the merge plan as an array.
*
* @psalm-return array<string, array<string, array<string, string[]>>>
*/
public function toArray(): array
{
return $this->mergePlan;
}

/**
* Checks whether the group exists in the merge plan.
*
Expand Down

0 comments on commit db62a7b

Please sign in to comment.