Skip to content

Commit

Permalink
afup#1442 optimisation du chargement de la configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
stakovicz committed Mar 14, 2024
1 parent 51216ec commit 0adcc11
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions sources/Afup/Utils/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}

0 comments on commit 0adcc11

Please sign in to comment.