]> BookStack Code Mirror - system-cli/commitdiff
Updated env loading to be contained/controlled for usage
authorDan Brown <redacted>
Sat, 4 Mar 2023 15:26:27 +0000 (15:26 +0000)
committerDan Brown <redacted>
Thu, 9 Mar 2023 15:28:13 +0000 (15:28 +0000)
scripts/Commands/BackupCommand.php
scripts/Commands/InitCommand.php
scripts/Services/EnvironmentLoader.php [new file with mode: 0644]
scripts/run

index d41b36d4d5d82702671961f8c0f8d58e1e47b20e..8dcc3fa253149478a355b50a56850543b6be6855 100644 (file)
@@ -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;
         }
index 1041678ee9536ef18c68167c89f94cae11c4f04b..24a8d8134dc76cc6baf6da9eed5d661a895be9c8 100644 (file)
@@ -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 (file)
index 0000000..d95c683
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace Cli\Services;
+
+use Dotenv\Dotenv;
+
+class EnvironmentLoader
+{
+    public static function load(string $rootPath): array
+    {
+        $dotenv = Dotenv::createArrayBacked($rootPath);
+        return $dotenv->safeLoad();
+    }
+
+    public static function loadMergedWithCurrentEnv(string $rootPath): array
+    {
+        $env = static::load($rootPath);
+        foreach ($_SERVER as $key => $val) {
+            $env[$key] = $val;
+        }
+        return $env;
+    }
+}
index fb7e23ce646d5246ad9914bd3a0759e2289782dd..758d707ae985d17ac80c9c2e9140111ba2194b12 100644 (file)
@@ -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');