<?php
namespace nur\php;

use nur\cli\Application;
use nur\data\types\Metadata;
use nur\msg;
use nur\str;

class UpdateClassesApp extends Application {
  const MAPPINGS_SCHEMA = [[
    "package" => ["string", null, "package de base"],
    "path" => ["string", null, "répertoire source"],
    "classes" => ["array", null, "liste de classes à mettre à jour"],
    "allow_undefined" => ["bool", false, "autoriser les constantes non définies dans la classe?"],
  ]];
  const MAPPINGS = [];

  function main() {
    $updater = new SrcUpdater();

    $md = new Metadata(self::MAPPINGS_SCHEMA);
    $mappings = static::MAPPINGS;
    $md->ensureSchema($mappings);

    foreach ($mappings as $title => $mapping) {
      msg::section($title);
      [
        "package" => $package, "path" => $path, "classes" => $classes,
        "allow_undefined" => $allowUndefined,
      ] = $mapping;
      $updater->setAllowUndefined($allowUndefined);
      foreach ($classes as $class) {
        msg::info($class);
        $file = $class;
        $file = str::without_prefix($package, $file);
        $file = str_replace("\\", "/", $file);
        $file = "$path/$file.php";
        $updater->update($file);
      }
    }
  }
}
