From: Dan Brown Date: Sat, 4 Mar 2023 15:26:27 +0000 (+0000) Subject: Updated env loading to be contained/controlled for usage X-Git-Url: http://source.bookstackapp.com/system-cli/commitdiff_plain/a2b168ac3c7fe9e12dfcafce12d5ada4ed13778f Updated env loading to be contained/controlled for usage --- diff --git a/scripts/Commands/BackupCommand.php b/scripts/Commands/BackupCommand.php index d41b36d..8dcc3fa 100644 --- a/scripts/Commands/BackupCommand.php +++ b/scripts/Commands/BackupCommand.php @@ -2,6 +2,7 @@ namespace Cli\Commands; +use Cli\Services\EnvironmentLoader; use Cli\Services\ProgramRunner; use Minicli\Command\CommandCall; use RecursiveDirectoryIterator; @@ -146,14 +147,15 @@ final class BackupCommand */ protected function createDatabaseDump(): string { + $envOptions = EnvironmentLoader::loadMergedWithCurrentEnv($this->appDir); $dbOptions = [ - 'host' => ($_SERVER['DB_HOST'] ?? ''), - 'username' => ($_SERVER['DB_USERNAME'] ?? ''), - 'password' => ($_SERVER['DB_PASSWORD'] ?? ''), - 'database' => ($_SERVER['DB_DATABASE'] ?? ''), + 'host' => ($envOptions['DB_HOST'] ?? ''), + 'username' => ($envOptions['DB_USERNAME'] ?? ''), + 'password' => ($envOptions['DB_PASSWORD'] ?? ''), + 'database' => ($envOptions['DB_DATABASE'] ?? ''), ]; - $port = $_SERVER['DB_PORT'] ?? ''; + $port = $envOptions['DB_PORT'] ?? ''; if ($port) { $dbOptions['host'] .= ':' . $port; } diff --git a/scripts/Commands/InitCommand.php b/scripts/Commands/InitCommand.php index 1041678..24a8d81 100644 --- a/scripts/Commands/InitCommand.php +++ b/scripts/Commands/InitCommand.php @@ -2,6 +2,7 @@ namespace Cli\Commands; +use Cli\Services\EnvironmentLoader; use Cli\Services\ProgramRunner; use Minicli\Command\CommandCall; @@ -15,10 +16,6 @@ class InitCommand $this->ensureRequiredExtensionInstalled(); // TODO - Ensure bookstack install deps are met? // TODO - Check composer and git exists before running - // TODO - Look at better way of handling env usage, on demand maybe where needed? - // Env loading in main `run` script if confilicting with certain bits here (app key generate, hence APP_KEY overload) - // See dotenv's Dotenv::createArrayBacked as way to go this. - // (More of a change for 'backup' command). // TODO - Potentially download composer? $suggestedOutPath = $input->subcommand; @@ -68,7 +65,7 @@ class InitCommand $errors = (new ProgramRunner('php', '/usr/bin/php')) ->withTimeout(60) ->withIdleTimeout(5) - ->withEnvironment(['APP_KEY' => 'SomeRandomString']) + ->withEnvironment(EnvironmentLoader::load($installDir)) ->runCapturingAllOutput([ $installDir . DIRECTORY_SEPARATOR . 'artisan', 'key:generate', '--force', '-n', '-q' diff --git a/scripts/Services/EnvironmentLoader.php b/scripts/Services/EnvironmentLoader.php new file mode 100644 index 0000000..d95c683 --- /dev/null +++ b/scripts/Services/EnvironmentLoader.php @@ -0,0 +1,23 @@ +safeLoad(); + } + + public static function loadMergedWithCurrentEnv(string $rootPath): array + { + $env = static::load($rootPath); + foreach ($_SERVER as $key => $val) { + $env[$key] = $val; + } + return $env; + } +} diff --git a/scripts/run b/scripts/run index fb7e23c..758d707 100644 --- a/scripts/run +++ b/scripts/run @@ -19,10 +19,6 @@ if (str_starts_with($scriptDir, 'phar://')) { } $bsDir = dirname($scriptDir); -// Load in our .env file from the parent directory -$dotenv = Dotenv\Dotenv::createImmutable($bsDir); -$dotenv->safeLoad(); - // Setup our CLI $app = new App(); $app->setSignature('./run');