|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeDistortion\FluentDotEnv\DotEnvAdapters; |
| 4 | + |
| 5 | +use CodeDistortion\FluentDotEnv\Exceptions\GeneralException; |
| 6 | +use CodeDistortion\FluentDotEnv\Exceptions\InvalidPathException; |
| 7 | +use CodeDistortion\FluentDotEnv\Misc\GetenvSupport; |
| 8 | +use CodeDistortion\FluentDotEnv\Misc\ValueStore; |
| 9 | +use Throwable; |
| 10 | + |
| 11 | +/** |
| 12 | + * Abstract adapter to read .env files. |
| 13 | + */ |
| 14 | +abstract class AbstractDotEnvAdapter implements DotEnvAdapterInterface |
| 15 | +{ |
| 16 | + /** @var array<string, string> The original set of getenv() values. */ |
| 17 | + private $origGetEnv = []; |
| 18 | + |
| 19 | + /** @var array<string, string> The original set of $_ENV values. */ |
| 20 | + private $origEnv = []; |
| 21 | + |
| 22 | + /** @var array<string, string> The original set of $_SERVER values. */ |
| 23 | + private $origServer = []; |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + /** |
| 28 | + * Work out if the import process will update the getenv() values. |
| 29 | + * |
| 30 | + * If it doesn't then the process of backing up and clearing the getenv() values can be skipped. |
| 31 | + * |
| 32 | + * @return boolean |
| 33 | + */ |
| 34 | + abstract protected function importWillUpdateGetenvValues(): bool; |
| 35 | + |
| 36 | + /** |
| 37 | + * When going through the process of backing up and clearing the getenv() values, work out if the code should touch |
| 38 | + * only the variables defined in the .env file (which requires it to be loaded an extra time beforehand). |
| 39 | + * |
| 40 | + * PHP 7.0 and below can't get a list of the current env vars using getenv() (with no arguments). |
| 41 | + * |
| 42 | + * So getting the keys from the .env file allows us to override those values and replace them after without needing |
| 43 | + * to know the full list. |
| 44 | + * |
| 45 | + * @return boolean |
| 46 | + */ |
| 47 | + protected function shouldOnlyWorkWithVariablesDefinedInEnvFile(): bool |
| 48 | + { |
| 49 | + // look for PHP 7.0 or below |
| 50 | + return (bool) version_compare(PHP_VERSION, '7.1.0', '<'); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Load the values from the given .env file, and return them. |
| 57 | + * |
| 58 | + * NOTE: This MUST leave the getenv(), $_ENV, $_SERVER etc values as they were to begin with. |
| 59 | + * |
| 60 | + * @param string $path The path to the .env file. |
| 61 | + * @return ValueStore |
| 62 | + * @throws InvalidPathException When the directory or file could not be used. |
| 63 | + * @throws Throwable Rethrows any other Throwable exception. |
| 64 | + */ |
| 65 | + public function import(string $path): ValueStore |
| 66 | + { |
| 67 | + try { |
| 68 | + |
| 69 | + $this->recordCurrentEnvValues($path); |
| 70 | + $this->removeCurrentEnvValues(); |
| 71 | + $valueStore = $this->importValuesFromEnvFile($path); |
| 72 | + |
| 73 | + } catch (Throwable $e) { |
| 74 | + |
| 75 | + throw $this->exceptionIsBecauseFileCantBeOpened($e) |
| 76 | + ? InvalidPathException::invalidPath($path, $e) |
| 77 | + : $e; |
| 78 | + |
| 79 | + } finally { |
| 80 | + |
| 81 | + $valueStore = $valueStore ?? new ValueStore(); |
| 82 | + |
| 83 | + $keysJustOverridden = array_keys($valueStore->all()); |
| 84 | + $this->restoreOriginalEnvValues($keysJustOverridden); |
| 85 | + } |
| 86 | + |
| 87 | + return $valueStore; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Record the current environment values, to be restored later. |
| 94 | + * |
| 95 | + * @param string $path The path to the .env file. |
| 96 | + * @return void |
| 97 | + */ |
| 98 | + private function recordCurrentEnvValues(string $path) |
| 99 | + { |
| 100 | + $this->origEnv = $_ENV; |
| 101 | + $this->origServer = $_SERVER; |
| 102 | + |
| 103 | + if (!$this->importWillUpdateGetenvValues()) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + $this->origGetEnv = $this->shouldOnlyWorkWithVariablesDefinedInEnvFile() |
| 108 | + ? $this->resolveCurrentEnvVarsBasedOnKeysDefinedInEnvFile($path) |
| 109 | + : GetenvSupport::getAllGetenvVariables(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Generate an array of the current env variables, based on the keys defined in the .env file. |
| 114 | + * |
| 115 | + * @param string $path The path to the .env file. |
| 116 | + * @return array<string, string> |
| 117 | + */ |
| 118 | + private function resolveCurrentEnvVarsBasedOnKeysDefinedInEnvFile(string $path): array |
| 119 | + { |
| 120 | + $keys = $this->determineKeysDefinedInEnvFile($path); |
| 121 | + return GetenvSupport::getParticularGetenvVariables($keys); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Look into a dotenv file, and find out which keys it defines. |
| 126 | + * |
| 127 | + * @param string $path The path to the .env file. |
| 128 | + * @return string[] |
| 129 | + */ |
| 130 | + private function determineKeysDefinedInEnvFile(string $path): array |
| 131 | + { |
| 132 | + $envFileValues = $this->parseEnvFileForValues($path); |
| 133 | + |
| 134 | + return array_keys($envFileValues); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Parse the contents of a .env file for key value pairs. |
| 139 | + * |
| 140 | + * Used when using PHP 7.0 or below, to determine which keys are in the .env file. |
| 141 | + * |
| 142 | + * @param string $path The path to the .env file. |
| 143 | + * @return array<string, mixed> |
| 144 | + */ |
| 145 | + protected function parseEnvFileForValues(string $path): array |
| 146 | + { |
| 147 | + throw GeneralException::pleaseOverrideMethodInChildClass(static::class, __FUNCTION__); |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + /** |
| 153 | + * Remove all current environment values. |
| 154 | + * |
| 155 | + * @return void |
| 156 | + */ |
| 157 | + private function removeCurrentEnvValues() |
| 158 | + { |
| 159 | + $_ENV = $_SERVER = []; |
| 160 | + |
| 161 | + if (!$this->importWillUpdateGetenvValues()) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + $origGetEnvKeys = array_keys($this->origGetEnv); |
| 166 | + GetenvSupport::removeGetenvVariables($origGetEnvKeys); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + /** |
| 172 | + * Read the data from the given .env path. |
| 173 | + * |
| 174 | + * @param string $path The path to the .env file. |
| 175 | + * @return ValueStore |
| 176 | + */ |
| 177 | + abstract protected function importValuesFromEnvFile(string $path): ValueStore; |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + /** |
| 182 | + * Check if the given exception is because the .env file could not be opened. |
| 183 | + * |
| 184 | + * @param Throwable $e The exception to check. |
| 185 | + * @return boolean |
| 186 | + */ |
| 187 | + abstract protected function exceptionIsBecauseFileCantBeOpened(Throwable $e): bool; |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + /** |
| 192 | + * Restore the original environment values. |
| 193 | + * |
| 194 | + * @param string[] $keysJustOverridden The keys that were just overridden. |
| 195 | + * @return void |
| 196 | + */ |
| 197 | + private function restoreOriginalEnvValues(array $keysJustOverridden) |
| 198 | + { |
| 199 | + $_ENV = $this->origEnv; |
| 200 | + $_SERVER = $this->origServer; |
| 201 | + |
| 202 | + if (!$this->importWillUpdateGetenvValues()) { |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + // PHP 7.1 and 7.2 on Windows don't pick up keys with empty values |
| 207 | + // so explicitly remove the values here in case any were empty |
| 208 | + GetenvSupport::removeGetenvVariables($keysJustOverridden); |
| 209 | + |
| 210 | + $this->shouldOnlyWorkWithVariablesDefinedInEnvFile() |
| 211 | + ? GetenvSupport::addGetenvVariables($this->origGetEnv) |
| 212 | + : GetenvSupport::replaceAllGetenvVariables($this->origGetEnv); |
| 213 | + } |
| 214 | +} |
0 commit comments