config.php 6.05 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel; defined('DS') or die('No direct script access.');
2

3 4 5
class Config {

	/**
6 7
	 * All of the loaded configuration items.
	 *
8
	 * The configuration arrays are keyed by their owning bundle and file.
9 10 11
	 *
	 * @var array
	 */
12
	public static $items = array();
13 14

	/**
15
	 * A cache of the parsed configuration items.
16 17 18
	 *
	 * @var array
	 */
19
	public static $cache = array();
20 21

	/**
22 23 24 25 26 27 28
	 * The configuration loader event name.
	 *
	 * @var string
	 */
	const loader = 'laravel.config.loader';

	/**
29 30 31 32 33 34
	 * Determine if a configuration item or file exists.
	 *
	 * <code>
	 *		// Determine if the "session" configuration file exists
	 *		$exists = Config::has('session');
	 *
35
	 *		// Determine if the "timezone" option exists in the configuration
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	 *		$exists = Config::has('application.timezone');
	 * </code>
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public static function has($key)
	{
		return ! is_null(static::get($key));
	}

	/**
	 * Get a configuration item.
	 *
	 * If no item is requested, the entire configuration array will be returned.
	 *
	 * <code>
	 *		// Get the "session" configuration array
	 *		$session = Config::get('session');
	 *
56 57 58
	 *		// Get a configuration item from a bundle's configuration file
	 *		$name = Config::get('admin::names.first');
	 *
59 60 61 62 63
	 *		// Get the "timezone" option from the "application" configuration file
	 *		$timezone = Config::get('application.timezone');
	 * </code>
	 *
	 * @param  string  $key
64
	 * @param  mixed   $default
65 66
	 * @return array
	 */
67
	public static function get($key, $default = null)
68
	{
69 70
		list($bundle, $file, $item) = static::parse($key);

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,
Taylor Otwell committed
76
		// meaning we'll to return the entire array of configuration item from the
77
		// requested configuration file. Otherwise we can return the item.
Taylor Otwell committed
78 79 80 81 82 83
		if (is_null($item))
		{
			return $items;
		}
		else
		{
84
			return array_get($items, $item, $default);
Taylor Otwell committed
85
		}
86 87 88 89 90 91 92 93 94
	}

	/**
	 * Set a configuration item's value.
	 *
	 * <code>
	 *		// Set the "session" configuration array
	 *		Config::set('session', $array);
	 *
95 96 97
	 *		// Set a configuration option that belongs by a bundle
	 *		Config::set('admin::names.first', 'Taylor');
	 *
98 99 100 101 102 103 104 105 106 107
	 *		// Set the "timezone" option in the "application" configuration file
	 *		Config::set('application.timezone', 'UTC');
	 * </code>
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
	public static function set($key, $value)
	{
Taylor Otwell committed
108 109 110 111 112 113
		list($bundle, $file, $item) = static::parse($key);

		static::load($bundle, $file);

		// If the item is null, it means the developer wishes to set the entire
		// configuration array to a given value, so we will pass the entire
114
		// array for the bundle into the array_set method.
Taylor Otwell committed
115 116 117 118 119 120 121 122
		if (is_null($item))
		{
			array_set(static::$items[$bundle], $file, $value);
		}
		else
		{
			array_set(static::$items[$bundle][$file], $item, $value);
		}
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 131 132 133 134
	 * @param  string  $key
	 * @return array
	 */
	protected static function parse($key)
	{
Taylor Otwell committed
135 136 137 138 139 140 141 142
		// First, we'll check the keyed cache of configuration items, as this will
		// be the fastest method of retrieving the configuration option. After an
		// item is parsed, it is always stored in the cache by its key.
		if (array_key_exists($key, static::$cache))
		{
			return static::$cache[$key];
		}

143
		$bundle = Bundle::name($key);
144

145 146 147 148
		$segments = explode('.', Bundle::element($key));

		// If there are not at least two segments in the array, it means that the
		// developer is requesting the entire configuration array to be returned.
Taylor Otwell committed
149
		// If that is the case, we'll make the item field "null".
150 151
		if (count($segments) >= 2)
		{
Taylor Otwell committed
152
			$parsed = array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
153 154 155
		}
		else
		{
Taylor Otwell committed
156
			$parsed = array($bundle, $segments[0], null);
157
		}
Taylor Otwell committed
158 159

		return static::$cache[$key] = $parsed;
160 161 162 163 164
	}

	/**
	 * Load all of the configuration items from a configuration file.
	 *
165
	 * @param  string  $bundle
166 167 168
	 * @param  string  $file
	 * @return bool
	 */
169
	public static function load($bundle, $file)
170
	{
171
		if (isset(static::$items[$bundle][$file])) return true;
172

173 174 175
		// We allow a "config.loader" event to be registered which is responsible for
		// returning an array representing the configuration for the bundle and file
		// requested. This allows many types of config "drivers".
176
		$config = Event::first(static::loader, func_get_args());
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

		// If configuration items were actually found for the bundle and file we
		// will add them to the configuration array and return true, otherwise
		// we will return false indicating the file was not found.
		if (count($config) > 0)
		{
			static::$items[$bundle][$file] = $config;
		}

		return isset(static::$items[$bundle][$file]);
	}

	/**
	 * Load the configuration items from a configuration file.
	 *
	 * @param  string  $bundle
	 * @param  string  $file
	 * @return array
	 */
	public static function file($bundle, $file)
	{
198 199
		$config = array();

200 201 202 203
		// Configuration files cascade. Typically, the bundle configuration array is
		// loaded first, followed by the environment array, providing the convenient
		// cascading of configuration options across environments.
		foreach (static::paths($bundle) as $directory)
204
		{
205
			if ($directory !== '' and file_exists($path = $directory.$file.EXT))
206 207 208 209 210
			{
				$config = array_merge($config, require $path);
			}
		}

211
		return $config;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	}

	/**
	 * Get the array of configuration paths that should be searched for a bundle.
	 *
	 * @param  string  $bundle
	 * @return array
	 */
	protected static function paths($bundle)
	{
		$paths[] = Bundle::path($bundle).'config/';

		// Configuration files can be made specific for a given environment. If an
		// environment has been set, we will merge the environment configuration
		// 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
	}

Taylor Otwell committed
235
}