PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/framework/boot/config/Config.php

http://zoop.googlecode.com/
PHP | 151 lines | 104 code | 22 blank | 25 comment | 11 complexity | 6e21327dc591f5328784a1eef55d4ebe MD5 | raw file
  1<?php
  2/**
  3 * Configuration class
  4 * 
  5 * Provides methods for retrieving configuration options from a YAML config file.
  6 *
  7 */
  8class Config
  9{
 10	private static $info = array();
 11	private static $file;
 12	
 13	static public function suggest($file, $prefix = NULL)
 14	{
 15		if($prefix)
 16			$root = &self::getReference($prefix);
 17		else
 18			$root = &self::$info;
 19		
 20		$root = self::mergeArray(Yaml::read($file), $root, true);
 21	}
 22	
 23	static public function insist($file, $prefix = NULL)
 24	{
 25		if($prefix)
 26			$root = &self::getReference($prefix);
 27		else
 28			$root = &self::$info;
 29		
 30		$root = self::mergeArray($root, Yaml::read($file), false);
 31	}
 32	
 33	//
 34	// these should maybe be put into a generic utilities file
 35	//
 36	
 37	static public function mergeArray($suggested, $insisted, $insistedFirst)
 38	{
 39		return self::_mergeArray($suggested, $insisted, $insistedFirst);
 40	}
 41	
 42	static public function _mergeArray(&$suggested, &$insisted, $insistedFirst)
 43	{
 44		if($insistedFirst)
 45		{
 46			foreach($suggested as $key => $val)
 47			{
 48				if(is_array($val))
 49					self::_mergeArray($suggested[$key], $insisted[$key], $insistedFirst);
 50				else
 51					$insisted[$key] = $val;
 52			}
 53
 54			return $insisted;
 55		}
 56		else
 57		{
 58			foreach($insisted as $key => $val)
 59			{
 60				if(is_array($val))
 61					self::_mergeArray($suggested[$key], $insisted[$key], $insistedFirst);
 62				else
 63					$suggested[$key] = $val;
 64			}
 65
 66			return $suggested;
 67		}
 68	}
 69	
 70	/**
 71	 * Specify configuration file to use
 72	 *
 73	 * @param string $file Path and filename of the config file to use
 74	 */
 75	static function setConfigFile($file)
 76	{
 77		self::$file = $file;
 78	}
 79	
 80	/**
 81	 * Loads the config file specified by the $file member variable (or app_dir/config.yaml) 
 82	 *
 83	 */
 84	static function load()
 85	{
 86		self::suggest(zoop_dir . '/config.yaml', 'zoop');
 87		
 88		if(!self::$file)
 89			self::setConfigFile(app_dir . '/config.yaml');
 90		self::insist(self::$file);
 91		
 92		if(defined('instance_dir') && instance_dir)
 93			self::insist(instance_dir . '/config.yaml');
 94	}
 95	
 96	/**
 97	 * Returns configuration options based on a path (i.e. zoop.db or zoop.application.info)
 98	 *
 99	 * @param string $path Path for which to fetch options
100	 * @return array of configuration values
101	 */
102	static function get($path)
103	{
104		if($path === '')
105			return self::$info;
106		
107		$parts = explode('.', $path);
108		$cur = self::$info;
109		
110		foreach($parts as $thisPart)
111			if(isset($cur[$thisPart]))
112				$cur = $cur[$thisPart];
113			else
114				return false;
115		
116		return $cur;
117	}	
118	
119	static public function set($path, $value)
120	{
121		$ref = &self::getReference($path, false);
122		$ref = $value;
123	}
124	
125	static function &getReference($path)
126	{
127		$parts = explode('.', $path);
128		$cur = &self::$info;
129		
130		foreach($parts as $thisPart)
131		{
132			if(isset($cur[$thisPart]))
133				$cur = &$cur[$thisPart];
134			else
135			{
136				$cur[$thisPart] = array();
137				$cur = &$cur[$thisPart];
138			}
139		}
140		
141		return $cur;
142	}
143	
144	//	functions for getting scalar values and then formatting them
145	static public function getFilePath($configPath)
146	{
147		$config = self::get($configPath);
148		assert(is_string($config));
149		return Zoop::expandPath($config);
150	}
151}