modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-10-01 19:30:25 +04:00
parent 2456a48b38
commit 373910b170
4 changed files with 46 additions and 48 deletions

View File

@ -3,6 +3,7 @@ namespace nulib\app;
use nulib\A;
use nulib\app\cli\Application;
use nulib\app\config\ProfileManager;
use nulib\cl;
use nulib\ExitError;
use nulib\os\path;
@ -97,7 +98,7 @@ class app {
static function params_putenv(): void {
$params = serialize(self::get()->getParams());
putenv("NULIB_APP_app_params=". $params);
putenv("NULIB_APP_app_params=$params");
}
static function params_getenv(): ?array {
@ -107,9 +108,7 @@ class app {
}
static function get_profile(?bool &$productionMode=null): string {
$app = self::get();
$productionMode = $app->isProductionMode();
return $app->getProfile();
return self::get()->getProfile($productionMode);
}
static function set_profile(?string $profile=null, ?bool $productionMode=null): void {
@ -137,7 +136,6 @@ class app {
] = $params;
$cwd = $params["cwd"] ?? null;
$datadirIsDefined = true;
$profile = $params["profile"] ?? null;
} else {
# projdir
$projdir = $params["projdir"] ?? null;
@ -190,15 +188,16 @@ class app {
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;
}
# cwd
$cwd ??= getcwd();
# profile
$profile ??= $datadirIsDefined? "prod": "devel";
$this->profileManager = new ProfileManager([
"app" => true,
"name" => $appcode,
"default_profile" => $datadirIsDefined? "prod": "devel",
"profile" => $params["profile"] ?? null,
]);
$this->projdir = $projdir;
$this->vendor = $vendor;
@ -208,7 +207,6 @@ class app {
$this->etcdir = $etcdir;
$this->vardir = $vardir;
$this->logdir = $logdir;
$this->profile = $profile;
# name, title
$appgroup = $params["appgroup"] ?? null;
@ -283,25 +281,18 @@ class app {
return $this->logdir;
}
protected string $profile;
protected ProfileManager $profileManager;
function getProfile(): string {
return $this->profile;
function getProfile(?bool &$productionMode=null): string {
return $this->profileManager->getProfile($productionMode);
}
protected bool $productionMode;
function isProductionMode(): bool {
return $this->productionMode;
return $this->profileManager->isProductionMode();
}
function setProfile(?string $profile, ?bool $productionMode=null): void {
$profile ??= $this->profile;
$this->profile = $profile;
if ($productionMode === null) {
$productionMode = $profile === "prod" || $profile === "test";
}
$this->productionMode = $productionMode;
$this->profileManager->setProfile($profile, $productionMode);
}
/**
@ -312,7 +303,7 @@ class app {
*/
function withProfile(string $file, $profile): string {
if ($profile !== false) {
if ($profile === null) $profile = $this->getProfile();
$profile ??= $this->getProfile();
[$dir, $filename] = path::split($file);
$basename = path::basename($filename);
$ext = path::ext($file);
@ -386,7 +377,7 @@ class app {
"etcdir" => $this->etcdir,
"vardir" => $this->vardir,
"logdir" => $this->logdir,
"profile" => $this->le,
"profile" => $this->getProfile(),
"appgroup" => $this->appgroup,
"name" => $this->name,
"title" => $this->title,

View File

@ -10,15 +10,6 @@ use nulib\cl;
*/
class config {
protected static ConfigManager $config;
protected static ProfileManager $profile;
static function init(string $appcode): void {
self::$config = new ConfigManager();
self::$profile = new ProfileManager([
"app" => true,
"name" => $appcode,
]);
}
static function init_configurator($configurators): void {
self::$config->addConfigurators(cl::with($configurators));
@ -28,3 +19,9 @@ class config {
self::$config->configure($params);
}
}
new class extends config {
function __construct() {
self::$config = new ConfigManager();
}
};

View File

@ -55,6 +55,12 @@ class ProfileManager {
$this->envKeys = [$envKey];
}
}
$this->defaultProfile = $params["default_profile"] ?? null;
$profile = $params["profile"] ?? null;
$productionMode = $params["production_mode"] ?? null;
$productionMode ??= $this->productionModes[$profile] ?? false;
$this->profile = $profile;
$this->productionMode = $productionMode;
}
/**
@ -89,7 +95,7 @@ class ProfileManager {
return null;
}
protected ?string $defaultProfile = null;
protected ?string $defaultProfile;
function getDefaultProfile(): ?string {
return $this->defaultProfile;
@ -99,9 +105,9 @@ class ProfileManager {
$this->defaultProfile = $profile;
}
protected ?string $profile = null;
protected ?string $profile;
protected bool $productionMode = false;
protected bool $productionMode;
protected function resolveProfile(): void {
$profile ??= $this->getenvProfile();
@ -122,6 +128,10 @@ class ProfileManager {
return $this->profile;
}
function isProductionMode(): bool {
return $this->productionMode;
}
function setProfile(?string $profile=null, ?bool $productionMode=null): void {
if ($profile === null) $this->profile = null;
$profile ??= $this->getProfile($productionMode);

View File

@ -1,10 +1,10 @@
<?php
namespace nulib {
namespace nulib\app {
use nulib\tests\TestCase;
use nulib\impl\config;
use nulib\impl\myapp;
use nulib\impl\MyApplication1;
use nulib\impl\MyApplication2;
use nulib\app\impl\config;
use nulib\app\impl\myapp;
use nulib\app\impl\MyApplication1;
use nulib\app\impl\MyApplication2;
class appTest extends TestCase {
function testWith() {
@ -19,7 +19,7 @@ namespace nulib {
"bindir" => "$projdir/vendor/bin",
"autoload" => "$projdir/vendor/autoload.php",
],
"appcode" => "nur-sery",
"appcode" => "nur-ture",
"cwd" => $cwd,
"datadir" => "$projdir/devel",
"etcdir" => "$projdir/devel/etc",
@ -38,7 +38,7 @@ namespace nulib {
"bindir" => "$projdir/vendor/bin",
"autoload" => "$projdir/vendor/autoload.php",
],
"appcode" => "nur-sery",
"appcode" => "nur-ture",
"cwd" => $cwd,
"datadir" => "$projdir/devel",
"etcdir" => "$projdir/devel/etc",
@ -63,7 +63,7 @@ namespace nulib {
"bindir" => "$projdir/vendor/bin",
"autoload" => "$projdir/vendor/autoload.php",
],
"appcode" => "nur-sery",
"appcode" => "nur-ture",
"cwd" => $cwd,
"datadir" => "$projdir/devel",
"etcdir" => "$projdir/devel/etc",
@ -82,7 +82,7 @@ namespace nulib {
"bindir" => "$projdir/vendor/bin",
"autoload" => "$projdir/vendor/autoload.php",
],
"appcode" => "nur-sery",
"appcode" => "nur-ture",
"cwd" => $cwd,
"datadir" => "$projdir/devel",
"etcdir" => "$projdir/devel/etc",
@ -97,14 +97,14 @@ namespace nulib {
}
}
namespace nulib\impl {
namespace nulib\app\impl {
use nulib\app\cli\Application;
use nulib\os\path;
use nulib\app\app;
class config {
const PROJDIR = __DIR__.'/..';
const PROJDIR = __DIR__.'/../..';
static function get_projdir(): string {
return path::abspath(self::PROJDIR);