summaryrefslogtreecommitdiff
path: root/src/Config.php
blob: e25c180ccc554c3c28ca98459f9eea240b2f53a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
// src/Config.php

declare(strict_types=1);

class Config
{
    // BDD
    static public string $db_host = 'localhost';
    static public string $database = '';
    static public string $db_driver = 'pdo_mysql';
    static public string $user = '';
    static public string $password = '';
    static public string $table_prefix = '';

    // classe URL
    static public string $protocol = 'http';
    static public string $index_path = '';
    static public string $port = '80';

    // e-mails
    static public string $smtp_host = '';
    static public string $smtp_username = '';
    static public string $smtp_password = '';
    static public string $smtp_secure = 'tls'; // tls (smarttls) ou ssl (smtps)
    static public string $email_from = '';
    static public string $email_from_name = 'site web';
    static public string $email_dest = '';
    static public string $email_dest_name = 'destinataire formulaire';

    // copier dans ce tableau les variables contenant des chemins
    static private array $path_vars = [];
    
    static public function load(string $file_path): void
    {
        if(file_exists($file_path))
        {
            // ce serait bien de gérer aussi les fichiers corrompus?
            $raw_data = parse_ini_file($file_path);
            self::hydrate($raw_data);
        }
        else
        {
            echo "le fichier config.ini n'existe pas ou n'est pas lisible";
        }
        define('TABLE_PREFIX', self::$table_prefix);
    }
    
    // renseigner les variables internes de Config
    static private function hydrate(array $raw_data): void
    {
        foreach($raw_data as $field => $value)
        {
            if($value != '') // valeur par défaut
            {
                if(isset(self::$$field)) // le champ existe dans Config
                {
                    // problème du slash à la fin du nom d'un dossier
                    $value = self::slashAtEndOfPath($field, $value);
                    self::$$field = $value;
                }
                else
                {
                    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";
                }
            }
            /*else
            {
                echo "debug: le champ " . $field . " est vide, la valeur par défaut " . self::$$field . " sera utilisée.\n";
            }*/
        }
    }
    

    // pour que les chemins finissent toujours par un /
    static private function slashAtEndOfPath(string $field, string $value): string
    {
        foreach(self::$path_vars as $item)
        {
            if($field === $item){
                return !str_ends_with($value, '/') ? $value . '/' : $value;
            }
        }
        return $value;
    }
}