0 follower

Class Yiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfigDependenciesModifier

InheritanceYiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfigDependenciesModifier

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfig $config )
$config Yiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfig

                public function __construct(private ComposerConfig $config)
{
}

            
ensureDependenciesUsedOnlyInSection() public method

public self ensureDependenciesUsedOnlyInSection ( array $packageNames, string $targetSection )
$packageNames array
$targetSection string

                public function ensureDependenciesUsedOnlyInSection(array $packageNames, string $targetSection): self
{
    $this->validatePackageNames($packageNames);
    ComposerConfig::validateDependencySection($targetSection);
    $sectionForCleaning = $targetSection === ComposerConfig::SECTION_REQUIRE ?
        ComposerConfig::SECTION_REQUIRE_DEV :
        ComposerConfig::SECTION_REQUIRE;
    $config = $this->config;
    $targetDependencyList = $config->getDependencyList($targetSection);
    $dependenciesChanged = false;
    foreach ($packageNames as $packageName) {
        if (!$targetDependencyList->hasDependency($packageName)) {
            $targetDependencyList->addDependency(
                $packageName,
                $this->getDependencyConstraint($packageName, $sectionForCleaning)
            );
            $dependenciesChanged = true;
        }
    }
    if ($dependenciesChanged) {
        if ($config->sortPackagesEnabled()) {
            $targetDependencyList->sort();
        }
        $config->setDependencyList($targetSection, $targetDependencyList);
    }
    $this->removeDependencies($packageNames, $sectionForCleaning);
    return $this;
}

            
removeDependencies() public method

Remove dependencies from composer config.

public $this removeDependencies ( string[] $packageNames, string|null $targetSection null )
$packageNames string[]

Names of packages to be removed.

$targetSection string|null

Section from which dependencies should be removed: "require" or "require-dev". If NULL, dependencies will be removed from both sections.

                public function removeDependencies(array $packageNames, ?string $targetSection = null): self
{
    $this->validatePackageNames($packageNames);
    if ($targetSection === null) {
        $sections = ComposerConfig::getAllDependencySections();
    } else {
        ComposerConfig::validateDependencySection($targetSection);
        $sections = [$targetSection];
    }
    $config = $this->config;
    foreach ($sections as $section) {
        if ($config->hasSection($section)) {
            $dependencyList = $config
                ->getDependencyList($section)
                ->removeDependencies($packageNames);
            if ($dependencyList->isEmpty()) {
                $config->removeSection($section);
            } else {
                $config->setDependencyList($section, $dependencyList);
            }
        }
    }
    return $this;
}