diff options
author | polo <ordipolo@gmx.fr> | 2023-01-16 03:33:06 +0100 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2023-01-16 03:33:06 +0100 |
commit | 209c0b93c529356a094d7133a717e8f6ee6d90c6 (patch) | |
tree | b8dac8968501fe4e0bba8dc98b80d0eb43770ecb /src/Config.php | |
parent | 945af9fda5146405ab9903d4d268bcb2fe95da25 (diff) | |
download | AppliGestionPHP-209c0b93c529356a094d7133a717e8f6ee6d90c6.zip |
Config & config.ini, data folder, partie latex bientôt finie
Diffstat (limited to 'src/Config.php')
-rw-r--r-- | src/Config.php | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..396b588 --- /dev/null +++ b/src/Config.php | |||
@@ -0,0 +1,131 @@ | |||
1 | <?php | ||
2 | // src/Config.php | ||
3 | |||
4 | class Config | ||
5 | { | ||
6 | // valeurs par défaut au cas où | ||
7 | static public $business_name = 'ORDIPOLO'; | ||
8 | static public $db_name = "ordipolo"; | ||
9 | static public $db_path = '../data/'; | ||
10 | static public $latex_path = '../data/latex/'; | ||
11 | static public $pdf_path = '../data/pdf/'; | ||
12 | static public $pub_path = "../pub/"; | ||
13 | static public $flyer = "flyer.xcf"; | ||
14 | static public $business_card = "carte.sla"; | ||
15 | static public $image_editor = 'gimp'; | ||
16 | static public $publishing = 'scribus'; | ||
17 | static public $sgbd = 'sqlite'; | ||
18 | static public $sqlite_gui = 'sqlitebrowser'; // ne pas utiliser si le sgbd n'est pas sqlite | ||
19 | static public $date_format = 'euro'; | ||
20 | |||
21 | // pour que Config crée le dsn: Data Source Name (à mettre dans une autre classe!) | ||
22 | //~ static public $dsn = ''; | ||
23 | |||
24 | // ça pourrait être bien de founir sqlite avec l'application pour supprimer une dépendance | ||
25 | //~ static public $sqliteBin = '../lib/sqlite_linux'; | ||
26 | //~ static public $sqliteBin = '../lib/sqlite_win.exe'; | ||
27 | |||
28 | static private $raw_data; | ||
29 | |||
30 | static public function readFile($file_path) | ||
31 | { | ||
32 | self::$raw_data = parse_ini_file($file_path); | ||
33 | } | ||
34 | |||
35 | static private function fieldIsPath($field): bool | ||
36 | { | ||
37 | if($field === 'db_path' || $field === 'latex_path' || $field === 'pdf_path' || $field === 'pub_path') | ||
38 | { | ||
39 | return true; | ||
40 | } | ||
41 | else | ||
42 | { | ||
43 | return false; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | static private function checkPath($field, $value): bool | ||
48 | { | ||
49 | if(!file_exists($value)) | ||
50 | { | ||
51 | 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"; | ||
52 | return false; | ||
53 | } | ||
54 | if(!is_writable($value)) | ||
55 | { | ||
56 | if($value == self::$$field) | ||
57 | { | ||
58 | echo "debug: le dossier " . $value . " n'est pas autorisé en écriture\n"; | ||
59 | } | ||
60 | else | ||
61 | { | ||
62 | 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"; | ||
63 | } | ||
64 | return false; | ||
65 | } | ||
66 | return true; | ||
67 | } | ||
68 | |||
69 | static public function slashAtEndOfPath($path): string | ||
70 | { | ||
71 | if(!str_ends_with($path, '/')) | ||
72 | { | ||
73 | return($path . '/'); | ||
74 | } | ||
75 | else | ||
76 | { | ||
77 | return $path; | ||
78 | } | ||
79 | } | ||
80 | |||
81 | static public function hydrate() | ||
82 | { | ||
83 | foreach(self::$raw_data as $field => $value) | ||
84 | { | ||
85 | if(isset(self::$$field)) // vérification du nom du champ | ||
86 | { | ||
87 | // vérification du contenu | ||
88 | if(self::fieldIsPath($field)) // cas où le champ db_path, latex_path, pdf_path ou pub_path | ||
89 | { | ||
90 | if(self::checkPath($field, $value)) | ||
91 | { | ||
92 | $value = self::slashAtEndOfPath($value); | ||
93 | self::$$field = $value; | ||
94 | } | ||
95 | } | ||
96 | else // tester le reste? | ||
97 | { | ||
98 | self::$$field = $value; | ||
99 | } | ||
100 | // else: la valeur par défaut est conservée | ||
101 | } | ||
102 | else | ||
103 | { | ||
104 | 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"; | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | |||
109 | // à faire plus tard | ||
110 | // transformer les chemins pour qu'ils soient relatif au dossier 'src' où s'exécute le programme | ||
111 | static private function adjustRelativePath($input) | ||
112 | { | ||
113 | // chemin windows? | ||
114 | |||
115 | // chemin relatif ou absolu (par le 1er caractère) | ||
116 | |||
117 | // obtenir l'emplacement du dossier de l'utilisateur '~' | ||
118 | $userHomePlace = exec(''); | ||
119 | |||
120 | // obtenir l'emplacement du dossier 'src' | ||
121 | $appExecPlace = getcwd(); | ||
122 | } | ||
123 | |||
124 | //~ static public function makeDsn() | ||
125 | //~ { | ||
126 | //~ if($this->sgbd === 'sqlite') | ||
127 | //~ { | ||
128 | //~ $this->dsn = 'sqlite:' . $this->db_name . '.sqlite'; | ||
129 | //~ } | ||
130 | //~ } | ||
131 | } | ||