2024-09-23 17:18:00 +04:00
|
|
|
<?php
|
2024-09-25 17:10:14 +04:00
|
|
|
namespace nur\sery\wip\app\cli;
|
2024-09-23 17:18:00 +04:00
|
|
|
|
|
|
|
use Exception;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\cli\ArgsException;
|
|
|
|
use nur\cli\ArgsParser;
|
2024-09-23 17:18:00 +04:00
|
|
|
use nur\config;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\sery\ExitError;
|
|
|
|
use nur\sery\output\console;
|
|
|
|
use nur\sery\output\log;
|
|
|
|
use nur\sery\output\msg;
|
2024-09-23 17:18:00 +04:00
|
|
|
use nur\sery\output\std\StdMessenger;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\sery\ValueException;
|
2024-09-23 17:18:00 +04:00
|
|
|
use nur\sery\wip\app\app2;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\sery\wip\app\RunFile;
|
2024-09-23 17:18:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Application: application de base
|
|
|
|
*/
|
2024-09-25 17:10:14 +04:00
|
|
|
abstract class Application {
|
2024-09-23 17:18:00 +04:00
|
|
|
/** @var string répertoire du projet (celui qui contient composer.json */
|
|
|
|
const PROJDIR = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array répertoires vendor exprimés relativement à PROJDIR
|
2024-09-25 17:10:14 +04:00
|
|
|
*
|
|
|
|
* les clés suivantes doivent être présentes dans le tableau:
|
|
|
|
* - autoload (chemin vers vendor/autoload.php)
|
|
|
|
* - bindir (chemin vers vendor/bin)
|
2024-09-23 17:18:00 +04:00
|
|
|
*/
|
|
|
|
const VENDOR = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string code du projet, utilisé pour dériver le noms de certains des
|
2024-09-25 17:10:14 +04:00
|
|
|
* paramètres extraits de l'environnement, e.g XXX_YYY_DATADIR si le projet a
|
|
|
|
* pour code xxx-yyy
|
2024-09-23 17:18:00 +04:00
|
|
|
*
|
|
|
|
* si non définie, cette valeur est calculée automatiquement à partir de
|
|
|
|
* self::PROJDIR sans le suffixe "-app"
|
|
|
|
*/
|
|
|
|
const APPCODE = null;
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
/**
|
|
|
|
* @var string|null identifiant d'un groupe auquel l'application appartient.
|
|
|
|
* les applications du même groupe enregistrent leur fichiers de controle au
|
|
|
|
* même endroit $VARDIR/$APPGROUP
|
|
|
|
*/
|
|
|
|
const APPGROUP = null;
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
/**
|
|
|
|
* @var string code de l'application, utilisé pour inférer le nom de certains
|
|
|
|
* fichiers spécifiques à l'application.
|
|
|
|
*
|
|
|
|
* si non définie, cette valeur est calculée automatiquement à partir de
|
|
|
|
* static::class
|
|
|
|
*/
|
|
|
|
const NAME = null;
|
|
|
|
|
|
|
|
/** @var string description courte de l'application */
|
|
|
|
const TITLE = null;
|
|
|
|
|
|
|
|
const DATADIR = null;
|
|
|
|
const ETCDIR = null;
|
|
|
|
const VARDIR = null;
|
|
|
|
const LOGDIR = null;
|
|
|
|
|
|
|
|
/** @var bool faut-il activer automatiquement l'écriture dans les logs */
|
|
|
|
const USE_LOGFILE = null;
|
|
|
|
|
|
|
|
/** @var bool faut-il maintenir un fichier de suivi du process? */
|
|
|
|
const USE_RUNFILE = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool faut-il empêcher deux instances de cette application de se lancer
|
|
|
|
* en même temps?
|
|
|
|
*
|
|
|
|
* nécessite USE_RUNFILE==true
|
|
|
|
*/
|
|
|
|
const USE_RUNLOCK = false;
|
|
|
|
|
|
|
|
/** @var bool faut-il installer le gestionnaire de signaux? */
|
|
|
|
const USE_SIGNAL_HANDLER = false;
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
static function _manage_runfile(RunFile $runfile): void {
|
|
|
|
global $argc, $argv;
|
|
|
|
if ($argc <= 1 || $argv[1] !== "//") return;
|
|
|
|
array_splice($argv, 1, 1); $argc--;
|
|
|
|
$ec = 0;
|
|
|
|
switch ($argv[1] ?? "infos") {
|
|
|
|
case "release-lock":
|
|
|
|
case "release":
|
|
|
|
case "rl":
|
|
|
|
$runfile->release();
|
|
|
|
break;
|
|
|
|
case "infos":
|
|
|
|
case "i":
|
|
|
|
if ($runfile->isRunning()) {
|
|
|
|
$action = $runfile->getActionDesc();
|
|
|
|
if ($action !== null) $action = ": $action";
|
|
|
|
echo "running$action\n";
|
|
|
|
} else {
|
|
|
|
echo "not running\n";
|
|
|
|
$ec = 1;
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
2024-09-25 17:10:14 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fwrite(STDERR, "$argv[2]: unexpected command\n");
|
|
|
|
$ec = 123;
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
2024-09-25 17:10:14 +04:00
|
|
|
exit($ec);
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
static function run(?Application $app=null): void {
|
2024-09-23 17:18:00 +04:00
|
|
|
$unlock = false;
|
|
|
|
$stop = false;
|
2024-09-25 17:10:14 +04:00
|
|
|
register_shutdown_function(function() use (&$unlock, &$stop) {
|
2024-09-23 17:18:00 +04:00
|
|
|
if ($unlock) {
|
|
|
|
app2::get()->getRunfile()->release();
|
|
|
|
$unlock = false;
|
|
|
|
}
|
|
|
|
if ($stop) {
|
|
|
|
app2::get()->getRunfile()->wfStop();
|
|
|
|
$stop = false;
|
|
|
|
}
|
2024-09-25 17:10:14 +04:00
|
|
|
});
|
|
|
|
app2::install_signal_handler(static::USE_SIGNAL_HANDLER);
|
2024-09-23 17:18:00 +04:00
|
|
|
try {
|
2024-09-25 17:10:14 +04:00
|
|
|
static::_initialize_app();
|
|
|
|
$useRunfile = static::USE_RUNFILE;
|
|
|
|
$useRunlock = static::USE_RUNLOCK;
|
|
|
|
if ($useRunfile) {
|
2024-09-23 17:18:00 +04:00
|
|
|
$runfile = app2::get()->getRunfile();
|
2024-09-25 17:10:14 +04:00
|
|
|
|
|
|
|
self::_manage_runfile($runfile);
|
|
|
|
if ($useRunlock && $runfile->warnIfLocked()) exit(1);
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
$runfile->wfStart();
|
|
|
|
$stop = true;
|
|
|
|
if ($useRunlock) {
|
|
|
|
$runfile->lock();
|
|
|
|
$unlock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($app === null) $app = new static();
|
2024-09-25 17:10:14 +04:00
|
|
|
static::_configure_app($app);
|
|
|
|
static::_start_app($app);
|
2024-09-23 17:18:00 +04:00
|
|
|
} catch (ExitError $e) {
|
|
|
|
if ($e->haveMessage()) msg::error($e);
|
|
|
|
exit($e->getCode());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
msg::error($e);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
protected static function _initialize_app(): void {
|
|
|
|
app2::init(static::class);
|
|
|
|
msg::set_messenger(new StdMessenger([
|
|
|
|
"min_level" => msg::DEBUG,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _configure_app(Application $app): void {
|
|
|
|
config::configure(config::CONFIGURE_INITIAL_ONLY);
|
|
|
|
|
|
|
|
$msgs = null;
|
|
|
|
$msgs["console"] = new StdMessenger([
|
|
|
|
"min_level" => msg::NORMAL,
|
|
|
|
]);
|
|
|
|
if (static::USE_LOGFILE) {
|
|
|
|
$msgs["log"] = new StdMessenger([
|
|
|
|
"output" => app2::get()->getLogfile(),
|
|
|
|
"min_level" => msg::MINOR,
|
|
|
|
"add_date" => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
msg::init($msgs);
|
|
|
|
|
|
|
|
$app->parseArgs();
|
|
|
|
config::configure();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function _start_app(Application $app): void {
|
|
|
|
$retcode = $app->main();
|
|
|
|
if (is_int($retcode)) exit($retcode);
|
|
|
|
elseif (is_bool($retcode)) exit($retcode? 0: 1);
|
|
|
|
elseif ($retcode !== null) exit(strval($retcode));
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
/**
|
|
|
|
* sortir de l'application avec un code d'erreur, qui est 0 par défaut (i.e
|
|
|
|
* pas d'erreur)
|
|
|
|
*
|
|
|
|
* équivalent à lancer l'exception {@link ExitError}
|
|
|
|
*/
|
|
|
|
protected static final function exit(int $exitcode=0, $message=null) {
|
|
|
|
throw new ExitError($exitcode, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sortir de l'application avec un code d'erreur, qui vaut 1 par défaut (i.e
|
|
|
|
* une erreur s'est produite)
|
|
|
|
*
|
|
|
|
* équivalent à lancer l'exception {@link ExitError}
|
|
|
|
*/
|
|
|
|
protected static final function die($message=null, int $exitcode=1) {
|
|
|
|
throw new ExitError($exitcode, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PROFILE_SECTION = [
|
|
|
|
"title" => "PROFILS D'EXECUTION",
|
|
|
|
["group",
|
|
|
|
["-p", "--profile", "--app-profile",
|
|
|
|
"args" => 1, "argsdesc" => "PROFILE",
|
2024-09-25 17:10:14 +04:00
|
|
|
"action" => [app2::class, "set_profile"],
|
2024-09-23 17:18:00 +04:00
|
|
|
"help" => "spécifier le profil d'exécution",
|
|
|
|
],
|
2024-09-25 17:10:14 +04:00
|
|
|
["-P", "--prod", "action" => [app2::class, "set_profile", config::PROD]],
|
|
|
|
["-T", "--test", "action" => [app2::class, "set_profile", config::TEST]],
|
|
|
|
["--devel", "action" => [app2::class, "set_profile", config::DEVEL]],
|
2024-09-23 17:18:00 +04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
const VERBOSITY_SECTION = [
|
|
|
|
"title" => "NIVEAU D'INFORMATION",
|
|
|
|
"show" => false,
|
|
|
|
["group",
|
|
|
|
["--verbosity",
|
2024-09-25 17:10:14 +04:00
|
|
|
"args" => 1, "argsdesc" => "silent|quiet|verbose|debug",
|
2024-09-23 17:18:00 +04:00
|
|
|
"action" => [null, "set_application_verbosity"],
|
|
|
|
"help" => "spécifier le niveau d'informations affiché",
|
|
|
|
],
|
|
|
|
["-q", "--quiet", "action" => [null, "set_application_verbosity", "quiet"]],
|
|
|
|
["-v", "--verbose", "action" => [null, "set_application_verbosity", "verbose"]],
|
|
|
|
["-D", "--debug", "action" => [null, "set_application_verbosity", "debug"]],
|
|
|
|
],
|
|
|
|
["-L", "--logfile",
|
|
|
|
"args" => "file", "argsdesc" => "OUTPUT",
|
|
|
|
"action" => [null, "set_application_log_output"],
|
|
|
|
"help" => "Logger les messages de l'application dans le fichier spécifié",
|
|
|
|
],
|
|
|
|
["group",
|
|
|
|
["--color",
|
|
|
|
"action" => [null, "set_application_color", true],
|
|
|
|
"help" => "Afficher (resp. ne pas afficher) la sortie en couleur par défaut",
|
|
|
|
],
|
|
|
|
["--no-color", "action" => [null, "set_application_color", false]],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
static function set_application_verbosity(string $verbosity): void {
|
2024-09-25 17:10:14 +04:00
|
|
|
$console = console::get();
|
2024-09-23 17:18:00 +04:00
|
|
|
switch ($verbosity) {
|
|
|
|
case "Q":
|
2024-09-25 17:10:14 +04:00
|
|
|
case "silent":
|
|
|
|
$console->resetParams([
|
|
|
|
"min_level" => msg::NONE,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
case "q":
|
|
|
|
case "quiet":
|
2024-09-25 17:10:14 +04:00
|
|
|
$console->resetParams([
|
|
|
|
"min_level" => msg::MAJOR,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
2024-09-25 17:10:14 +04:00
|
|
|
break;
|
|
|
|
case "n":
|
|
|
|
case "normal":
|
|
|
|
$console->resetParams([
|
|
|
|
"min_level" => msg::NORMAL,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
case "v":
|
|
|
|
case "verbose":
|
2024-09-25 17:10:14 +04:00
|
|
|
$console->resetParams([
|
|
|
|
"min_level" => msg::MINOR,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
case "D":
|
|
|
|
case "debug":
|
|
|
|
config::set_debug();
|
2024-09-25 17:10:14 +04:00
|
|
|
$console->resetParams([
|
|
|
|
"min_level" => msg::DEBUG,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw ValueException::invalid_value($verbosity, "verbosity");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function set_application_log_output(string $logfile): void {
|
2024-09-25 17:10:14 +04:00
|
|
|
log::create_or_reset_params([
|
2024-09-23 17:18:00 +04:00
|
|
|
"output" => $logfile,
|
|
|
|
], StdMessenger::class, [
|
|
|
|
"add_date" => true,
|
2024-09-25 17:10:14 +04:00
|
|
|
"min_level" => log::MINOR,
|
2024-09-23 17:18:00 +04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
static function set_application_color(bool $color): void {
|
2024-09-25 17:10:14 +04:00
|
|
|
console::reset_params([
|
2024-09-23 17:18:00 +04:00
|
|
|
"color" => $color,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
const ARGS = [
|
|
|
|
"sections" => [
|
|
|
|
self::PROFILE_SECTION,
|
|
|
|
self::VERBOSITY_SECTION,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @throws ArgsException */
|
|
|
|
function parseArgs(array $args=null): void {
|
|
|
|
$parser = new ArgsParser(static::ARGS);
|
|
|
|
$parser->parse($this, $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PROFILE_COLORS = [
|
|
|
|
"prod" => "@r",
|
|
|
|
"test" => "@g",
|
|
|
|
"devel" => "@w",
|
|
|
|
];
|
|
|
|
const DEFAULT_PROFILE_COLOR = "y";
|
|
|
|
|
|
|
|
/** retourner le profil courant en couleur */
|
2024-09-25 17:10:14 +04:00
|
|
|
static function get_profile(?string $profile=null): string {
|
|
|
|
if ($profile === null) $profile = app2::get_profile();
|
2024-09-23 17:18:00 +04:00
|
|
|
foreach (static::PROFILE_COLORS as $text => $color) {
|
|
|
|
if (strpos($profile, $text) !== false) {
|
|
|
|
return $color? "<color $color>$profile</color>": $profile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$color = static::DEFAULT_PROFILE_COLOR;
|
|
|
|
return $color? "<color $color>$profile</color>": $profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract function main();
|
|
|
|
}
|