diff options
Diffstat (limited to 'src/Config.php')
-rw-r--r-- | src/Config.php | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..cfec876 --- /dev/null +++ b/src/Config.php | |||
@@ -0,0 +1,76 @@ | |||
1 | <?php | ||
2 | // src/controller/Config.php | ||
3 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | class Config | ||
7 | { | ||
8 | // BDD | ||
9 | static public string $db_host = 'localhost'; | ||
10 | static public string $database = 'nageurs'; | ||
11 | static public string $db_driver = 'pdo_mysql'; | ||
12 | static public string $user = 'root'; | ||
13 | static public string $password = ''; | ||
14 | static public string $table_prefix = ''; | ||
15 | |||
16 | // classe URL | ||
17 | static public string $protocol = 'http'; | ||
18 | static public string $host = 'nageurs.localhost'; | ||
19 | static public string $port = '80'; | ||
20 | |||
21 | // copier dans ce tableau les variables contenant des chemins | ||
22 | static private array $path_vars = []; | ||
23 | |||
24 | static public function load(string $file_path): void | ||
25 | { | ||
26 | if(file_exists($file_path)) | ||
27 | { | ||
28 | // ce serait bien de gérer aussi les fichiers corrompus? | ||
29 | $raw_data = parse_ini_file($file_path); | ||
30 | self::hydrate($raw_data); | ||
31 | } | ||
32 | else | ||
33 | { | ||
34 | echo "le fichier config.ini n'existe pas ou n'est pas lisible"; | ||
35 | } | ||
36 | define('TABLE_PREFIX', self::$table_prefix); | ||
37 | } | ||
38 | |||
39 | // renseigner les variables internes de Config | ||
40 | static private function hydrate(array $raw_data): void | ||
41 | { | ||
42 | foreach($raw_data as $field => $value) | ||
43 | { | ||
44 | if($value != '') // valeur par défaut | ||
45 | { | ||
46 | if(isset(self::$$field)) // le champ existe dans Config | ||
47 | { | ||
48 | // problème du slash à la fin du nom d'un dossier | ||
49 | $value = self::slashAtEndOfPath($field, $value); | ||
50 | self::$$field = $value; | ||
51 | } | ||
52 | else | ||
53 | { | ||
54 | echo "debug: le fichier config.ini comporte une erreur, le champ: " . $field . " est incorrect,\nl'information contenue sur cette ligne ne sera pas utilisée\n"; | ||
55 | } | ||
56 | } | ||
57 | /*else | ||
58 | { | ||
59 | echo "debug: le champ " . $field . " est vide, la valeur par défaut " . self::$$field . " sera utilisée.\n"; | ||
60 | }*/ | ||
61 | } | ||
62 | } | ||
63 | |||
64 | |||
65 | // pour que les chemins finissent toujours par un / | ||
66 | static private function slashAtEndOfPath(string $field, string $value): string | ||
67 | { | ||
68 | foreach(self::$path_vars as $item) | ||
69 | { | ||
70 | if($field === $item){ | ||
71 | return !str_ends_with($value, '/') ? $value . '/' : $value; | ||
72 | } | ||
73 | } | ||
74 | return $value; | ||
75 | } | ||
76 | } | ||