Class Yiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfigDependenciesModifier
| Inheritance | Yiisoft\ |
|---|
Public Methods
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $config | Yiisoft\ |
|
public function __construct(private ComposerConfig $config) {}
| 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;
}
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.