420 lines
12 KiB
PHP
420 lines
12 KiB
PHP
|
<?php
|
||
|
namespace nur\sery\wip\app;
|
||
|
|
||
|
use nur\b\ExitError;
|
||
|
use nur\cli\Application;
|
||
|
use nur\cli\Application2;
|
||
|
use nur\sery\A;
|
||
|
use nur\sery\app\LockFile;
|
||
|
use nur\sery\app\RunFile;
|
||
|
use nur\sery\cl;
|
||
|
use nur\sery\os\path;
|
||
|
use nur\sery\os\sh;
|
||
|
use nur\sery\output\msg;
|
||
|
use nur\sery\php\func;
|
||
|
use nur\sery\str;
|
||
|
use nur\sery\ValueException;
|
||
|
|
||
|
class app2 {
|
||
|
static ?func $is_bgapplication_enabled = null;
|
||
|
|
||
|
/**
|
||
|
* spécifier la fonction permettant de vérifier si l'exécution de tâches
|
||
|
* planifiées est autorisé. Par défaut, les tâches planifiées sont autorisées
|
||
|
*/
|
||
|
static function set_bgapplication_enabled_checker($func): void {
|
||
|
self::$is_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::$is_bgapplication_enabled === null) return;
|
||
|
$enabled = boolval(self::$is_bgapplication_enabled->invoke());
|
||
|
if (!$forceEnabled && !$enabled) {
|
||
|
msg::debug("Planifications désactivées. L'application n'a pas été lancée");
|
||
|
throw new ExitError();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#############################################################################
|
||
|
|
||
|
private static function isa_Application($app): bool {
|
||
|
if (!is_string($app)) return false;
|
||
|
return $app === Application::class || is_subclass_of($app, Application::class) ||
|
||
|
$app === Application2::class || is_subclass_of($app, Application2::class);
|
||
|
}
|
||
|
|
||
|
private static function get_params($app): array {
|
||
|
if ($app instanceof self) {
|
||
|
$params = $app->getParams();
|
||
|
} elseif ($app instanceof Application || $app instanceof Application2) {
|
||
|
$params = [
|
||
|
"projdir" => $app::PROJDIR,
|
||
|
"vendor" => $app::VENDOR,
|
||
|
"appcode" => $app::APPCODE,
|
||
|
"datadir" => $app::DATADIR,
|
||
|
"etcdir" => $app::ETCDIR,
|
||
|
"vardir" => $app::VARDIR,
|
||
|
"logdir" => $app::LOGDIR,
|
||
|
"apptype" => "cli",
|
||
|
"name" => $app::NAME,
|
||
|
"title" => $app::TITLE,
|
||
|
];
|
||
|
} elseif (self::isa_Application($app)) {
|
||
|
$params = [
|
||
|
"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"),
|
||
|
"apptype" => "cli",
|
||
|
"name" => constant("$app::NAME"),
|
||
|
"title" => constant("$app::TITLE"),
|
||
|
];
|
||
|
} elseif (is_array($app)) {
|
||
|
$params = $app;
|
||
|
} else {
|
||
|
throw ValueException::invalid_type($app, Application::class);
|
||
|
}
|
||
|
return $params;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param Application|string $app
|
||
|
* @param Application|string $proj
|
||
|
*/
|
||
|
static function with($app, $proj=null): self {
|
||
|
$params = self::get_params($app);
|
||
|
$proj_params = $proj !== null? self::get_params($proj): null;
|
||
|
if ($proj_params !== null) {
|
||
|
A::merge($params, cl::select($proj_params, [
|
||
|
"projdir",
|
||
|
"vendor",
|
||
|
"appcode",
|
||
|
"datadir",
|
||
|
"etcdir",
|
||
|
"vardir",
|
||
|
"logdir",
|
||
|
]));
|
||
|
}
|
||
|
return new static($params, $proj_params !== null);
|
||
|
}
|
||
|
|
||
|
protected static ?self $app = null;
|
||
|
|
||
|
static function init($app, $proj=null): void {
|
||
|
self::$app = static::with($app, $proj);
|
||
|
}
|
||
|
|
||
|
static function get(): self {
|
||
|
return self::$app ??= new self(null);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @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,
|
||
|
"cwd" => $cwd,
|
||
|
"datadir" => $datadir,
|
||
|
"etcdir" => $etcdir,
|
||
|
"vardir" => $vardir,
|
||
|
"logdir" => $logdir,
|
||
|
"profile" => $profile,
|
||
|
] = $params;
|
||
|
} else {
|
||
|
$projdir = path::abspath($params["projdir"] ?? ".");
|
||
|
$vendor = $params["vendor"] ?? self::DEFAULT_VENDOR;
|
||
|
$vendor["bindir"] = path::reljoin($projdir, $vendor["bindir"]);
|
||
|
$vendor["autoload"] = path::reljoin($projdir, $vendor["autoload"]);
|
||
|
$appcode = $params["appcode"] ?? "app";
|
||
|
$APPCODE = str_replace("-", "_", strtoupper($appcode));
|
||
|
# cwd
|
||
|
$cwd = getcwd();
|
||
|
# datadir
|
||
|
$datadir = getenv("${APPCODE}_DATADIR");
|
||
|
$datadirIsDefined = $datadir !== false;
|
||
|
if ($datadir === false) $datadir = $params["datadir"] ?? null;
|
||
|
if ($datadir === null) $datadir = "devel/data";
|
||
|
$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;
|
||
|
if ($profile === null) $profile = $datadirIsDefined? "prod": "devel";
|
||
|
}
|
||
|
$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;
|
||
|
|
||
|
# apptype, name, title
|
||
|
$this->apptype = $params["apptype"] ?? "cli";
|
||
|
$name = $params["name"] ?? null;
|
||
|
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);
|
||
|
$name = str::without_suffix("-app", str::camel2us($name, false, "-"));
|
||
|
}
|
||
|
$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;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param ?string|false $profile
|
||
|
*/
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
function fencedJoin(string $basedir, string $path): string {
|
||
|
$path = path::reljoin($basedir, $path);
|
||
|
if (!path::is_within($path, $basedir)) {
|
||
|
throw ValueException::invalid_value($path, "path");
|
||
|
}
|
||
|
return $path;
|
||
|
}
|
||
|
|
||
|
#############################################################################
|
||
|
# Paramètres spécifiques à cette application
|
||
|
|
||
|
protected string $apptype;
|
||
|
|
||
|
function getApptype(): string {
|
||
|
return $this->apptype;
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
"apptype" => $this->apptype,
|
||
|
"name" => $this->name,
|
||
|
"title" => $this->title,
|
||
|
];
|
||
|
}
|
||
|
|
||
|
function getEtcfile(?string $name=null, $profile=null): string {
|
||
|
if ($name === null) $name = "{$this->name}.conf";
|
||
|
return $this->findFile([$this->etcdir], [$name], $profile);
|
||
|
}
|
||
|
|
||
|
function getVarfile(?string $name=null, $profile=null): string {
|
||
|
if ($name === null) $name = "{$this->name}.tmp";
|
||
|
$file = $this->withProfile($this->fencedJoin($this->vardir, $name), $profile);
|
||
|
sh::mkdirof($file);
|
||
|
return $file;
|
||
|
}
|
||
|
|
||
|
function getLogfile(?string $name=null, $profile=null): string {
|
||
|
if ($name === null) $name = "{$this->name}.log";
|
||
|
$file = $this->withProfile($this->fencedJoin($this->logdir, $name), $profile);
|
||
|
sh::mkdirof($file);
|
||
|
return $file;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* obtenir le chemin absolu vers un fichier de travail
|
||
|
* - si le chemin est absolu, il est inchangé
|
||
|
* - si le chemin est qualifié (commence par ./ ou ../) ou sans chemin, il est
|
||
|
* exprimé par rapport à $vardir
|
||
|
* - sinon le chemin est exprimé par rapport au répertoire de travail de base
|
||
|
* $datadir
|
||
|
*
|
||
|
* is $ensure_dir, créer le répertoire du fichier s'il n'existe pas déjà
|
||
|
*/
|
||
|
function getWorkfile(?string $file, $profile=null, bool $ensureDir=true): ?string {
|
||
|
if ($file === null) return null;
|
||
|
if (path::is_qualified($file) || !path::have_dir($file)) {
|
||
|
$file = path::reljoin($this->vardir, $file);
|
||
|
} else {
|
||
|
$file = path::reljoin($this->datadir, $file);
|
||
|
}
|
||
|
$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
|
||
|
* - sinon le chemin est exprimé par rapport au répertoire de travail $vardir
|
||
|
*/
|
||
|
function getUserfile(?string $file): ?string {
|
||
|
if ($file === null) return null;
|
||
|
if (path::is_qualified($file)) {
|
||
|
return path::reljoin($this->cwd, $file);
|
||
|
} else {
|
||
|
return path::reljoin($this->vardir, $file);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected ?RunFile $runfile = null;
|
||
|
|
||
|
function getRunfile(): RunFile {
|
||
|
$name = $this->name;
|
||
|
$runfile = $this->getWorkfile($name);
|
||
|
$logfile = $this->getLogfile($name);
|
||
|
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];
|
||
|
}
|
||
|
}
|