172 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			172 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
namespace cli\pman;
 | 
						|
 | 
						|
use nulib\cl;
 | 
						|
use nulib\exceptions;
 | 
						|
use nulib\ext\json;
 | 
						|
use nulib\file;
 | 
						|
use nulib\os\path;
 | 
						|
 | 
						|
class ComposerFile {
 | 
						|
  function __construct(string $composerFile=".", bool $ensureExists=true) {
 | 
						|
    if (is_dir($composerFile)) $composerFile = path::join($composerFile, 'composer.json');
 | 
						|
    if ($ensureExists && !file_exists($composerFile)) {
 | 
						|
      throw exceptions::invalid_value(path::ppath($composerFile), "ce fichier", "il est introuvable");
 | 
						|
    }
 | 
						|
    $this->composerFile = $composerFile;
 | 
						|
    $this->load();
 | 
						|
  }
 | 
						|
 | 
						|
  protected string $composerFile;
 | 
						|
 | 
						|
  function getComposerFile(): string {
 | 
						|
    return $this->composerFile;
 | 
						|
  }
 | 
						|
 | 
						|
  protected ?array $data = null;
 | 
						|
 | 
						|
  protected function load(): array {
 | 
						|
    if ($this->data === null) {
 | 
						|
      $this->data = json::load($this->composerFile);
 | 
						|
    }
 | 
						|
    return $this->data;
 | 
						|
  }
 | 
						|
 | 
						|
  function getv(string $pkey="", $default=null) {
 | 
						|
    return cl::pget($this->data, $pkey, $default);
 | 
						|
  }
 | 
						|
 | 
						|
  function geta(string $pkey="", ?array $default=[]): ?array {
 | 
						|
    return cl::withn($this->getv($pkey, $default));
 | 
						|
  }
 | 
						|
 | 
						|
  function getRequires(): array {
 | 
						|
    return $this->geta("require");
 | 
						|
  }
 | 
						|
 | 
						|
  function setRequire(string $dep, string $version): void {
 | 
						|
    $this->setPkey("require.$dep", $version);
 | 
						|
  }
 | 
						|
 | 
						|
  function getRequireDevs(): array {
 | 
						|
    return $this->geta("require-dev");
 | 
						|
  }
 | 
						|
 | 
						|
  function setRequireDev(string $dep, string $version): void {
 | 
						|
    $this->setPkey("require-dev.$dep", $version);
 | 
						|
  }
 | 
						|
 | 
						|
  function getRepositories(): array {
 | 
						|
    return $this->geta("repositories");
 | 
						|
  }
 | 
						|
 | 
						|
  function setRepositories(array $repositories): void {
 | 
						|
    $this->data["repositories"] = $repositories;
 | 
						|
  }
 | 
						|
 | 
						|
  function setPkey(string $pkey, $value): void {
 | 
						|
    cl::pset($this->data, $pkey, $value);
 | 
						|
  }
 | 
						|
 | 
						|
  function delPkey(string $pkey): void {
 | 
						|
    cl::pdel($this->data, $pkey);
 | 
						|
  }
 | 
						|
 | 
						|
  const PATHS = [
 | 
						|
    "nulib/php" => "nulib",
 | 
						|
  ];
 | 
						|
 | 
						|
  function selectProfile(string $profile, ComposerPmanFile $config): void {
 | 
						|
    $config = $config->getProfileConfig($profile, $this->getRequires(), $this->getRequireDevs());
 | 
						|
    // corriger les liens
 | 
						|
    $deps = cl::merge(array_keys($config["require"]), array_keys($config["require-dev"]));
 | 
						|
    $paths = [];
 | 
						|
    foreach ($deps as $dep) {
 | 
						|
      $path = cl::get(self::PATHS, $dep, $dep);
 | 
						|
      $path = str_replace("/", "-", $path);
 | 
						|
      $path = "../$path";
 | 
						|
      $paths[$dep] = $path;
 | 
						|
    }
 | 
						|
    if ($config["link"]) {
 | 
						|
      // Ajouter les liens
 | 
						|
      $adds = [];
 | 
						|
      $repositories = $this->getRepositories();
 | 
						|
      foreach ($deps as $dep) {
 | 
						|
        $found = false;
 | 
						|
        foreach ($repositories as $repository) {
 | 
						|
          if ($repository["type"] === "path" && $repository["url"] === $paths[$dep]) {
 | 
						|
            $found = true;
 | 
						|
            break;
 | 
						|
          }
 | 
						|
        }
 | 
						|
        if (!$found) {
 | 
						|
          $adds[] = [
 | 
						|
            "type" => "path",
 | 
						|
            "url" => $paths[$dep],
 | 
						|
          ];
 | 
						|
        }
 | 
						|
      }
 | 
						|
      if ($adds) {
 | 
						|
        $this->setRepositories(cl::merge($adds, $repositories));
 | 
						|
      }
 | 
						|
    } else {
 | 
						|
      // Supprimer les liens
 | 
						|
      $dels = [];
 | 
						|
      $repositories = $this->getRepositories();
 | 
						|
      foreach ($deps as $dep) {
 | 
						|
        foreach ($repositories as $key => $repository) {
 | 
						|
          if ($repository["type"] === "path" && $repository["url"] === $paths[$dep]) {
 | 
						|
            $dels[] = $key;
 | 
						|
            break;
 | 
						|
          }
 | 
						|
        }
 | 
						|
      }
 | 
						|
      if ($dels) {
 | 
						|
        foreach (array_reverse($dels) as $key) {
 | 
						|
          unset($repositories[$key]);
 | 
						|
        }
 | 
						|
        $this->setRepositories(array_values($repositories));
 | 
						|
      }
 | 
						|
    }
 | 
						|
    // corriger les versions
 | 
						|
    foreach ($config["require"] as $dep => $version) {
 | 
						|
      $this->data["require"][$dep] = $version;
 | 
						|
    }
 | 
						|
    foreach ($config["require-dev"] as $dep => $version) {
 | 
						|
      $this->data["require-dev"][$dep] = $version;
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  function getLocalDeps(): array {
 | 
						|
    $deps = cl::merge(array_keys($this->getRequires()), array_keys($this->getRequireDevs()));
 | 
						|
    $paths = [];
 | 
						|
    foreach ($deps as $dep) {
 | 
						|
      $path = cl::get(self::PATHS, $dep, $dep);
 | 
						|
      $path = str_replace("/", "-", $path);
 | 
						|
      $path = "../$path";
 | 
						|
      $paths[$dep] = $path;
 | 
						|
    }
 | 
						|
    $repositories = $this->getRepositories();
 | 
						|
    $localDeps = [];
 | 
						|
    foreach ($deps as $dep) {
 | 
						|
      foreach ($repositories as $key => $repository) {
 | 
						|
        if ($repository["type"] === "path" && $repository["url"] === $paths[$dep]) {
 | 
						|
          $localDeps[$dep] = $repository["url"];
 | 
						|
          break;
 | 
						|
        }
 | 
						|
      }
 | 
						|
    }
 | 
						|
    return $localDeps;
 | 
						|
  }
 | 
						|
 | 
						|
  function print(): void {
 | 
						|
    $contents = json::with($this->data, json::INDENT_TABS);
 | 
						|
    if ($contents) echo "$contents\n";
 | 
						|
  }
 | 
						|
 | 
						|
  function write(): void {
 | 
						|
    $contents = json::with($this->data, json::INDENT_TABS);
 | 
						|
    file::writer($this->composerFile)->putContents("$contents\n");
 | 
						|
  }
 | 
						|
}
 |