nur-sery/lib/_launch.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2024-06-13 07:28:11 +04:00
#!/usr/bin/php
<?php
2024-06-13 17:19:58 +04:00
$internalUse = $argv[1] ?? null;
if ($internalUse !== "--internal-use") exit("Wrong args");
$paramsfile = $argv[2] ?? null;
2024-06-14 13:29:47 +04:00
if (!file_exists($paramsfile)) exit("Bad params file");
2024-06-13 17:19:58 +04:00
$argc -= 2;
$argv = array_merge(
array_slice($argv, 0, 1),
array_slice($argv, 3),
);
$app_params = unserialize(file_get_contents($paramsfile));
2024-06-17 17:44:43 +04:00
@unlink($paramsfile);
2024-06-13 17:19:58 +04:00
require $app_params["vendor"]["autoload"];
2024-06-13 07:28:11 +04:00
use nur\cli\Application;
2024-06-14 13:29:47 +04:00
use nur\sery\output\msg;
2024-06-13 07:28:11 +04:00
use nur\sery\wip\app\app;
2024-06-13 07:32:06 +04:00
use nur\sery\app\launcher;
2024-06-13 07:28:11 +04:00
use nur\yaml;
2024-06-13 17:19:58 +04:00
class _LaunchApp extends Application {
const NAME = "_launch";
2024-06-14 13:29:47 +04:00
const USE_LOGFILE = true;
2024-06-13 17:19:58 +04:00
2024-06-13 07:28:11 +04:00
const ACTION_INFOS = 0, ACTION_START = 1, ACTION_STOP = 2;
const ARGS = [
"merge" => parent::ARGS,
"purpose" => "lancer une tâche de fond",
"usage" => "ApplicationClass args...",
["-s", "--start", "name" => "action", "value" => self::ACTION_START,
"help" => "démarrer la tâche, c'est la valeur par défaut"
],
["-k", "--stop", "name" => "action", "value" => self::ACTION_STOP,
"help" => "arrêter la tâche"
],
["-i", "--infos", "name" => "action", "value" => self::ACTION_INFOS,
"help" => "afficher des informations sur la tâche"
],
];
protected $action = self::ACTION_START;
protected $args;
function main() {
$appClass = $this->args[0] ?? null;
if ($appClass === null) {
2024-06-14 13:29:47 +04:00
msg::error("Vous devez spécifier la classe de l'application");
self::die();
2024-06-13 15:11:02 +04:00
} elseif (!class_exists($appClass)) {
2024-06-14 13:29:47 +04:00
msg::error("$appClass: Cette classe n'existe pas");
self::die();
2024-06-13 07:28:11 +04:00
}
$args = array_slice($this->args, 1);
$useRunfile = constant("$appClass::USE_RUNFILE");
if (!$useRunfile) {
2024-06-14 13:29:47 +04:00
msg::error("Cette application ne supporte pas l'usage de runfile");
self::die();
2024-06-13 07:28:11 +04:00
}
2024-06-13 17:19:58 +04:00
$runfile = app::with($appClass, self::$internal_use_app_params)->getRunfile();
2024-06-13 07:28:11 +04:00
switch ($this->action) {
case self::ACTION_START:
launcher::_start($args, $runfile);
break;
case self::ACTION_STOP:
launcher::_stop($runfile);
break;
case self::ACTION_INFOS:
yaml::dump($runfile->read());
break;
}
}
2024-06-13 17:19:58 +04:00
}
_LaunchApp::internal_use_set_app_params($app_params);
_LaunchApp::run();