You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace jrosset\EnvReader;
|
|
|
|
use Exception;
|
|
|
|
abstract class EnvConfig extends GenericConfig implements IExternalConfigFile {
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function readConfig (): void {
|
|
$configFile = $this->getConfigFilePath();
|
|
if (!file_exists($configFile)) {
|
|
throw new Exception('Unable to find configuration file "' . $configFile . '"');
|
|
}
|
|
|
|
$lines = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
if ($lines === false) {
|
|
throw new Exception('Unable to read configuration file "' . $configFile . '"');
|
|
}
|
|
|
|
foreach ($lines as $line) {
|
|
if (preg_match('/^\s*(?:(?<comment>#).*|(?<key>[a-zA-Z\\d_-]+)=(?<value>.+?))$/i', $line, $match) !== 1) {
|
|
continue; // Ligne invalide
|
|
}
|
|
if (isset($match['comment']) && trim($match['comment']) !== '') {
|
|
continue; // Commentaire
|
|
}
|
|
|
|
if (preg_match('/^"(?<payload>.+)(?<!(?<!\\\\)\\\\)"$/', $match['value'], $matchValue) === 1) {
|
|
$match['value'] = preg_replace(
|
|
[
|
|
'/(?<!\\\\)\\\\"/',
|
|
'/\\\\\\\\/',
|
|
],
|
|
[
|
|
'"',
|
|
'\\',
|
|
],
|
|
$matchValue['payload']
|
|
);
|
|
}
|
|
$this->properties->set($match['key'], $match['value']);
|
|
}
|
|
}
|
|
} |