2024-09-23 17:18:00 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\wip\app;
|
|
|
|
|
|
|
|
use nur\sery\A;
|
|
|
|
use nur\sery\cl;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\sery\ExitError;
|
2024-09-23 17:18:00 +04:00
|
|
|
use nur\sery\os\path;
|
|
|
|
use nur\sery\os\sh;
|
|
|
|
use nur\sery\php\func;
|
|
|
|
use nur\sery\str;
|
|
|
|
use nur\sery\ValueException;
|
2024-09-25 17:10:14 +04:00
|
|
|
use nur\sery\wip\app\cli\Application;
|
2024-09-23 17:18:00 +04:00
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
#XXX une réécriture de app, qui remplacera app à terme
|
2024-09-23 17:18:00 +04:00
|
|
|
class app2 {
|
|
|
|
private static function isa_Application($app): bool {
|
|
|
|
if (!is_string($app)) return false;
|
2024-09-25 17:10:14 +04:00
|
|
|
return $app === Application::class || is_subclass_of($app, Application::class);
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static function get_params($app): array {
|
|
|
|
if ($app instanceof self) {
|
|
|
|
$params = $app->getParams();
|
2024-09-25 17:10:14 +04:00
|
|
|
} elseif ($app instanceof Application) {
|
2024-09-24 22:15:15 +04:00
|
|
|
$class = get_class($app);
|
2024-09-23 17:18:00 +04:00
|
|
|
$params = [
|
2024-09-24 22:15:15 +04:00
|
|
|
"class" => $class,
|
2024-09-23 17:18:00 +04:00
|
|
|
"projdir" => $app::PROJDIR,
|
|
|
|
"vendor" => $app::VENDOR,
|
|
|
|
"appcode" => $app::APPCODE,
|
|
|
|
"datadir" => $app::DATADIR,
|
|
|
|
"etcdir" => $app::ETCDIR,
|
|
|
|
"vardir" => $app::VARDIR,
|
|
|
|
"logdir" => $app::LOGDIR,
|
2024-09-24 22:15:15 +04:00
|
|
|
"appgroup" => $app::APPGROUP,
|
2024-09-23 17:18:00 +04:00
|
|
|
"name" => $app::NAME,
|
|
|
|
"title" => $app::TITLE,
|
|
|
|
];
|
|
|
|
} elseif (self::isa_Application($app)) {
|
2024-09-24 22:15:15 +04:00
|
|
|
$class = $app;
|
2024-09-23 17:18:00 +04:00
|
|
|
$params = [
|
2024-09-24 22:15:15 +04:00
|
|
|
"class" => $class,
|
2024-09-23 17:18:00 +04:00
|
|
|
"projdir" => constant("$app::PROJDIR"),
|
|
|
|
"vendor" => constant("$app::VENDOR"),
|
|
|
|
"appcode" => constant("$app::APPCODE"),
|
|
|
|
"datadir" => constant("$app::DATADIR"),
|
|
|
|
"etcdir" => constant("$app::ETCDIR"),
|
|
|
|
"vardir" => constant("$app::VARDIR"),
|
|
|
|
"logdir" => constant("$app::LOGDIR"),
|
2024-09-24 22:15:15 +04:00
|
|
|
"appgroup" => constant("$app::APPGROUP"),
|
2024-09-23 17:18:00 +04:00
|
|
|
"name" => constant("$app::NAME"),
|
|
|
|
"title" => constant("$app::TITLE"),
|
|
|
|
];
|
|
|
|
} elseif (is_array($app)) {
|
|
|
|
$params = $app;
|
|
|
|
} else {
|
|
|
|
throw ValueException::invalid_type($app, Application::class);
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2024-09-24 22:15:15 +04:00
|
|
|
protected static ?self $app = null;
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
/**
|
2024-09-24 22:15:15 +04:00
|
|
|
* @param Application|string|array $app
|
|
|
|
* @param Application|string|array|null $proj
|
2024-09-23 17:18:00 +04:00
|
|
|
*/
|
|
|
|
static function with($app, $proj=null): self {
|
|
|
|
$params = self::get_params($app);
|
2024-09-25 17:10:14 +04:00
|
|
|
$proj ??= self::params_getenv();
|
2024-09-24 22:15:15 +04:00
|
|
|
$proj ??= self::$app;
|
2024-09-23 17:18:00 +04:00
|
|
|
$proj_params = $proj !== null? self::get_params($proj): null;
|
|
|
|
if ($proj_params !== null) {
|
|
|
|
A::merge($params, cl::select($proj_params, [
|
|
|
|
"projdir",
|
|
|
|
"vendor",
|
|
|
|
"appcode",
|
2024-09-24 22:15:15 +04:00
|
|
|
"cwd",
|
2024-09-23 17:18:00 +04:00
|
|
|
"datadir",
|
|
|
|
"etcdir",
|
|
|
|
"vardir",
|
|
|
|
"logdir",
|
2024-09-24 22:15:15 +04:00
|
|
|
"profile",
|
2024-09-23 17:18:00 +04:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
return new static($params, $proj_params !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function init($app, $proj=null): void {
|
|
|
|
self::$app = static::with($app, $proj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function get(): self {
|
2024-09-25 17:10:14 +04:00
|
|
|
return self::$app ??= new static(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function params_putenv(): void {
|
|
|
|
$params = serialize(self::get()->getParams());
|
|
|
|
putenv("NULIB_APP_app_params=". $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function params_getenv(): ?array {
|
|
|
|
$params = getenv("NULIB_APP_app_params");
|
|
|
|
if ($params === false) return null;
|
|
|
|
return unserialize($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function get_profile(): string {
|
|
|
|
return self::get()->getProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function set_profile(?string $profile=null): void {
|
|
|
|
self::get()->setProfile($profile);
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array répertoires vendor exprimés relativement à PROJDIR
|
|
|
|
*/
|
|
|
|
const DEFAULT_VENDOR = [
|
|
|
|
"bindir" => "vendor/bin",
|
|
|
|
"autoload" => "vendor/autoload.php",
|
|
|
|
];
|
|
|
|
|
|
|
|
function __construct(?array $params, bool $useProjParams=false) {
|
|
|
|
if ($useProjParams) {
|
|
|
|
[
|
|
|
|
"projdir" => $projdir,
|
|
|
|
"vendor" => $vendor,
|
|
|
|
"appcode" => $appcode,
|
|
|
|
"datadir" => $datadir,
|
|
|
|
"etcdir" => $etcdir,
|
|
|
|
"vardir" => $vardir,
|
|
|
|
"logdir" => $logdir,
|
|
|
|
] = $params;
|
2024-09-24 22:15:15 +04:00
|
|
|
$cwd = $params["cwd"] ?? null;
|
|
|
|
$datadirIsDefined = true;
|
|
|
|
$profile = $params["profile"] ?? null;
|
2024-09-23 17:18:00 +04:00
|
|
|
} else {
|
2024-09-24 22:15:15 +04:00
|
|
|
# projdir
|
|
|
|
$projdir = $params["projdir"] ?? null;
|
|
|
|
if ($projdir === null) {
|
|
|
|
global $_composer_autoload_path, $_composer_bin_dir;
|
|
|
|
$autoload = $_composer_autoload_path ?? null;
|
|
|
|
$bindir = $_composer_bin_dir ?? null;
|
|
|
|
if ($autoload !== null) {
|
|
|
|
$vendor = preg_replace('/\/[^\/]+\.php$/', "", $autoload);
|
|
|
|
$bindir ??= "$vendor/bin";
|
|
|
|
$projdir = preg_replace('/\/[^\/]+$/', "", $vendor);
|
|
|
|
$params["vendor"] = [
|
|
|
|
"autoload" => $autoload,
|
|
|
|
"bindir" => $bindir,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($projdir === null) $projdir = ".";
|
|
|
|
$projdir = path::abspath($projdir);
|
|
|
|
# vendor
|
2024-09-23 17:18:00 +04:00
|
|
|
$vendor = $params["vendor"] ?? self::DEFAULT_VENDOR;
|
|
|
|
$vendor["bindir"] = path::reljoin($projdir, $vendor["bindir"]);
|
|
|
|
$vendor["autoload"] = path::reljoin($projdir, $vendor["autoload"]);
|
2024-09-24 22:15:15 +04:00
|
|
|
# appcode
|
|
|
|
$appcode = $params["appcode"] ?? null;
|
|
|
|
if ($appcode === null) {
|
|
|
|
$appcode = str::without_suffix("-app", path::basename($projdir));
|
|
|
|
}
|
2024-09-23 17:18:00 +04:00
|
|
|
$APPCODE = str_replace("-", "_", strtoupper($appcode));
|
|
|
|
# cwd
|
2024-09-24 22:15:15 +04:00
|
|
|
$cwd = $params["cwd"] ?? null;
|
2024-09-23 17:18:00 +04:00
|
|
|
# datadir
|
|
|
|
$datadir = getenv("${APPCODE}_DATADIR");
|
|
|
|
$datadirIsDefined = $datadir !== false;
|
|
|
|
if ($datadir === false) $datadir = $params["datadir"] ?? null;
|
2024-09-24 22:15:15 +04:00
|
|
|
if ($datadir === null) $datadir = "devel";
|
2024-09-23 17:18:00 +04:00
|
|
|
$datadir = path::reljoin($projdir, $datadir);
|
|
|
|
# etcdir
|
|
|
|
$etcdir = getenv("${APPCODE}_ETCDIR");
|
|
|
|
if ($etcdir === false) $etcdir = $params["etcdir"] ?? null;
|
|
|
|
if ($etcdir === null) $etcdir = "etc";
|
|
|
|
$etcdir = path::reljoin($datadir, $etcdir);
|
|
|
|
# vardir
|
|
|
|
$vardir = getenv("${APPCODE}_VARDIR");
|
|
|
|
if ($vardir === false) $vardir = $params["vardir"] ?? null;
|
|
|
|
if ($vardir === null) $vardir = "var";
|
|
|
|
$vardir = path::reljoin($datadir, $vardir);
|
|
|
|
# logdir
|
|
|
|
$logdir = getenv("${APPCODE}_LOGDIR");
|
|
|
|
if ($logdir === false) $logdir = $params["logdir"] ?? null;
|
|
|
|
if ($logdir === null) $logdir = "log";
|
|
|
|
$logdir = path::reljoin($datadir, $logdir);
|
|
|
|
# profile
|
|
|
|
$profile = getenv("${APPCODE}_PROFILE");
|
|
|
|
if ($profile === false) $profile = getenv("APP_PROFILE");
|
|
|
|
if ($profile === false) $profile = $params["profile"] ?? null;
|
|
|
|
}
|
2024-09-24 22:15:15 +04:00
|
|
|
# cwd
|
|
|
|
$cwd ??= getcwd();
|
|
|
|
# profile
|
|
|
|
$profile ??= $datadirIsDefined? "prod": "devel";
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
$this->projdir = $projdir;
|
|
|
|
$this->vendor = $vendor;
|
|
|
|
$this->appcode = $appcode;
|
|
|
|
$this->cwd = $cwd;
|
|
|
|
$this->datadir = $datadir;
|
|
|
|
$this->etcdir = $etcdir;
|
|
|
|
$this->vardir = $vardir;
|
|
|
|
$this->logdir = $logdir;
|
|
|
|
$this->profile = $profile;
|
|
|
|
|
2024-09-24 22:15:15 +04:00
|
|
|
# name, title
|
|
|
|
$appgroup = $params["appgroup"] ?? null;
|
|
|
|
$name = $params["name"] ?? $params["class"] ?? null;
|
2024-09-23 17:18:00 +04:00
|
|
|
if ($name === null) {
|
|
|
|
$name = $appcode;
|
|
|
|
} else {
|
|
|
|
# si $name est une classe, enlever le package et normaliser i.e
|
|
|
|
# my\package\MyApplication --> my-application
|
|
|
|
$name = preg_replace('/.*\\\\/', "", $name);
|
2024-09-24 22:15:15 +04:00
|
|
|
$name = str::camel2us($name, false, "-");
|
2024-09-25 17:10:14 +04:00
|
|
|
$name = str::without_suffix("-app", $name);
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
2024-09-24 22:15:15 +04:00
|
|
|
$this->appgroup = $appgroup;
|
2024-09-23 17:18:00 +04:00
|
|
|
$this->name = $name;
|
|
|
|
$this->title = $params["title"] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
# Paramètres partagés par tous les scripts d'un projet (et les scripts lancés
|
|
|
|
# à partir d'une application de ce projet)
|
|
|
|
|
|
|
|
protected string $projdir;
|
|
|
|
|
|
|
|
function getProjdir(): string {
|
|
|
|
return $this->projdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected array $vendor;
|
|
|
|
|
|
|
|
function getVendorBindir(): string {
|
|
|
|
return $this->vendor["bindir"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVendorAutoload(): string {
|
|
|
|
return $this->vendor["autoload"];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $appcode;
|
|
|
|
|
|
|
|
function getAppcode(): string {
|
|
|
|
return $this->appcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $cwd;
|
|
|
|
|
|
|
|
function getCwd(): string {
|
|
|
|
return $this->cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $datadir;
|
|
|
|
|
|
|
|
function getDatadir(): string {
|
|
|
|
return $this->datadir;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $etcdir;
|
|
|
|
|
|
|
|
function getEtcdir(): string {
|
|
|
|
return $this->etcdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $vardir;
|
|
|
|
|
|
|
|
function getVardir(): string {
|
|
|
|
return $this->vardir;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $logdir;
|
|
|
|
|
|
|
|
function getLogdir(): string {
|
|
|
|
return $this->logdir;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected string $profile;
|
|
|
|
|
|
|
|
function getProfile(): string {
|
|
|
|
return $this->profile;
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
function setProfile(?string $profile): void {
|
|
|
|
$profile ??= $this->profile;
|
|
|
|
$this->profile = $profile;
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:18:00 +04:00
|
|
|
/**
|
|
|
|
* @param ?string|false $profile
|
2024-09-24 22:15:15 +04:00
|
|
|
*
|
|
|
|
* false === pas de profil
|
|
|
|
* null === profil par défaut
|
2024-09-23 17:18:00 +04:00
|
|
|
*/
|
|
|
|
function withProfile(string $file, $profile): string {
|
|
|
|
if ($profile !== false) {
|
|
|
|
if ($profile === null) $profile = $this->getProfile();
|
|
|
|
[$dir, $filename] = path::split($file);
|
|
|
|
$basename = path::basename($filename);
|
|
|
|
$ext = path::ext($file);
|
|
|
|
$file = path::join($dir, "$basename.$profile$ext");
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function findFile(array $dirs, array $names, $profile=null): string {
|
|
|
|
# d'abord chercher avec le profil
|
|
|
|
if ($profile !== false) {
|
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
foreach ($names as $name) {
|
|
|
|
$file = path::join($dir, $name);
|
|
|
|
$file = $this->withProfile($file, $profile);
|
|
|
|
if (file_exists($file)) return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# puis sans profil
|
|
|
|
foreach ($dirs as $dir) {
|
|
|
|
foreach ($names as $name) {
|
|
|
|
$file = path::join($dir, $name);
|
|
|
|
if (file_exists($file)) return $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# la valeur par défaut est avec profil
|
|
|
|
return $this->withProfile(path::join($dirs[0], $names[0]), $profile);
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
function fencedJoin(string $basedir, ?string ...$paths): string {
|
|
|
|
$path = path::reljoin($basedir, ...$paths);
|
2024-09-23 17:18:00 +04:00
|
|
|
if (!path::is_within($path, $basedir)) {
|
|
|
|
throw ValueException::invalid_value($path, "path");
|
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
# Paramètres spécifiques à cette application
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
protected ?string $appgroup;
|
2024-09-23 17:18:00 +04:00
|
|
|
|
2024-09-24 22:15:15 +04:00
|
|
|
function getAppgroup(): ?string {
|
|
|
|
return $this->appgroup;
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected string $name;
|
|
|
|
|
|
|
|
function getName(): ?string {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ?string $title;
|
|
|
|
|
|
|
|
function getTitle(): ?string {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
# Méthodes outils
|
|
|
|
|
|
|
|
/** recréer le tableau des paramètres */
|
|
|
|
function getParams(): array {
|
|
|
|
return [
|
|
|
|
"projdir" => $this->projdir,
|
|
|
|
"vendor" => $this->vendor,
|
|
|
|
"appcode" => $this->appcode,
|
|
|
|
"cwd" => $this->cwd,
|
|
|
|
"datadir" => $this->datadir,
|
|
|
|
"etcdir" => $this->etcdir,
|
|
|
|
"vardir" => $this->vardir,
|
|
|
|
"logdir" => $this->logdir,
|
|
|
|
"profile" => $this->profile,
|
2024-09-24 22:15:15 +04:00
|
|
|
"appgroup" => $this->appgroup,
|
2024-09-23 17:18:00 +04:00
|
|
|
"name" => $this->name,
|
|
|
|
"title" => $this->title,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
/**
|
|
|
|
* obtenir le chemin vers le fichier de configuration. par défaut, retourner
|
|
|
|
* une valeur de la forme "$ETCDIR/$name[.$profile].conf"
|
|
|
|
*/
|
2024-09-23 17:18:00 +04:00
|
|
|
function getEtcfile(?string $name=null, $profile=null): string {
|
|
|
|
if ($name === null) $name = "{$this->name}.conf";
|
|
|
|
return $this->findFile([$this->etcdir], [$name], $profile);
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
/**
|
|
|
|
* obtenir le chemin vers le fichier de travail. par défaut, retourner une
|
|
|
|
* valeur de la forme "$VARDIR/$appgroup/$name[.$profile].tmp"
|
|
|
|
*/
|
2024-09-23 17:18:00 +04:00
|
|
|
function getVarfile(?string $name=null, $profile=null): string {
|
|
|
|
if ($name === null) $name = "{$this->name}.tmp";
|
2024-09-25 17:10:14 +04:00
|
|
|
$file = $this->fencedJoin($this->vardir, $this->appgroup, $name);
|
|
|
|
$file = $this->withProfile($file, $profile);
|
2024-09-23 17:18:00 +04:00
|
|
|
sh::mkdirof($file);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:10:14 +04:00
|
|
|
/**
|
|
|
|
* obtenir le chemin vers le fichier de log. par défaut, retourner une
|
|
|
|
* valeur de la forme "$LOGDIR/$appgroup/$name.log" (sans le profil, parce
|
|
|
|
* qu'il s'agit du fichier de log par défaut)
|
|
|
|
*
|
|
|
|
* Si $name est spécifié, la valeur retournée sera de la forme
|
|
|
|
* "$LOGDIR/$appgroup/$basename[.$profile].$ext"
|
|
|
|
*/
|
2024-09-23 17:18:00 +04:00
|
|
|
function getLogfile(?string $name=null, $profile=null): string {
|
2024-09-25 17:10:14 +04:00
|
|
|
if ($name === null) {
|
|
|
|
$name = "{$this->name}.log";
|
|
|
|
$profile ??= false;
|
|
|
|
}
|
|
|
|
$file = $this->fencedJoin($this->logdir, $this->appgroup, $name);
|
|
|
|
$file = $this->withProfile($file, $profile);
|
2024-09-23 17:18:00 +04:00
|
|
|
sh::mkdirof($file);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* obtenir le chemin absolu vers un fichier de travail
|
|
|
|
* - si le chemin est absolu, il est inchangé
|
2024-09-25 17:10:14 +04:00
|
|
|
* - sinon le chemin est exprimé par rapport à $vardir/$appgroup
|
|
|
|
*
|
|
|
|
* is $ensureDir, créer le répertoire du fichier s'il n'existe pas déjà
|
2024-09-23 17:18:00 +04:00
|
|
|
*
|
2024-09-25 17:10:14 +04:00
|
|
|
* la différence est avec {@link self::getVarfile()} est que le fichier peut
|
|
|
|
* au final être situé ailleurs que dans $vardir. de plus, il n'y a pas de
|
|
|
|
* valeur par défaut pour $file
|
2024-09-23 17:18:00 +04:00
|
|
|
*/
|
2024-09-25 17:10:14 +04:00
|
|
|
function getWorkfile(string $file, $profile=null, bool $ensureDir=true): string {
|
|
|
|
$file = path::reljoin($this->vardir, $this->appgroup, $file);
|
2024-09-23 17:18:00 +04:00
|
|
|
$file = $this->withProfile($file, $profile);
|
|
|
|
if ($ensureDir) sh::mkdirof($file);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* obtenir le chemin absolu vers un fichier spécifié par l'utilisateur.
|
|
|
|
* - si le chemin commence par /, il est laissé en l'état
|
|
|
|
* - si le chemin commence par ./ ou ../, il est exprimé par rapport à $cwd
|
2024-09-25 17:10:14 +04:00
|
|
|
* - sinon le chemin est exprimé par rapport à $vardir/$appgroup
|
|
|
|
*
|
|
|
|
* la différence est avec {@link self::getVarfile()} est que le fichier peut
|
|
|
|
* au final être situé ailleurs que dans $vardir. de plus, il n'y a pas de
|
|
|
|
* valeur par défaut pour $file
|
2024-09-23 17:18:00 +04:00
|
|
|
*/
|
2024-09-25 17:10:14 +04:00
|
|
|
function getUserfile(string $file): string {
|
2024-09-23 17:18:00 +04:00
|
|
|
if (path::is_qualified($file)) {
|
|
|
|
return path::reljoin($this->cwd, $file);
|
|
|
|
} else {
|
2024-09-25 17:10:14 +04:00
|
|
|
return path::reljoin($this->vardir, $this->appgroup, $file);
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ?RunFile $runfile = null;
|
|
|
|
|
|
|
|
function getRunfile(): RunFile {
|
|
|
|
$name = $this->name;
|
|
|
|
$runfile = $this->getWorkfile($name);
|
2024-09-25 17:10:14 +04:00
|
|
|
# ne pas spécifier $name pour avoir le fichier par défaut sans profil
|
|
|
|
$logfile = $this->getLogfile();
|
2024-09-23 17:18:00 +04:00
|
|
|
return $this->runfile ??= new RunFile($name, $runfile, $logfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ?array $lockFiles = null;
|
|
|
|
|
|
|
|
function getLockfile(?string $name=null): LockFile {
|
|
|
|
$this->lockFiles[$name] ??= $this->getRunfile()->getLockFile($name, $this->title);
|
|
|
|
return $this->lockFiles[$name];
|
|
|
|
}
|
2024-09-25 17:16:14 +04:00
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
|
2024-09-26 10:54:43 +04:00
|
|
|
const EC_UNDEAD = 247;
|
|
|
|
const EC_REAPABLE = 248;
|
|
|
|
const EC_FORK_CHILD = 249;
|
|
|
|
const EC_FORK_PARENT = 250;
|
2024-09-25 17:25:09 +04:00
|
|
|
const EC_DISABLED = 251;
|
2024-09-25 17:21:56 +04:00
|
|
|
const EC_LOCKED = 252;
|
|
|
|
const EC_BAD_COMMAND = 253;
|
|
|
|
const EC_EXCEPTION = 254;
|
|
|
|
const EC_SIGNAL = 255;
|
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
|
2024-09-25 17:16:14 +04:00
|
|
|
static bool $dispach_signals = false;
|
|
|
|
|
|
|
|
static function install_signal_handler(bool $allow=true): void {
|
|
|
|
if (!$allow) return;
|
|
|
|
$signalHandler = function(int $signo, $siginfo) {
|
2024-09-25 17:21:56 +04:00
|
|
|
throw new ExitError(self::EC_SIGNAL);
|
2024-09-25 17:16:14 +04:00
|
|
|
};
|
|
|
|
pcntl_signal(SIGHUP, $signalHandler);
|
|
|
|
pcntl_signal(SIGINT, $signalHandler);
|
|
|
|
pcntl_signal(SIGQUIT, $signalHandler);
|
|
|
|
pcntl_signal(SIGTERM, $signalHandler);
|
|
|
|
self::$dispach_signals = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function _dispatch_signals() {
|
|
|
|
if (self::$dispach_signals) pcntl_signal_dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
|
|
|
|
static ?func $bgapplication_enabled = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* spécifier la fonction permettant de vérifier si l'exécution de tâches
|
|
|
|
* de fond est autorisée. Si cette méthode n'est pas utilisée, par défaut,
|
|
|
|
* les tâches planifiées sont autorisées
|
|
|
|
*
|
|
|
|
* si $func===true, spécifier une fonction qui retourne toujours vrai
|
|
|
|
* si $func===false, spécifiée une fonction qui retourne toujours faux
|
|
|
|
* sinon, $func doit être une fonction valide
|
|
|
|
*/
|
|
|
|
static function set_bgapplication_enabled($func): void {
|
|
|
|
if (is_bool($func)) {
|
|
|
|
$enabled = $func;
|
|
|
|
$func = function () use ($enabled) {
|
|
|
|
return $enabled;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self::$bgapplication_enabled = func::with($func);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Si les exécutions en tâche de fond sont autorisée, retourner. Sinon
|
|
|
|
* afficher une erreur et quitter l'application
|
|
|
|
*/
|
|
|
|
static function check_bgapplication_enabled(bool $forceEnabled=false): void {
|
|
|
|
if (self::$bgapplication_enabled === null) return;
|
|
|
|
$enabled = boolval(self::$bgapplication_enabled->invoke());
|
|
|
|
if (!$forceEnabled && !$enabled) {
|
2024-09-25 17:25:09 +04:00
|
|
|
throw new ExitError(self::EC_DISABLED, "Planifications désactivées. La tâche n'a pas été lancée");
|
2024-09-25 17:16:14 +04:00
|
|
|
}
|
|
|
|
}
|
2024-09-26 10:54:43 +04:00
|
|
|
|
|
|
|
#############################################################################
|
|
|
|
|
|
|
|
static function action(?string $title, ?int $maxSteps=null): void {
|
|
|
|
self::get()->getRunfile()->action($title, $maxSteps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function step(int $nbSteps=1): void {
|
|
|
|
self::get()->getRunfile()->step($nbSteps);
|
|
|
|
}
|
2024-09-23 17:18:00 +04:00
|
|
|
}
|