0 follower

Class Yiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfig

InheritanceYiisoft\YiiDevTool\Infrastructure\Composer\Config\ComposerConfig

Method Details

Hide inherited methods

asArray() public method

public asArray( ): array

                public function asArray(): array
{
    return $this->data;
}

            
asPrettyJson() public method

public asPrettyJson( ): string

                public function asPrettyJson(): string
{
    $content = json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    if ($content === false) {
        throw new RuntimeException('Failed to encode JSON.');
    }
    return $content;
}

            
createByArray() public static method

public static createByArray( array $config ): self
$config array

                public static function createByArray(array $config): self
{
    return new self($config);
}

            
createByFilePath() public static method

public static createByFilePath( string $path ): self
$path string

                public static function createByFilePath(string $path): self
{
    $content = file_get_contents($path);
    if ($content === false) {
        throw new RuntimeException('Failed to read file ' . $path);
    }
    return static::createByJson($content);
}

            
createByJson() public static method

public static createByJson( string $json ): self
$json string

                public static function createByJson(string $json): self
{
    $config = json_decode($json, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new RuntimeException('Failed to decode JSON.');
    }
    return static::createByArray($config);
}

            
getAllDependencySections() public static method

public static getAllDependencySections( ): array

                public static function getAllDependencySections(): array
{
    return [
        self::SECTION_REQUIRE,
        self::SECTION_REQUIRE_DEV,
    ];
}

            
getDependencyList() public method

public getDependencyList( string $section ): Yiisoft\YiiDevTool\Infrastructure\Composer\Config\Dependency\ComposerConfigDependencyList
$section string

                public function getDependencyList(string $section): ComposerConfigDependencyList
{
    self::validateDependencySection($section);
    return new ComposerConfigDependencyList($this->getSection($section));
}

            
getPSRNamespaces() public method

public getPSRNamespaces( ): array

                public function getPSRNamespaces(): array
{
    $namespaces = [];
    if (isset($this->data['autoload']['psr-4'])) {
        $namespaces = array_merge($namespaces, array_keys($this->data['autoload']['psr-4']));
    }
    if (isset($this->data['autoload-dev']['psr-4'])) {
        $namespaces = [...$namespaces, ...array_keys($this->data['autoload-dev']['psr-4'])];
    }
    if (isset($this->data['autoload']['psr-0'])) {
        $namespaces = [...$namespaces, ...array_keys($this->data['autoload']['psr-0'])];
    }
    if (isset($this->data['autoload-dev']['psr-0'])) {
        $namespaces = [...$namespaces, ...array_keys($this->data['autoload-dev']['psr-0'])];
    }
    return $namespaces;
}

            
getSection() public method

public getSection( string $section ): mixed
$section string

                public function getSection(string $section)
{
    return $this->hasSection($section) ? $this->data[$section] : null;
}

            
hasSection() public method

public hasSection( string $section ): boolean
$section string

                public function hasSection(string $section): bool
{
    return array_key_exists($section, $this->data);
}

            
removeSection() public method

public removeSection( mixed $section ): void
$section mixed

                public function removeSection($section): void
{
    if ($this->hasSection($section)) {
        unset($this->data[$section]);
    }
}

            
setDependencyList() public method

public setDependencyList( string $section, Yiisoft\YiiDevTool\Infrastructure\Composer\Config\Dependency\ComposerConfigDependencyList $dependencyList ): self
$section string
$dependencyList Yiisoft\YiiDevTool\Infrastructure\Composer\Config\Dependency\ComposerConfigDependencyList

                public function setDependencyList(string $section, ComposerConfigDependencyList $dependencyList): self
{
    self::validateDependencySection($section);
    $this->setSection($section, $dependencyList->asArray());
    return $this;
}

            
setSection() public method

public setSection( string $section, mixed $data ): void
$section string
$data mixed

                public function setSection(string $section, $data): void
{
    $this->data[$section] = $data;
}

            
sortPackagesEnabled() public method

public sortPackagesEnabled( ): boolean

                public function sortPackagesEnabled(): bool
{
    return isset($this->data['config']['sort-packages']) && $this->data['config']['sort-packages'] === true;
}

            
usesNonPSRAutoload() public method

public usesNonPSRAutoload( ): boolean

                public function usesNonPSRAutoload(): bool
{
    return isset($this->data['autoload']['classmap'])
        || isset($this->data['autoload']['files'])
        || isset($this->data['autoload-dev']['classmap'])
        || isset($this->data['autoload-dev']['files']);
}

            
validateDependencySection() public static method

public static validateDependencySection( string $section ): void
$section string

                public static function validateDependencySection(string $section): void
{
    if (!in_array($section, self::getAllDependencySections(), true)) {
        throw new InvalidArgumentException('Invalid section.');
    }
}

            
writeToFile() public method

public writeToFile( string $targetPath ): self
$targetPath string

                public function writeToFile(string $targetPath): self
{
    $result = file_put_contents($targetPath, $this->asPrettyJson() . "\n");
    if ($result === false) {
        throw new RuntimeException('Failed to write file ' . $targetPath);
    }
    return $this;
}