42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Helpers;
|
||
|
|
|
||
|
|
use App\Exceptions\ConfigFailedLoadingException;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Classe outillage qui permet de charger les fichiers de configuration.
|
||
|
|
*/
|
||
|
|
class ConfigFactory {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Chemin relatif à partir de APP_ROOT du dossier contenant les fichiers de configuration.
|
||
|
|
*
|
||
|
|
* @see APP_ROOT
|
||
|
|
*/
|
||
|
|
const string CONFIG_RELATIVE_FOLDER = "config/";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Permet de charger un fichier de configuration.
|
||
|
|
*
|
||
|
|
* @param string $fileName Le nom du fichier de configuration, avec ou sans l'extension .php.
|
||
|
|
* @return array
|
||
|
|
*
|
||
|
|
* @throws ConfigFailedLoadingException
|
||
|
|
*/
|
||
|
|
public static function loadConfigFile( string $fileName ): array {
|
||
|
|
if( !str_ends_with( $fileName, ".php" ) ) {
|
||
|
|
$fileName .= ".php";
|
||
|
|
}
|
||
|
|
|
||
|
|
$filePath = APP_ROOT . self::CONFIG_RELATIVE_FOLDER . $fileName;
|
||
|
|
|
||
|
|
if( !file_exists( $filePath ) ){
|
||
|
|
throw new ConfigFailedLoadingException( "Config file '$fileName' does not exist" );
|
||
|
|
}
|
||
|
|
|
||
|
|
$configArray = require $filePath;
|
||
|
|
return $configArray;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|