nulib/php/src/tools/pman/PmanYamlConfigFile.php

58 lines
1.5 KiB
PHP

<?php
namespace nulib\tools\pman;
use nulib\A;
use nulib\ext\yaml;
use nulib\os\path;
use nulib\ValueException;
class PmanYamlConfigFile {
function __construct(string $configFile=".", bool $ensureExists=true) {
if (is_dir($configFile)) $configFile = path::join($configFile, '.pman.yml');
if ($ensureExists && !file_exists($configFile)) {
$message = path::ppath($configFile).": fichier introuvable";
throw new ValueException($message);
}
$this->configFile = $configFile;
$this->load();
}
protected string $configFile;
function getConfigFile(): string {
return $this->configFile;
}
protected ?array $data = null;
protected function load(): array {
if ($this->data === null) {
$data = yaml::load($this->configFile);
$composer =& $data["composer"];
A::ensure_array($composer);
A::ensure_array($composer["profiles"]);
foreach ($composer["profiles"] as $profileName) {
$profile =& $composer[$profileName];
A::ensure_array($profile);
$profile["link"] = boolval($profile["link"] ?? false);
A::ensure_array($profile["require"]);
A::ensure_array($profile["require-dev"]);
}
$this->data = $data;
}
return $this->data;
}
function getProfileConfig(string $profile): array {
$config = $this->data["composer"][$profile] ?? null;
if ($config === null) {
throw new ValueException("$profile: profil invalide");
}
return $config;
}
function print(): void {
yaml::dump($this->data);
}
}