config.php 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php namespace System;

class Config {

	/**
	 * All of the loaded configuration items.
	 *
	 * @var array
	 */
10
	public static $items = array();
11 12

	/**
13
	 * Determine if a configuration item or file exists.
14 15 16 17 18 19 20 21 22 23
	 *
	 * @param  string  $key
	 * @return bool
	 */
	public static function has($key)
	{
		return ! is_null(static::get($key));
	}

	/**
24 25
	 * Get a configuration item.
	 *
26 27 28 29 30 31 32
	 * Configuration items are retrieved using "dot" notation. So, asking for the
	 * "application.timezone" configuration item would return the "timezone" option
	 * from the "application" configuration file.
	 *
	 * If the name of a configuration file is passed without specifying an item, the
	 * entire configuration array will be returned.
	 *
33
	 * @param  string  $key
34
	 * @param  string  $default
35
	 * @return array
36
	 */
37
	public static function get($key, $default = null)
38
	{
Taylor Otwell committed
39
		list($module, $file, $key) = static::parse($key);
Taylor Otwell committed
40

41 42 43 44 45 46
		if ( ! static::load($module, $file))
		{
			return is_callable($default) ? call_user_func($default) : $default;
		}

		if (is_null($key)) return static::$items[$module][$file];
47

48
		return Arr::get(static::$items[$module][$file], $key, $default);
49 50 51 52 53 54 55 56 57
	}

	/**
	 * Set a configuration item.
	 *
	 * @param  string  $key
	 * @param  mixed   $value
	 * @return void
	 */
58
	public static function set($key, $value)
59
	{
Taylor Otwell committed
60
		list($module, $file, $key) = static::parse($key);
Taylor Otwell committed
61

62 63 64 65
		if ( ! static::load($module, $file))
		{
			throw new \Exception("Error setting configuration option. Configuration file [$file] is not defined.");
		}
66

67
		Arr::set(static::$items[$module][$file], $key, $value);
68 69 70
	}

	/**
71 72 73 74
	 * Parse a configuration key.
	 *
	 * The value on the left side of the dot is the configuration file
	 * name, while the right side of the dot is the item within that file.
75 76 77 78 79 80
	 *
	 * @param  string  $key
	 * @return array
	 */
	private static function parse($key)
	{
Taylor Otwell committed
81
		$module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
82

83 84 85 86
		if ($module !== 'application')
		{
			$key = substr($key, strpos($key, ':') + 2);
		}
87

88
		$key = (count($segments = explode('.', $key)) > 1) ? implode('.', array_slice($segments, 1)) : null;
Taylor Otwell committed
89 90

		return array($module, $segments[0], $key);
91 92 93
	}

	/**
94
	 * Load all of the configuration items from a file.
95
	 *
96 97 98
	 * Laravel supports environment specific configuration files. So, the base configuration
	 * array will be loaded first, then any environment specific options will be merged in.
	 *
99
	 * @param  string  $file
Taylor Otwell committed
100
	 * @param  string  $module
101
	 * @return bool
102
	 */
103
	private static function load($module, $file)
104
	{
105
		if (isset(static::$items[$module]) and array_key_exists($file, static::$items[$module])) return true;
Taylor Otwell committed
106 107

		$path = ($module === 'application') ? CONFIG_PATH : MODULE_PATH.$module.'/config/';
108

109 110 111
		// Load the base configuration file. Once that is loaded, we will merge any environment
		// specific configuration options into the base array. This allows for the convenient
		// cascading of configuration options depending on the application environment.
Taylor Otwell committed
112
		$config = (file_exists($base = $path.$file.EXT)) ? require $base : array();
113

Taylor Otwell committed
114
		if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = $path.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
115
		{
116
			$config = array_merge($config, require $path);
117
		}
118

119 120 121
		if (count($config) > 0) static::$items[$module][$file] = $config;

		return isset(static::$items[$module][$file]);
122 123 124
	}

}