| Total Complexity | 51 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Config |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Fichier de configuration déjà chargé |
||
| 25 | */ |
||
| 26 | private static array $loaded = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Drapeau permettant de savoir si la config a deja ete initialiser |
||
| 30 | */ |
||
| 31 | private static bool $initialized = false; |
||
| 32 | |||
| 33 | private Configurator $configurator; |
||
| 34 | |||
| 35 | public function __construct() |
||
| 36 | { |
||
| 37 | $this->configurator = new Configurator(); |
||
| 38 | $this->initialize(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Détermine si une clé de configuration existe. |
||
| 43 | */ |
||
| 44 | public function exists(string $key): bool |
||
| 45 | { |
||
| 46 | if (! $this->configurator->exists($key)) { |
||
| 47 | $config = explode('.', $key); |
||
| 48 | $this->load($config[0]); |
||
| 49 | |||
| 50 | return $this->configurator->exists(implode('.', $config)); |
||
| 51 | } |
||
| 52 | |||
| 53 | return true; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Détermine s'il y'a une clé de configuration. |
||
| 58 | */ |
||
| 59 | public function has(string $key): bool |
||
| 60 | { |
||
| 61 | return $this->exists($key); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Détermine s'il manque une clé de configuration. |
||
| 66 | */ |
||
| 67 | public function missing(string $key): bool |
||
| 68 | { |
||
| 69 | return ! $this->exists($key); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Renvoyer une configuration de l'application |
||
| 74 | * |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | public function get(string $key, mixed $default = null) |
||
| 78 | { |
||
| 79 | if ($this->exists($key)) { |
||
| 80 | return $this->configurator->get($key); |
||
| 81 | } |
||
| 82 | |||
| 83 | if (func_num_args() > 1) { |
||
| 84 | return $default; |
||
| 85 | } |
||
| 86 | |||
| 87 | $path = explode('.', $key); |
||
| 88 | |||
| 89 | throw ConfigException::notFound(implode(' » ', $path)); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Définir une configuration de l'application |
||
| 94 | */ |
||
| 95 | public function set(string $key, $value) |
||
| 96 | { |
||
| 97 | $this->configurator->set($key, $value); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Charger la configuration spécifique dans le scoope |
||
| 102 | * |
||
| 103 | * @param string|string[] $config |
||
| 104 | */ |
||
| 105 | public function load($config, ?string $file = null, ?Schema $schema = null) |
||
| 106 | { |
||
| 107 | if (is_array($config)) { |
||
| 108 | foreach ($config as $key => $value) { |
||
| 109 | if (! is_string($value) || empty($value)) { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | if (is_string($key)) { |
||
| 113 | $file = $value; |
||
| 114 | $conf = $key; |
||
| 115 | } else { |
||
| 116 | $file = null; |
||
| 117 | $conf = $value; |
||
| 118 | } |
||
| 119 | self::load($conf, $file); |
||
|
|
|||
| 120 | } |
||
| 121 | } elseif (is_string($config) && ! isset(self::$loaded[$config])) { |
||
| 122 | if (empty($file)) { |
||
| 123 | $file = self::path($config); |
||
| 124 | } |
||
| 125 | |||
| 126 | $configurations = []; |
||
| 127 | if (file_exists($file) && ! in_array($file, get_included_files(), true)) { |
||
| 128 | $configurations = (array) require $file; |
||
| 129 | } |
||
| 130 | |||
| 131 | if (empty($schema)) { |
||
| 132 | $schema = self::schema($config); |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->configurator->addSchema($config, $schema, false); |
||
| 136 | $this->configurator->merge([$config => (array) $configurations]); |
||
| 137 | |||
| 138 | self::$loaded[$config] = $file; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Affiche l'exception dû à la mauvaise definition d'une configuration |
||
| 144 | * |
||
| 145 | * @param array|string $accepts_values |
||
| 146 | * @param string $group (app, data, database, etc.) |
||
| 147 | */ |
||
| 148 | public static function exceptBadConfigValue(string $config_key, $accepts_values, string $group) |
||
| 149 | { |
||
| 150 | if (is_array($accepts_values)) { |
||
| 151 | $accepts_values = '(Accept values: ' . implode('/', $accepts_values) . ')'; |
||
| 152 | } elseif (! is_string($accepts_values)) { |
||
| 153 | throw new InvalidArgumentException('Misuse of the method ' . __METHOD__); |
||
| 154 | } |
||
| 155 | |||
| 156 | throw new ConfigException("The '{$group}.{$config_key} configuration is not set correctly. {$accepts_values} \n Please edit '{" . self::path($group) . "}' file to correct it"); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Renvoie le chemin du fichier d'un groupe de configuration donné |
||
| 161 | */ |
||
| 162 | public static function path(string $path): string |
||
| 163 | { |
||
| 164 | $path = preg_replace('#\.php$#', '', $path); |
||
| 165 | |||
| 166 | if (file_exists($file = CONFIG_PATH . $path . '.php')) { |
||
| 167 | return $file; |
||
| 168 | } |
||
| 169 | |||
| 170 | $paths = Services::locator()->search('Config/' . $path); |
||
| 171 | |||
| 172 | if (isset($paths[0]) && file_exists($path[0])) { |
||
| 173 | return $paths[0]; |
||
| 174 | } |
||
| 175 | |||
| 176 | return ''; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrouve le schema de configuration d'un groupe |
||
| 181 | */ |
||
| 182 | public static function schema(string $key): Schema |
||
| 183 | { |
||
| 184 | $file = 'schemas' . DS . Helpers::ensureExt($key . '.config', 'php'); |
||
| 185 | $syst_schema = SYST_PATH . 'Constants' . DS . $file; |
||
| 186 | $app_schema = CONFIG_PATH . $file; |
||
| 187 | |||
| 188 | if (file_exists($syst_schema)) { |
||
| 189 | $schema = require $syst_schema; |
||
| 190 | } elseif (file_exists($app_schema)) { |
||
| 191 | $schema = require $app_schema; |
||
| 192 | } |
||
| 193 | |||
| 194 | if (empty($schema) || ! ($schema instanceof Schema)) { |
||
| 195 | $schema = Expect::mixed(); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $schema; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Initialiser la configuration du système avec les données des fichier de configuration |
||
| 203 | */ |
||
| 204 | private function initialize() |
||
| 205 | { |
||
| 206 | if (self::$initialized) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->load(['app']); |
||
| 211 | |||
| 212 | ini_set('log_errors', 1); |
||
| 213 | ini_set('error_log', LOG_PATH . 'blitz-logs'); |
||
| 214 | |||
| 215 | $this->initializeURL(); |
||
| 216 | $this->initializeEnvironment(); |
||
| 217 | $this->initializeDebugbar(); |
||
| 218 | |||
| 219 | self::$initialized = true; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Initialise l'URL |
||
| 224 | */ |
||
| 225 | private function initializeURL() |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Initialise l'environnement d'execution de l'application |
||
| 238 | */ |
||
| 239 | private function initializeEnvironment() |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Initialise les paramètres de la bar de debug |
||
| 276 | */ |
||
| 277 | private function initializeDebugbar() |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 |