From ccb81d35a6b8080123c92e3fb8ae301396023661 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 24 Sep 2024 22:15:15 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- tests/wip/app/app2Test.php | 127 +++++++++++++++++++++++++++++++++++++ wip/app/app2.php | 105 ++++++++++++++++++++++-------- 2 files changed, 204 insertions(+), 28 deletions(-) create mode 100644 tests/wip/app/app2Test.php diff --git a/tests/wip/app/app2Test.php b/tests/wip/app/app2Test.php new file mode 100644 index 0000000..661cf36 --- /dev/null +++ b/tests/wip/app/app2Test.php @@ -0,0 +1,127 @@ + $projdir, + "vendor" => [ + "bindir" => "$projdir/vendor/bin", + "autoload" => "$projdir/vendor/autoload.php", + ], + "appcode" => "nur-sery", + "cwd" => $cwd, + "datadir" => "$projdir/devel", + "etcdir" => "$projdir/devel/etc", + "vardir" => "$projdir/devel/var", + "logdir" => "$projdir/devel/log", + "profile" => "devel", + "name" => "my-application1", + "title" => null, + ], $app1->getParams()); + + $app2 = myapp2::with(MyApplication2::class, $app1); + self::assertSame([ + "projdir" => $projdir, + "vendor" => [ + "bindir" => "$projdir/vendor/bin", + "autoload" => "$projdir/vendor/autoload.php", + ], + "appcode" => "nur-sery", + "cwd" => $cwd, + "datadir" => "$projdir/devel", + "etcdir" => "$projdir/devel/etc", + "vardir" => "$projdir/devel/var", + "logdir" => "$projdir/devel/log", + "profile" => "devel", + "name" => "my-application2", + "title" => null, + ], $app2->getParams()); + } + + function testInit() { + $projdir = config::get_projdir(); + $cwd = getcwd(); + + myapp2::reset(); + myapp2::init(MyApplication1::class); + self::assertSame([ + "projdir" => $projdir, + "vendor" => [ + "bindir" => "$projdir/vendor/bin", + "autoload" => "$projdir/vendor/autoload.php", + ], + "appcode" => "nur-sery", + "cwd" => $cwd, + "datadir" => "$projdir/devel", + "etcdir" => "$projdir/devel/etc", + "vardir" => "$projdir/devel/var", + "logdir" => "$projdir/devel/log", + "profile" => "devel", + "name" => "my-application1", + "title" => null, + ], myapp2::get()->getParams()); + + myapp2::init(MyApplication2::class); + self::assertSame([ + "projdir" => $projdir, + "vendor" => [ + "bindir" => "$projdir/vendor/bin", + "autoload" => "$projdir/vendor/autoload.php", + ], + "appcode" => "nur-sery", + "cwd" => $cwd, + "datadir" => "$projdir/devel", + "etcdir" => "$projdir/devel/etc", + "vardir" => "$projdir/devel/var", + "logdir" => "$projdir/devel/log", + "profile" => "devel", + "name" => "my-application2", + "title" => null, + ], myapp2::get()->getParams()); + } + } +} + +namespace nur\sery\wip\app\impl { + use nur\cli\Application2; + use nur\sery\os\path; + use nur\sery\wip\app\app2; + + class config { + const PROJDIR = __DIR__.'/../../..'; + + static function get_projdir(): string { + return path::abspath(self::PROJDIR); + } + } + + class myapp2 extends app2 { + static function reset(): void { + self::$app = null; + } + } + + class MyApplication1 extends Application2 { + const PROJDIR = config::PROJDIR; + + function main() { + } + } + class MyApplication2 extends Application2 { + const PROJDIR = null; + + function main() { + } + } +} diff --git a/wip/app/app2.php b/wip/app/app2.php index 76e940c..02d6ea2 100644 --- a/wip/app/app2.php +++ b/wip/app/app2.php @@ -16,14 +16,25 @@ use nur\sery\str; use nur\sery\ValueException; class app2 { - static ?func $is_bgapplication_enabled = null; + static ?func $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 + * 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_checker($func): void { - self::$is_bgapplication_enabled = func::with($func); + 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); } /** @@ -31,10 +42,10 @@ class app2 { * 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 (self::$bgapplication_enabled === null) return; + $enabled = boolval(self::$bgapplication_enabled->invoke()); if (!$forceEnabled && !$enabled) { - msg::debug("Planifications désactivées. L'application n'a pas été lancée"); + msg::debug("Planifications désactivées. La tâche n'a pas été lancée"); throw new ExitError(); } } @@ -51,7 +62,9 @@ class app2 { if ($app instanceof self) { $params = $app->getParams(); } elseif ($app instanceof Application || $app instanceof Application2) { + $class = get_class($app); $params = [ + "class" => $class, "projdir" => $app::PROJDIR, "vendor" => $app::VENDOR, "appcode" => $app::APPCODE, @@ -59,12 +72,14 @@ class app2 { "etcdir" => $app::ETCDIR, "vardir" => $app::VARDIR, "logdir" => $app::LOGDIR, - "apptype" => "cli", + "appgroup" => $app::APPGROUP, "name" => $app::NAME, "title" => $app::TITLE, ]; } elseif (self::isa_Application($app)) { + $class = $app; $params = [ + "class" => $class, "projdir" => constant("$app::PROJDIR"), "vendor" => constant("$app::VENDOR"), "appcode" => constant("$app::APPCODE"), @@ -72,7 +87,7 @@ class app2 { "etcdir" => constant("$app::ETCDIR"), "vardir" => constant("$app::VARDIR"), "logdir" => constant("$app::LOGDIR"), - "apptype" => "cli", + "appgroup" => constant("$app::APPGROUP"), "name" => constant("$app::NAME"), "title" => constant("$app::TITLE"), ]; @@ -84,29 +99,32 @@ class app2 { return $params; } + protected static ?self $app = null; + /** - * @param Application|string $app - * @param Application|string $proj + * @param Application|string|array $app + * @param Application|string|array|null $proj */ static function with($app, $proj=null): self { $params = self::get_params($app); + $proj ??= self::$app; $proj_params = $proj !== null? self::get_params($proj): null; if ($proj_params !== null) { A::merge($params, cl::select($proj_params, [ "projdir", "vendor", "appcode", + "cwd", "datadir", "etcdir", "vardir", "logdir", + "profile", ])); } 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); } @@ -129,27 +147,50 @@ class app2 { "projdir" => $projdir, "vendor" => $vendor, "appcode" => $appcode, - "cwd" => $cwd, "datadir" => $datadir, "etcdir" => $etcdir, "vardir" => $vardir, "logdir" => $logdir, - "profile" => $profile, ] = $params; + $cwd = $params["cwd"] ?? null; + $datadirIsDefined = true; + $profile = $params["profile"] ?? null; } else { - $projdir = path::abspath($params["projdir"] ?? "."); + # 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 $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 + $appcode = $params["appcode"] ?? null; + if ($appcode === null) { + $appcode = str::without_suffix("-app", path::basename($projdir)); + } $APPCODE = str_replace("-", "_", strtoupper($appcode)); # cwd - $cwd = getcwd(); + $cwd = $params["cwd"] ?? null; # datadir $datadir = getenv("${APPCODE}_DATADIR"); $datadirIsDefined = $datadir !== false; if ($datadir === false) $datadir = $params["datadir"] ?? null; - if ($datadir === null) $datadir = "devel/data"; + if ($datadir === null) $datadir = "devel"; $datadir = path::reljoin($projdir, $datadir); # etcdir $etcdir = getenv("${APPCODE}_ETCDIR"); @@ -170,8 +211,12 @@ class app2 { $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"; } + # cwd + $cwd ??= getcwd(); + # profile + $profile ??= $datadirIsDefined? "prod": "devel"; + $this->projdir = $projdir; $this->vendor = $vendor; $this->appcode = $appcode; @@ -182,17 +227,18 @@ class app2 { $this->logdir = $logdir; $this->profile = $profile; - # apptype, name, title - $this->apptype = $params["apptype"] ?? "cli"; - $name = $params["name"] ?? null; + # name, title + $appgroup = $params["appgroup"] ?? null; + $name = $params["name"] ?? $params["class"] ?? 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, "-")); + $name = str::camel2us($name, false, "-"); } + $this->appgroup = $appgroup; $this->name = $name; $this->title = $params["title"] ?? null; } @@ -261,6 +307,9 @@ class app2 { /** * @param ?string|false $profile + * + * false === pas de profil + * null === profil par défaut */ function withProfile(string $file, $profile): string { if ($profile !== false) { @@ -306,10 +355,10 @@ class app2 { ############################################################################# # Paramètres spécifiques à cette application - protected string $apptype; + protected string $appgroup; - function getApptype(): string { - return $this->apptype; + function getAppgroup(): ?string { + return $this->appgroup; } protected string $name; @@ -339,7 +388,7 @@ class app2 { "vardir" => $this->vardir, "logdir" => $this->logdir, "profile" => $this->profile, - "apptype" => $this->apptype, + "appgroup" => $this->appgroup, "name" => $this->name, "title" => $this->title, ];