summaryrefslogtreecommitdiff
path: root/src/Config.php
blob: b4f8e5b7b956d94886e99d93bf367578d4bc7ac9 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
// src/Config.php

class Config
{
    // valeurs par défaut au cas où
    static public $business_name = 'ORDIPOLO';
    static public $business_guy = 'Paul Jusot';
    static public $business_address = "2A rue de l'île de Man";
    static public $business_postcode = '29000';
    static public $business_city = 'Quimper';
    static public $db_name = "ordipolo";
    static public $db_path = '../data/';
    static public $latex_path = '../data/latex/';
    static public $pdf_path = '../data/pdf/';
    static public $pub_path = "../pub/";
    static public $flyer = "flyer.xcf";
    static public $business_card = "carte.sla";
    static public $image_editor = 'gimp';
    static public $publishing = 'scribus';
    static public $sgbd = 'sqlite';
    static public $sqlite_gui = 'sqlitebrowser'; // ne pas utiliser si le sgbd n'est pas sqlite
    static public $date_format = 'euro';
    
    // pour que Config crée le dsn: Data Source Name (à mettre dans une autre classe!)
    //~ static public $dsn = '';
    
    // ça pourrait être bien de founir sqlite avec l'application pour supprimer une dépendance
    //~ static public $sqliteBin = '../lib/sqlite_linux';
    //~ static public $sqliteBin = '../lib/sqlite_win.exe';
    
    static private $raw_data;
    
    static public function readFile($file_path)
    {
        self::$raw_data = parse_ini_file($file_path);
    }
    
    static public function hydrate()
    {
        foreach(self::$raw_data as $field => $value)
        {
            if($value != '') // valeur par défaut
            {
                if(isset(self::$$field)) // vérification du nom du champ
                {
                    // vérification du contenu
                    if(self::fieldIsPath($field)) // cas où le champ db_path, latex_path, pdf_path ou pub_path
                    {
                        if(self::checkPath($field, $value))
                        {
                            self::$$field = self::slashAtEndOfPath($value);
                            //self::$$field = $value;
                        }
                    }
                    else // tester le reste?
                    {
                        self::$$field = $value;
                    }
                    // else: la valeur par défaut est conservée
                }
                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";
            }
        }
    }
    
    static private function fieldIsPath($field): bool
    {
        if($field === 'db_path' || $field === 'latex_path' || $field === 'pdf_path' || $field === 'pub_path')
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    static private function checkPath($field, $value): bool
    {
        if(!file_exists($value))
        {
            echo "debug: le fichier config.ini comporte une erreur, le dossier " . $value . " renseigné pour le champ " . $field . " n'existe pas,\nla valeur par défaut: " . self::$$field . " sera utilisé\n";
            return false;
        }
        if(!is_writable($value))
        {
            if($value == self::$$field)
            {
                echo "debug: le dossier " . $value . " n'est pas autorisé en écriture\n";
            }
            else
            {
                echo "debug: le dossier " . $value . " renseigné pour le champ " . $field . " dans le fichier config.ini n'est pas autorisé en écriture,\nla valeur par défaut: " . self::$$field . " sera utilisée\n";
            }
            return false;
        }
        return true;
    }
    
    static public function slashAtEndOfPath($path): string
    {
        if(!str_ends_with($path, '/'))
        {
            return($path . '/');
        }
        else
        {
            return $path;
        }
    }
    
    // à faire plus tard
    // transformer les chemins pour qu'ils soient relatifs au dossier 'src' où s'exécute le programme
    static private function adjustRelativePath($input)
    {
        // chemin windows?
        
        // chemin relatif ou absolu (par le 1er caractère)
        
        // obtenir l'emplacement du dossier de l'utilisateur '~'
        $userHomePlace = exec('');
        
        // obtenir l'emplacement du dossier 'src'
        $appExecPlace = getcwd();
    }
    
    //~ static public function makeDsn()
    //~ {
        //~ if($this->sgbd === 'sqlite')
        //~ {
            //~ $this->dsn = 'sqlite:' . $this->db_name . '.sqlite';
        //~ }
    //~ }
}