1: <?php namespace Laravel; defined('DS') or die('No direct script access.');
2:
3: class Config {
4:
5: /**
6: * All of the loaded configuration items.
7: *
8: * The configuration arrays are keyed by their owning bundle and file.
9: *
10: * @var array
11: */
12: public static $items = array();
13:
14: /**
15: * A cache of the parsed configuration items.
16: *
17: * @var array
18: */
19: public static $cache = array();
20:
21: /**
22: * The configuration loader event name.
23: *
24: * @var string
25: */
26: const loader = 'laravel.config.loader';
27:
28: /**
29: * Determine if a configuration item or file exists.
30: *
31: * <code>
32: * // Determine if the "session" configuration file exists
33: * $exists = Config::has('session');
34: *
35: * // Determine if the "timezone" option exists in the configuration
36: * $exists = Config::has('application.timezone');
37: * </code>
38: *
39: * @param string $key
40: * @return bool
41: */
42: public static function has($key)
43: {
44: return ! is_null(static::get($key));
45: }
46:
47: /**
48: * Get a configuration item.
49: *
50: * If no item is requested, the entire configuration array will be returned.
51: *
52: * <code>
53: * // Get the "session" configuration array
54: * $session = Config::get('session');
55: *
56: * // Get a configuration item from a bundle's configuration file
57: * $name = Config::get('admin::names.first');
58: *
59: * // Get the "timezone" option from the "application" configuration file
60: * $timezone = Config::get('application.timezone');
61: * </code>
62: *
63: * @param string $key
64: * @param mixed $default
65: * @return array
66: */
67: public static function get($key, $default = null)
68: {
69: list($bundle, $file, $item) = static::parse($key);
70:
71: if ( ! static::load($bundle, $file)) return value($default);
72:
73: $items = static::$items[$bundle][$file];
74:
75: // If a specific configuration item was not requested, the key will be null,
76: // meaning we'll return the entire array of configuration items from the
77: // requested configuration file. Otherwise we can return the item.
78: if (is_null($item))
79: {
80: return $items;
81: }
82: else
83: {
84: return array_get($items, $item, $default);
85: }
86: }
87:
88: /**
89: * Set a configuration item's value.
90: *
91: * <code>
92: * // Set the "session" configuration array
93: * Config::set('session', $array);
94: *
95: * // Set a configuration option that belongs by a bundle
96: * Config::set('admin::names.first', 'Taylor');
97: *
98: * // Set the "timezone" option in the "application" configuration file
99: * Config::set('application.timezone', 'UTC');
100: * </code>
101: *
102: * @param string $key
103: * @param mixed $value
104: * @return void
105: */
106: public static function set($key, $value)
107: {
108: list($bundle, $file, $item) = static::parse($key);
109:
110: static::load($bundle, $file);
111:
112: // If the item is null, it means the developer wishes to set the entire
113: // configuration array to a given value, so we will pass the entire
114: // array for the bundle into the array_set method.
115: if (is_null($item))
116: {
117: array_set(static::$items[$bundle], $file, $value);
118: }
119: else
120: {
121: array_set(static::$items[$bundle][$file], $item, $value);
122: }
123: }
124:
125: /**
126: * Parse a key and return its bundle, file, and key segments.
127: *
128: * Configuration items are named using the {bundle}::{file}.{item} convention.
129: *
130: * @param string $key
131: * @return array
132: */
133: protected static function parse($key)
134: {
135: // First, we'll check the keyed cache of configuration items, as this will
136: // be the fastest method of retrieving the configuration option. After an
137: // item is parsed, it is always stored in the cache by its key.
138: if (array_key_exists($key, static::$cache))
139: {
140: return static::$cache[$key];
141: }
142:
143: $bundle = Bundle::name($key);
144:
145: $segments = explode('.', Bundle::element($key));
146:
147: // If there are not at least two segments in the array, it means that the
148: // developer is requesting the entire configuration array to be returned.
149: // If that is the case, we'll make the item field "null".
150: if (count($segments) >= 2)
151: {
152: $parsed = array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
153: }
154: else
155: {
156: $parsed = array($bundle, $segments[0], null);
157: }
158:
159: return static::$cache[$key] = $parsed;
160: }
161:
162: /**
163: * Load all of the configuration items from a configuration file.
164: *
165: * @param string $bundle
166: * @param string $file
167: * @return bool
168: */
169: public static function load($bundle, $file)
170: {
171: if (isset(static::$items[$bundle][$file])) return true;
172:
173: // We allow a "config.loader" event to be registered which is responsible for
174: // returning an array representing the configuration for the bundle and file
175: // requested. This allows many types of config "drivers".
176: $config = Event::first(static::loader, func_get_args());
177:
178: // If configuration items were actually found for the bundle and file, we
179: // will add them to the configuration array and return true, otherwise
180: // we will return false indicating the file was not found.
181: if (count($config) > 0)
182: {
183: static::$items[$bundle][$file] = $config;
184: }
185:
186: return isset(static::$items[$bundle][$file]);
187: }
188:
189: /**
190: * Load the configuration items from a configuration file.
191: *
192: * @param string $bundle
193: * @param string $file
194: * @return array
195: */
196: public static function file($bundle, $file)
197: {
198: $config = array();
199:
200: // Configuration files cascade. Typically, the bundle configuration array is
201: // loaded first, followed by the environment array, providing the convenient
202: // cascading of configuration options across environments.
203: foreach (static::paths($bundle) as $directory)
204: {
205: if ($directory !== '' and file_exists($path = $directory.$file.EXT))
206: {
207: $config = array_merge($config, require $path);
208: }
209: }
210:
211: return $config;
212: }
213:
214: /**
215: * Get the array of configuration paths that should be searched for a bundle.
216: *
217: * @param string $bundle
218: * @return array
219: */
220: protected static function paths($bundle)
221: {
222: $paths[] = Bundle::path($bundle).'config/';
223:
224: // Configuration files can be made specific for a given environment. If an
225: // environment has been set, we will merge the environment configuration
226: // in last, so that it overrides all other options.
227: if ( ! is_null(Request::env()))
228: {
229: $paths[] = $paths[count($paths) - 1].Request::env().'/';
230: }
231:
232: return $paths;
233: }
234:
235: }