3
* This file contains classes implementing configuration feature.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
13
* CConfiguration represents an array-based configuration.
15
* It can be used to initialize an object's properties.
17
* The configuration data may be obtained from a PHP script. For example,
20
* 'name'=>'My Application',
21
* 'defaultController'=>'index',
24
* Use the following code to load the above configuration data:
26
* $config=new CConfiguration('path/to/config.php');
29
* To apply the configuration to an object, call {@link applyTo()}.
30
* Each (key,value) pair in the configuration data is applied
31
* to the object like: $object->$key=$value.
33
* Since CConfiguration extends from {@link CMap}, it can be
34
* used like an associative array. See {@link CMap} for more details.
36
* @author Qiang Xue <qiang.xue@gmail.com>
37
* @version $Id: CConfiguration.php 2799 2011-01-01 19:31:13Z qiang.xue $
38
* @package system.collections
41
class CConfiguration extends CMap
45
* @param mixed $data if string, it represents a config file (a PHP script returning the configuration as an array);
46
* If array, it is config data.
48
public function __construct($data=null)
51
parent::__construct(require($data));
53
parent::__construct($data);
57
* Loads configuration data from a file and merges it with the existing configuration.
59
* A config file must be a PHP script returning a configuration array (like the following)
63
* 'name'=>'My Application',
64
* 'defaultController'=>'index',
68
* @param string $configFile configuration file path (if using relative path, be aware of what is the current path)
71
public function loadFromFile($configFile)
73
$data=require($configFile);
74
if($this->getCount()>0)
75
$this->mergeWith($data);
77
$this->copyFrom($data);
81
* Saves the configuration into a string.
82
* The string is a valid PHP expression representing the configuration data as an array.
83
* @return string the string representation of the configuration
85
public function saveAsString()
87
return str_replace("\r",'',var_export($this->toArray(),true));
91
* Applies the configuration to an object.
92
* Each (key,value) pair in the configuration data is applied
93
* to the object like: $object->$key=$value.
94
* @param object $object object to be applied with this configuration
96
public function applyTo($object)
98
foreach($this->toArray() as $key=>$value)
103
* Creates an object and initializes it based on the given configuration.
105
* NOTE: this method has been deprecated since version 1.0.1.
106
* Please use {@link YiiBase::createComponent Yii::createComponent}, instead.
108
* @param mixed $config the configuration. It can be either a string or an array.
109
* @return mixed the created object
110
* @throws CException if the configuration does not have 'class' value
112
public static function createObject($config)
114
if($config instanceof self)
115
$config=$config->toArray();
116
return Yii::createComponent($config);