diff --git a/src/EnvReader/BaseEnv.php b/src/EnvReader/BaseEnv.php index 4b4efd2..b096aed 100644 --- a/src/EnvReader/BaseEnv.php +++ b/src/EnvReader/BaseEnv.php @@ -38,6 +38,57 @@ abstract class BaseEnv { $this->properties = array_change_key_case($this->initProperties(), CASE_UPPER); $this->readEnv(); } + /** + * Initialize initial properties + * + * @return string[] Properties initial values (<property name> => <property value>) + */ + protected function initProperties (): array { + return [ + 'APP_DEV' => 0, + ]; + } + + /** + * Read the ENV file + * + * @throws Exception If ENV can't be read + */ + private function readEnv (): void { + if (!file_exists(static::PATH_ENV)) { + return; + } + + $lines = file(static::PATH_ENV, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if ($lines === false) { + throw new Exception('Unable to read environment file "' . static::PATH_ENV . '"'); + } + + foreach ($lines as $line) { + if (preg_match('/^\s*(?:(?#).*|(?[a-zA-Z0-9_-]+)=(?.+?))$/i', $line, $match) !== 1) { + continue; // Ligne invalide + } + if (isset($match['comment']) && trim($match['comment']) !== '') { + continue; // Commentaire + } + + if (preg_match('/^"(?.+)(?properties[mb_strtoupper($match['key'])] = $match['value']; + } + } + /** * Get all properties * @@ -78,6 +129,7 @@ abstract class BaseEnv { return $this->properties[$name]; } + /** * Get a property value as a boolean * @@ -195,41 +247,4 @@ abstract class BaseEnv { return $value; } - /** - * Initialize initial properties - * - * @return string[] Properties initial values (<property name> => <property value>) - */ - protected function initProperties (): array { - return [ - 'APP_DEV' => 0, - ]; - } - /** - * Read the ENV file - * - * @throws Exception If ENV can't be read - */ - private function readEnv (): void { - if (!file_exists(static::PATH_ENV)) { - return; - } - - $lines = file(static::PATH_ENV, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - if ($lines === false) { - throw new Exception('Unable to read environment file "' . static::PATH_ENV . '"'); - } - - foreach ($lines as $line) { - if (preg_match('/^\s*(?:(?#).+|(?[a-zA-Z0-9_-]+)=(?.+))$/i', $line, $match) !== 1) { - continue; // Ligne invalide - } - - if (isset($match['comment']) && trim($match['comment']) !== '') { - continue; // Commentaire - } - - $this->properties[mb_strtoupper($match['key'])] = $match['value']; - } - } } \ No newline at end of file diff --git a/tests/.env b/tests/.env index 4f45cb5..53f4747 100644 --- a/tests/.env +++ b/tests/.env @@ -1,4 +1,4 @@ -WEBSITE_NAME=Foo +WEBSITE_NAME="Foo Bar" DB_NAME=Bar DB_LOGIN=Bar