Start project PHP Back.
50% routes.
This commit is contained in:
42
src/Helpers/ConfigFactory.php
Normal file
42
src/Helpers/ConfigFactory.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user