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

use Closure;
4 5 6 7

class Config {

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

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

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

	/**
31 32 33 34 35 36
	 * Determine if a configuration item or file exists.
	 *
	 * <code>
	 *		// Determine if the "session" configuration file exists
	 *		$exists = Config::has('session');
	 *
37
	 *		// Determine if the "timezone" option exists in the configuration
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	 *		$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');
	 *
58 59 60
	 *		// Get a configuration item from a bundle's configuration file
	 *		$name = Config::get('admin::names.first');
	 *
61 62 63 64 65
	 *		// Get the "timezone" option from the "application" configuration file
	 *		$timezone = Config::get('application.timezone');
	 * </code>
	 *
	 * @param  string  $key
66
	 * @param  mixed   $default
67 68
	 * @return array
	 */
69
	public static function get($key, $default = null)
70
	{
71 72
		list($bundle, $file, $item) = static::parse($key);

73
		if ( ! static::load($bundle, $file)) return value($default);
74 75

		$items = static::$items[$bundle][$file];
76

77
		// If a specific configuration item was not requested, the key will be null,
Taylor Otwell committed
78
		// meaning we'll to return the entire array of configuration item from the
79
		// requested configuration file. Otherwise we can return the item.
Taylor Otwell committed
80 81 82 83 84 85
		if (is_null($item))
		{
			return $items;
		}
		else
		{
86
			return array_get($items, $item, $default);
Taylor Otwell committed
87
		}
88 89 90 91 92 93 94 95 96
	}

	/**
	 * Set a configuration item's value.
	 *
	 * <code>
	 *		// Set the "session" configuration array
	 *		Config::set('session', $array);
	 *
97 98 99
	 *		// Set a configuration option that belongs by a bundle
	 *		Config::set('admin::names.first', 'Taylor');
	 *
100 101 102 103 104 105 106 107 108 109
	 *		// 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
110 111 112 113 114 115
		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
116
		// array for the bundle into the array_set method.
Taylor Otwell committed
117 118 119 120 121 122 123 124
		if (is_null($item))
		{
			array_set(static::$items[$bundle], $file, $value);
		}
		else
		{
			array_set(static::$items[$bundle][$file], $item, $value);
		}
125 126 127
	}

	/**
128
	 * Parse a key and return its bundle, file, and key segments.
129
	 *
130
	 * Configuration items are named using the {bundle}::{file}.{item} convention.
131
	 *
132 133 134 135 136
	 * @param  string  $key
	 * @return array
	 */
	protected static function parse($key)
	{
Taylor Otwell committed
137 138 139 140 141 142 143 144
		// 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];
		}

145
		$bundle = Bundle::name($key);
146

147 148 149 150
		$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
151
		// If that is the case, we'll make the item field "null".
152 153
		if (count($segments) >= 2)
		{
Taylor Otwell committed
154
			$parsed = array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
155 156 157
		}
		else
		{
Taylor Otwell committed
158
			$parsed = array($bundle, $segments[0], null);
159
		}
Taylor Otwell committed
160 161

		return static::$cache[$key] = $parsed;
162 163 164 165 166
	}

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

175 176 177
		// 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".
178
		$config = Event::first(static::loader, func_get_args());
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

		// 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)
	{
200 201
		$config = array();

202 203 204 205
		// 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)
206
		{
207
			if ($directory !== '' and file_exists($path = $directory.$file.EXT))
208 209 210 211 212
			{
				$config = array_merge($config, require $path);
			}
		}

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

	/**
	 * 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.
229
		if ( ! is_null(Request::env()))
230
		{
231
			$paths[] = $paths[count($paths) - 1].Request::env().'/';
232
		}
233

234
		return $paths;
235 236
	}

Taylor Otwell committed
237
}