108 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace cli\pman;
 | 
						|
 | 
						|
use nulib\A;
 | 
						|
use nulib\exceptions;
 | 
						|
use nulib\ext\yaml;
 | 
						|
use nulib\os\path;
 | 
						|
use nulib\str;
 | 
						|
 | 
						|
class ComposerPmanFile {
 | 
						|
  const NAMES = [".composer.pman", ".pman"];
 | 
						|
  const EXTS = [".yml", ".yaml"];
 | 
						|
 | 
						|
  function __construct(string $configFile=".", bool $ensureExists=true) {
 | 
						|
    if (is_dir($configFile)) {
 | 
						|
      $found = false;
 | 
						|
      foreach (self::NAMES as $name) {
 | 
						|
        foreach (self::EXTS as $ext) {
 | 
						|
          $file = path::join($configFile, "$name$ext");
 | 
						|
          if (file_exists($file)) {
 | 
						|
            $configFile = $file;
 | 
						|
            $found = true;
 | 
						|
            break;
 | 
						|
          }
 | 
						|
        }
 | 
						|
      }
 | 
						|
      if (!$found) {
 | 
						|
        $configFile = path::join($configFile, self::NAMES[0].self::EXTS[0]);
 | 
						|
      }
 | 
						|
    }
 | 
						|
    if ($ensureExists && !file_exists($configFile)) {
 | 
						|
      throw exceptions::invalid_value(path::ppath($configFile), "ce fichier", "il est introuvable");
 | 
						|
    }
 | 
						|
    $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"]);
 | 
						|
      A::ensure_array($composer["match_require"]);
 | 
						|
      A::ensure_array($composer["match_require-dev"]);
 | 
						|
      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 $composerRequires=null, ?array $composerRequireDevs=null): array {
 | 
						|
    $config = $this->data["composer"][$profile] ?? null;
 | 
						|
    if ($config === null) throw exceptions::invalid_value($profile, "ce profil");
 | 
						|
    if ($composerRequires !== null) {
 | 
						|
      $matchRequires = $this->data["composer"]["match_require"];
 | 
						|
      foreach ($composerRequires as $dep => $version) {
 | 
						|
        $found = false;
 | 
						|
        foreach ($matchRequires as $matchRequire) {
 | 
						|
          if (str::match_prefix($dep, $matchRequire)) {
 | 
						|
            $found = true;
 | 
						|
            break;
 | 
						|
          }
 | 
						|
        }
 | 
						|
        $require = $config["require"][$dep] ?? null;
 | 
						|
        if ($found && $require === null) {
 | 
						|
          $config["require"][$dep] = $version;
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    if ($composerRequireDevs !== null) {
 | 
						|
      $matchRequireDevs = $this->data["composer"]["match_require-dev"];
 | 
						|
      foreach ($composerRequireDevs as $dep => $version) {
 | 
						|
        $found = false;
 | 
						|
        foreach ($matchRequireDevs as $matchRequireDev) {
 | 
						|
          if (str::match_prefix($dep, $matchRequireDev)) {
 | 
						|
            $found = true;
 | 
						|
            break;
 | 
						|
          }
 | 
						|
        }
 | 
						|
        $requireDev = $config["require-dev"][$dep] ?? null;
 | 
						|
        if ($found && $requireDev === null) {
 | 
						|
          $config["require"][$dep] = $version;
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    return $config;
 | 
						|
  }
 | 
						|
 | 
						|
  function print(): void {
 | 
						|
    yaml::dump($this->data);
 | 
						|
  }
 | 
						|
}
 |