-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIni.php
494 lines (383 loc) · 11.8 KB
/
Ini.php
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// ini
// class with methods a layer over the native PHP ini functions
final class Ini extends Root
{
// config
protected static array $config = [
'default'=>[ // ini à appliquer par défaut lors du chargement
'default_charset'=>'UTF-8',
'error_log'=>'error_log',
'log_errors'=>true,
'html_errors'=>true,
'display_errors'=>true,
'error_reporting'=>-1,
'date.timezone'=>'America/New_York'],
'important'=>[ // ini considéré importante
'error_log',
'mail.log',
'browscap',
'date.timezone',
'default_charset',
'display_startup_errors',
'display_errors',
'html_errors',
'error_reporting',
'memory_limit',
'upload_max_filesize',
'max_file_uploads',
'post_max_size',
'max_input_time',
'session.gc_maxlifetime',
'precision',
'mysql.default_socket',
'always_populate_raw_post_data',
'register_argc_argv',
'request_order',
'variables_order',
'allow_url_fopen',
'allow_url_include',
'assert.exception',
'zend.assertions',
'zend.exception_ignore_args',
'opcache.enable',
'opcache.revalidate_freq',
'apc.enabled',
'xdebug.remote_enable',
'xdebug.coverage_enable',
'xdebug.profiler_enable',
'xdebug.overload_var_dump']
];
// is
// retourne vrai si la valeur ini existe
final public static function is($value):bool
{
return is_string($value) && array_key_exists($value,self::all());
}
// isVarDumpOverloaded
// retourne vrai si l'extension xdebug est active et var dump est overloadé
final public static function isVarDumpOverloaded():bool
{
return Extension::hasXdebug() && self::xdebug();
}
// get
// retourne une valeur ini
// si format est int, les ini string vide retournent false
// le retour est passé dans scalar cast avec maximum de conversion de type
final public static function get(string $key,?int $format=null)
{
$return = null;
$get = ini_get($key);
if(is_string($get))
{
$return = Scalar::cast($get,2,2);
if(is_string($return) && is_int($format))
{
if($return === '')
$return = false;
if(!empty((int) $return))
$return = self::sizeFormat($return,$format);
}
}
return $return;
}
// gets
// retourne plusieurs valeurs ini
final public static function gets(string ...$keys):array
{
$return = [];
foreach ($keys as $key)
{
$return[$key] = self::get($key);
}
return $return;
}
// set
// change une valeur ini
// la valeur est cast en string avant d'être mis dans ini_set
final public static function set(string $key,$value):bool
{
$return = false;
if(is_scalar($value))
{
$value = (string) $value;
$return = (!empty($key) && ini_set($key,$value) !== false);
}
return $return;
}
// sets
// change pluieurs valeurs ini
final public static function sets(array $array):array
{
$return = [];
foreach ($array as $key => $value)
{
if(is_string($key))
$return[$key] = self::set($key,$value);
}
return $return;
}
// unset
// retourne une ou plusieurs valeurs ini à leurs valeurs originales
final public static function unset(string ...$keys):void
{
foreach ($keys as $key)
{
ini_restore($key);
}
}
// all
// retourne toutes les configurations ini
final public static function all(?string $extension=null,bool $details=false):array
{
$return = [];
if(is_string($extension))
{
if(Extension::is($extension))
$return = ini_get_all($extension,$details);
}
else
$return = ini_get_all(null,$details);
return $return;
}
// parse
// parse un fichier ou une string ini et retourne un tableau
final public static function parse(string $ini,bool $processSections=false,int $scannerMode=INI_SCANNER_NORMAL):?array
{
$return = null;
if(!empty($ini))
{
if(is_file($ini))
$return = parse_ini_file($ini,$processSections,$scannerMode);
else
$return = parse_ini_string($ini,$processSections,$scannerMode);
}
return $return;
}
// files
// retourne tous les fichiers ini loaded ou scanned
final public static function files():array
{
$return = [];
$return['loaded'] = php_ini_loaded_file();
$scanned = php_ini_scanned_files();
if(!empty($scanned))
{
$explode = explode(',',$scanned);
$return['scanned'] = Arr::map($explode,fn($value) => trim($value));
}
return $return;
}
// sizeFormat
// format une string de taille dans ini de style 100M et retourne un int en byte
// si format est true, la valeur est renvoyé dans sizeFormat
final public static function sizeFormat(string $return,int $format=0)
{
if($format >= 1)
{
$return = Num::fromSizeFormatMb($return);
if(is_int($return) && $format === 2)
$return = Num::sizeFormat($return,$format);
}
else
$return = Str::toInt($return);
return $return;
}
// uploadMaxFilesize
// retourne la valeur de uploadMaxFilesize
final public static function uploadMaxFilesize(?int $format=0)
{
return self::get('upload_max_filesize',$format);
}
// postMaxSize
// retourne la valeur de postMaxSize
final public static function postMaxSize(?int $format=0)
{
return self::get('post_max_size',$format);
}
// memoryLimit
// retourne la valeur de memoryLimit
final public static function memoryLimit(?int $format=0)
{
return self::get('memory_limit',$format);
}
// tempDir
// retourne le chemin du dossier temporaire tel que spécifié dans ini
// note ce chemin est souvent vide -> utilise dir::temp
final public static function tempDir()
{
return self::get('sys_temp_dir');
}
// getCharset
// retourne la valeur de default charset
final public static function getCharset():string
{
return self::get('default_charset');
}
// setCharset
// change la valeur de default charset
final public static function setCharset(string $value):bool
{
return self::set('default_charset',$value);
}
// getTimezone
// retourne la valeur de timezone
final public static function getTimezone():string
{
return self::get('date.timezone');
}
// setTimezone
// change la timezone
final public static function setTimezone(string $value):bool
{
return self::set('date.timezone',$value);
}
// getTimeLimit
// applique une limite de temps au process
final public static function getTimeLimit():int
{
return self::get('max_execution_time');
}
// setTimeLimit
// applique une limite de temps au process
final public static function setTimeLimit($value):bool
{
return self::set('max_execution_time',$value);
}
// getErrorReporting
// retourne la valeur actuel de error reporting
final public static function getErrorReporting()
{
return self::get('error_reporting');
}
// setErrorReporting
// change la valeur de error reporting
final public static function setErrorReporting($value):bool
{
return self::set('error_reporting',$value);
}
// getErrorLog
// retourne la valeur du log d'erreur
final public static function getErrorLog()
{
return self::get('error_log');
}
// setErrorLog
// change la valeur de error log
// la valeur est passé dans finder shortcut
final public static function setErrorLog(string $value):bool
{
return self::set('error_log',Finder::normalize($value));
}
// getIncludePathSeparator
// retourne le séparateur pour include path selon le os du serveur
final public static function getIncludePathSeparator():string
{
return (Server::isWindows())? ';':':';
}
// getIncludePath
// retourne la ou les valeurs actuels de include path
// retourne un tableau
final public static function getIncludePath():array
{
$return = [];
$paths = self::get('include_path');
if(is_string($paths) && !empty($paths))
$return = explode(self::getIncludePathSeparator(),$paths);
return $return;
}
// setIncludePath
// change la valeur de include path
// input peut être tableau ou string
final public static function setIncludePath($path):bool
{
$return = false;
if(is_array($path) && !empty($path))
$path = implode(self::getIncludePathSeparator(),$path);
if(is_string($path))
$return = self::set('include_path',$path);
return $return;
}
// addIncludePath
// ajoute un include path à ceux déjà paramétré
final public static function addIncludePath(string $path):bool
{
$return = false;
if(!empty($path))
{
$paths = self::getIncludePath();
$paths[] = $path;
$return = self::setIncludePath($paths);
}
return $return;
}
// opcache
// retourne vrai si opcache roule
final public static function opcache():bool
{
return !empty(self::get('opcache.enable'));
}
// xdebug
// retourne vrai si xdebug roule
final public static function xdebug():bool
{
return !empty(self::get('xdebug.overload_var_dump'));
}
// apcu
// retourne vrai si apcu roule
final public static function apcu():bool
{
return !empty(self::get('apc.enabled'));
}
// important
// retourne toutes les ini importantes
final public static function important(?int $format=null):array
{
$return = [];
$important = (array) self::$config['important'];
foreach ($important as $key => $value)
{
$return[$value] = self::get($value,$format);
}
return $return;
}
// session
// retourne toutes les ini de session
final public static function session():array
{
return self::all('session');
}
// requirement
// lance les tests de requirement
final public static function requirement():array
{
$return = [];
if(self::postMaxSize() <= 1)
$return[] = 'post_max_size_too_small';
if(self::postMaxSize() < self::uploadMaxFilesize())
$return[] = 'post_max_size_smaller_than_upload_max_filesize';
if(self::memoryLimit() < 128)
$return[] = 'memory_limit';
return $return;
}
// setDefault
// fait les changements ini par défaut
final public static function setDefault(?array $option=null):array
{
$return = [];
$option = Arr::plus(self::$config['default'],$option);
foreach ($option as $key => $value)
{
$return[$key] = self::set($key,$value);
}
return $return;
}
}
?>