From 0adcc114ccca826d1c7017b594937576444ca51c Mon Sep 17 00:00:00 2001 From: Albin Date: Sat, 9 Mar 2024 13:13:08 +0100 Subject: [PATCH] afup#1442 optimisation du chargement de la configuration --- sources/Afup/Utils/Configuration.php | 73 ++++++++++++---------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/sources/Afup/Utils/Configuration.php b/sources/Afup/Utils/Configuration.php index 0b36fe190..910b38bf0 100644 --- a/sources/Afup/Utils/Configuration.php +++ b/sources/Afup/Utils/Configuration.php @@ -11,67 +11,56 @@ */ class Configuration { - /** - * Valeurs de configuration - */ - private $_valeurs; + static private $values; - /** - * Charge les valeurs depuis le fichier de configuration - */ public function __construct() { + if (self::$values) { + return; + } + $sfParameters = $this->loadSymfonyParameters(); - if ([] !== $sfParameters) { - $this->_valeurs['database_host'] = $sfParameters['database_host'] ?? ''; - $this->_valeurs['database_name'] = $sfParameters['database_name'] ?? ''; - $this->_valeurs['database_user'] = $sfParameters['database_user'] ?? ''; - $this->_valeurs['database_password'] = $sfParameters['database_password'] ?? ''; + $parameters = [ + 'database_host', 'database_name', 'database_user', 'database_password', + 'smtp_host', 'smtp_port', 'smtp_tls', 'smtp_username', 'smtp_password', + 'mailer_force_recipients', 'mailer_bcc' + ]; - $this->_valeurs['smtp_host'] = $sfParameters['smtp_host'] ?? ''; - $this->_valeurs['smtp_port'] = $sfParameters['smtp_port'] ?? ''; - $this->_valeurs['smtp_tls'] = $sfParameters['smtp_tls'] ?? ''; - $this->_valeurs['smtp_username'] = $sfParameters['smtp_username'] ?? ''; - $this->_valeurs['smtp_password'] = $sfParameters['smtp_password'] ?? ''; + foreach($parameters as $param) { + + // env var exist ? + if (false !== $value = getenv(strtoupper($param))) { + self::$values[$param] = $value; + } - $this->_valeurs['mailer_force_recipients'] = $sfParameters['mailer_force_recipients'] ?? ''; - $this->_valeurs['mailer_bcc'] = $sfParameters['mailer_bcc'] ?? ''; + // override by parameter_ENV.yaml ? + if (isset($sfParameters[$param])) { + self::$values[$param] = $sfParameters[$param]; + } } + } private function loadSymfonyParameters(): array { - static $parameters = []; - - if ([] === $parameters) { - $basePath = __DIR__ . '/../../../app/config'; + $parameters = []; + $basePath = __DIR__ . '/../../../app/config'; - $this->mergeSymfonyParametersFromFile($basePath . '/parameters.yml', $parameters); - $this->mergeSymfonyParametersFromFile($basePath . '/config.yml', $parameters); - if (isset($_ENV['SYMFONY_ENV'])) { - $this->mergeSymfonyParametersFromFile($basePath . '/config_' . $_ENV['SYMFONY_ENV'] . '.yml', $parameters); + if (isset($_ENV['SYMFONY_ENV'])) { + $file = $basePath . '/config_' . $_ENV['SYMFONY_ENV'] . '.yml'; + if (is_file($file)) { + $values = Yaml::parseFile($file); + if (isset($values['parameters'])) { + $parameters = array_merge($parameters, $values['parameters']); + } } } return $parameters; } - private function mergeSymfonyParametersFromFile($file, &$parameters) - { - if (is_file($file)) { - $values = Yaml::parseFile($file); - if (isset($values['parameters'])) { - $parameters = array_merge($parameters, $values['parameters']); - } - } - } - - /** - * Renvoie la valeur correspondant à la clé - */ public function obtenir($cle) { - return $this->_valeurs[$cle]; + return self::$values[$cle]; } - }