[BUGFIX] Replace .env parsing with reading from environment 11/52311/5
authorHelmut Hummel <typo3@helhum.io>
Sat, 1 Apr 2017 14:24:47 +0000 (16:24 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Mon, 3 Apr 2017 11:05:03 +0000 (13:05 +0200)
Implementing a preliminary parsing code and having an
assumption on the existence of a .env file and a hardcoded
location of that file, makes TYPO3 again implement a speciality
instead of using standard tools.

Therefore the .env reading and parsing is removed in favor
of reading the values directly from the environment, which
gives users the option to choose their own .env solution, or rely
on just setting the env vars.

Resolves: #80626
Releases: master
Change-Id: Id386bb6248e4e814df7579cf848e5b115481b88a
Reviewed-on: https://review.typo3.org/52311
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/install/Classes/Controller/Action/Step/DatabaseConnect.php
typo3/sysext/install/Resources/Private/Partials/Action/Step/DatabaseConnect/ConnectDetails.html

index 946f0d5..473c6bd 100644 (file)
@@ -42,8 +42,8 @@ class DatabaseConnect extends AbstractStepAction
         $postValues = $this->postValues['values'];
         $defaultConnectionSettings = [];
 
-        if ($postValues['availableSet'] === 'configurationFromDotEnv') {
-            $defaultConnectionSettings = $this->getConfigurationFromDotEnv();
+        if ($postValues['availableSet'] === 'configurationFromEnvironment') {
+            $defaultConnectionSettings = $this->getConfigurationFromEnvironment();
         } else {
             if (isset($postValues['driver'])) {
                 $validDrivers = [
@@ -226,10 +226,10 @@ class DatabaseConnect extends AbstractStepAction
             }
         }
 
-        if (!empty($this->getConfigurationFromDotEnv())) {
+        if (!empty($this->getConfigurationFromEnvironment())) {
             $hasAtLeastOneOption = true;
-            $activeAvailableOption = 'configurationFromDotEnv';
-            $this->view->assign('hasConfigurationFromDotEnv', true);
+            $activeAvailableOption = 'configurationFromEnvironment';
+            $this->view->assign('hasConfigurationFromEnvironment', true);
         }
 
         $this->view->assign('hasAtLeastOneOption', $hasAtLeastOneOption);
@@ -306,45 +306,15 @@ class DatabaseConnect extends AbstractStepAction
      *
      * @return array Empty array if no file is found or connect is not successful, else working credentials
      */
-    protected function getConfigurationFromDotEnv(): array
+    protected function getConfigurationFromEnvironment(): array
     {
         $envCredentials = [];
-        if (file_exists(PATH_site . '/../.env')) {
-            $env = file_get_contents(PATH_site . '/../.env');
-            $envArray = explode("\n", $env);
-            foreach ($envArray as $val) {
-                if (strpos($val, 'TYPO3_DB_DRIVER') !== false) {
-                    $driver = substr($val, strpos($val, '=') + 1);
-                    $driver = trim($driver, " \t\n\r\0\x0B\"");
-                    $envCredentials['driver'] = $driver;
-                } elseif (strpos($val, 'TYPO3_DB_NAME') !== false) {
-                    $database = substr($val, strpos($val, '=') + 1);
-                    $database = trim($database, " \t\n\r\0\x0B\"");
-                    $envCredentials['database'] = $database;
-                } elseif (strpos($val, 'TYPO3_DB_HOST') !== false) {
-                    $host = substr($val, strpos($val, '=') + 1);
-                    $host = trim($host, " \t\n\r\0\x0B\"");
-                    $envCredentials['host'] = $host;
-                } elseif (strpos($val, 'TYPO3_DB_USER') !== false) {
-                    $user = substr($val, strpos($val, '=') + 1);
-                    $user = trim($user, " \t\n\r\0\x0B\"");
-                    $envCredentials['user'] = $user;
-                } elseif (strpos($val, 'TYPO3_DB_PASSWORD') !== false) {
-                    $password = substr($val, strpos($val, '=') + 1);
-                    $password = trim($password, " \t\n\r\0\x0B\"");
-                    $envCredentials['password'] = $password;
-                } elseif (strpos($val, 'TYPO3_DB_PORT') !== false) {
-                    $port = substr($val, strpos($val, '=') + 1);
-                    $port = trim($port, " \t\n\r\0\x0B\"");
-                    $envCredentials['port'] = $port;
-                } elseif (strpos($val, 'TYPO3_DB_SOCKET') !== false) {
-                    $port = substr($val, strpos($val, '=') + 1);
-                    $port = trim($port, " \t\n\r\0\x0B\"");
-                    $envCredentials['unix_socket'] = $port;
-                }
+        foreach (['driver', 'host', 'user', 'password', 'port', 'dbname', 'unix_socket'] as $value) {
+            $envVar = 'TYPO3_INSTALL_DB_' . strtoupper($value);
+            if (getenv($envVar) !== false) {
+                $envCredentials[$value] = getenv($envVar);
             }
         }
-
         if (!empty($envCredentials)) {
             $connectionParams = $envCredentials;
             $connectionParams['wrapperClass'] = Connection::class;
index 893baa7..10cece4 100644 (file)
                                                        '{activeAvailableOption} == postgresManualConfiguration', then: 'selected="selected"')}>Manually
                                                        configured PostgreSQL connection</option>
                                                </f:if>
-                                               <f:if condition="{hasConfigurationFromDotEnv}">
-                                                       <option value="configurationFromDotEnv" {f:if(condition:
-                                                       '{activeAvailableOption} == configurationFromDotEnv', then: 'selected="selected"')}>Configuration from
-                                                       .env file</option>
+                                               <f:if condition="{hasConfigurationFromEnvironment}">
+                                                       <option value="configurationFromEnvironment" {f:if(condition:
+                                                       '{activeAvailableOption} == configurationFromEnvironment', then: 'selected="selected"')}>Configuration from environment variables</option>
                                                </f:if>
                                        </select>
                                </div>
                                </div>
                        </f:if>
 
-                       <f:if condition="{hasConfigurationFromDotEnv}">
-                               <div id="configurationFromDotEnv" class="t3-install-driver-data">
+                       <f:if condition="{hasConfigurationFromEnvironment}">
+                               <div id="configurationFromEnvironment" class="t3-install-driver-data">
                                </div>
                        </f:if>