1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Options |
5
|
|
|
* |
6
|
|
|
* A dictionary to handle application-wide options. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Options extends Dictionary { |
|
|
|
|
14
|
|
|
protected static $fields = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Load a PHP configuration file (script must return array) |
18
|
|
|
* @param string $filepath The path of the PHP config file |
19
|
|
|
* @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
20
|
|
|
*/ |
21
|
|
|
public static function loadPHP($filepath,$prefix_path=null){ |
22
|
|
|
ob_start(); |
23
|
|
|
$results = include($filepath); |
24
|
|
|
ob_end_clean(); |
25
|
|
|
if($results) static::loadArray($results,$prefix_path); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Load an INI configuration file |
30
|
|
|
* @param string $filepath The path of the INI config file |
31
|
|
|
* @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
32
|
|
|
*/ |
33
|
|
|
public static function loadINI($filepath,$prefix_path=null){ |
34
|
|
|
$results = parse_ini_file($filepath,true); |
35
|
|
|
if($results) static::loadArray($results,$prefix_path); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Load a JSON configuration file |
40
|
|
|
* @param string $filepath The path of the JSON config file |
41
|
|
|
* @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
42
|
|
|
*/ |
43
|
|
|
public static function loadJSON($filepath,$prefix_path=null){ |
44
|
|
|
$data = file_get_contents($filepath); |
45
|
|
|
$results = $data?json_decode($data,true):[]; |
46
|
|
|
if($results) static::loadArray($results,$prefix_path); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Load an array to the configuration |
51
|
|
|
* @param array $array The array to load |
52
|
|
|
* @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
53
|
|
|
*/ |
54
|
|
|
public static function loadArray(array $array,$prefix_path=null){ |
55
|
|
|
if (is_array($array)) { |
56
|
|
|
if ($prefix_path){ |
57
|
|
|
static::set($prefix_path,$array); |
58
|
|
|
} else { |
59
|
|
|
static::merge($array); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Load an ENV file |
66
|
|
|
* @param string $dir The directory of the ENV file |
67
|
|
|
* @param string $envname The filename for the ENV file (Default to `.env`) |
68
|
|
|
* @param string $prefix_path You can insert/update the loaded array to a specific key path, if omitted it will be merged with the whole dictionary |
69
|
|
|
*/ |
70
|
|
|
public static function loadENV($dir,$envname='.env',$prefix_path=null){ |
|
|
|
|
71
|
|
|
$dir = rtrim($dir,'/'); |
72
|
|
|
$results = []; |
73
|
|
|
foreach(file("$dir/$envname", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line){ |
74
|
|
|
$line = trim($line); |
75
|
|
|
if ($line[0]=='#' || strpos($line,'=')===false) continue; |
76
|
|
|
list($key,$value) = explode('=',$line,2); |
77
|
|
|
$key = trim(str_replace(['export ', "'", '"'], '', $key)); |
78
|
|
|
$value = stripslashes(trim($value,'"')); |
79
|
|
|
$results[$key] = preg_replace_callback('/\${([a-zA-Z0-9_]+)}/',function($m) use (&$results){ |
80
|
|
|
return isset($results[$m[1]]) ? $results[$m[1]] : ''; |
81
|
|
|
},$value); |
82
|
|
|
putenv("$key={$results[$key]}"); |
83
|
|
|
$_ENV[$key] = $results[$key]; |
84
|
|
|
} |
85
|
|
|
if($results) static::loadArray($results,$prefix_path); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.