From 65ce5e23823a509528a7cb7b2491b7861ab60223 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Wed, 19 Oct 2022 09:45:02 +0200 Subject: [PATCH] EnvConfig: use TExternalFile instead of a constant for config file path --- README.md | 17 +---------------- src/EnvReader/EnvConfig.php | 14 ++++++-------- tests/test_env.php | 8 +++----- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5e098d8..05a2dcf 100644 --- a/README.md +++ b/README.md @@ -10,19 +10,4 @@ composer require jrosset/envreader ## Usage -```php - 'DEV', - 'WEBSITE_NAME' => 'Generic site' - ]; - } -} -``` +Check "tests" directory for various examples diff --git a/src/EnvReader/EnvConfig.php b/src/EnvReader/EnvConfig.php index 8f36f3b..81637bb 100644 --- a/src/EnvReader/EnvConfig.php +++ b/src/EnvReader/EnvConfig.php @@ -4,23 +4,21 @@ namespace jrosset\EnvReader; use Exception; -class EnvConfig extends GenericConfig { - /** - * ENV file path - */ - protected const PATH_ENV = '.env'; +abstract class EnvConfig extends GenericConfig { + use TExternalFile; /** * @inheritDoc */ protected function readConfig (): void { - if (!file_exists(static::PATH_ENV)) { + $configFile = $this->getConfigFilePath(); + if (!file_exists($configFile)) { return; } - $lines = file(static::PATH_ENV, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + $lines = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if ($lines === false) { - throw new Exception('Unable to read environment file "' . static::PATH_ENV . '"'); + throw new Exception('Unable to read environment file "' . $configFile . '"'); } foreach ($lines as $line) { diff --git a/tests/test_env.php b/tests/test_env.php index 29043c8..b148c5e 100644 --- a/tests/test_env.php +++ b/tests/test_env.php @@ -6,17 +6,15 @@ require_once __DIR__ . '/../vendor/autoload.php'; use jrosset\EnvReader\EnvConfig; class Env extends EnvConfig { - /** - * @inheritdoc - */ - protected const PATH_ENV = __DIR__ . '/.env'; - protected function initialProperties (): array { return [ 'WEBSITE_MODE' => 'DEV', 'WEBSITE_NAME' => 'Generic site', ]; } + protected function getConfigFilePath (): string { + return __DIR__ . '/.env'; + } } var_dump(Env::getInstance()->getProperties());