1: <?php
2: namespace Azalea\Util;
3:
4: class Config
5: {
6: /** @var string $path */
7: protected static $path = null;
8:
9: /** @var string $environment */
10: protected static $environment = "";
11:
12: /** @var array $cache */
13: protected static $cache = array();
14:
15: /**
16: * Get a configuration value.
17: * @param string $path
18: * @param mixed $default
19: * @return mixed
20: */
21: public static function get($key, $default = null)
22: {
23: // parse the path and load the configuration
24: list($group, $item) = self::parseKey($key);
25:
26: $items = self::getItems($group);
27:
28: if (!isset($items[$item])) {
29: return $default;
30: }
31:
32: return $items[$item];
33: }
34:
35: /**
36: * Set a configuration.
37: * The configuration's value is only changed for the duration
38: * of the request.
39: * @param string $path
40: * @param mixed $value
41: */
42: public static function set($key, $value)
43: {
44: // parse the path and load the configuration
45: list($group, $item) = self::parseKey($key);
46:
47: // load into the cache, if not already
48: self::getItems($group);
49:
50: self::$cache[$group][$item] = $value;
51: }
52:
53: /**
54: * Sets the path to the config directory
55: * @param string $path
56: */
57: public static function setPath($path)
58: {
59: self::$path = $path;
60: self::$cache = array();
61: }
62:
63: /**
64: * Gets the path to the config directory
65: * @return string
66: */
67: public static function getPath()
68: {
69: return self::$path;
70: }
71:
72: /**
73: * Sets the environment
74: * @param string $path
75: */
76: public static function setEnvironment($env)
77: {
78: self::$environment = $env;
79: self::$cache = array();
80: }
81:
82: /**
83: * Gets the environment
84: * @return string
85: */
86: public static function getEnvironment()
87: {
88: return self::$environment;
89: }
90:
91: /**
92: * @param string $path
93: * @return array
94: */
95: protected static function getItems($group)
96: {
97: if (isset(self::$cache[$group])) {
98: return self::$cache[$group];
99: }
100:
101: // load the list of configs for the given group
102: $items = self::load($group);
103: self::$cache[$group] = $items;
104: return $items;
105: }
106:
107: /**
108: * @param string $path
109: * @throws \InvalidArgumentException
110: */
111: protected static function load($group)
112: {
113: $items = array();
114:
115: $path = self::getPath();
116:
117: if (is_null($path)) {
118: return $items;
119: }
120:
121: // Load the main configuration items for the group
122: $file = "{$path}/{$group}.php";
123:
124: if (file_exists($file)) {
125: $items = self::requireFile($file);
126: }
127:
128: // Load the environment specific configuration file and merge them on
129: // top of the main configuration items.
130: $environment = self::getEnvironment();
131: $file = "{$path}/{$environment}/{$group}.php";
132:
133: if (!empty($environment) && file_exists($file)) {
134: $items = array_replace_recursive($items, self::requireFile($file));
135: }
136:
137: return $items;
138: }
139:
140: /**
141: * @param array $key
142: * @throws \InvalidArgumentException
143: * @return array
144: */
145: protected static function parseKey($key)
146: {
147: // key is in the form of group.item
148: $pieces = explode(".", $key);
149:
150: if (count($pieces) === 0) {
151: throw new \InvalidArgumentException("Config: invalid path ");
152: }
153:
154: return $pieces;
155: }
156:
157: protected static function requireFile($file)
158: {
159: return require $file;
160: }
161: }
162: ?>