Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 118 lines (112 sloc) 3.371 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
<?php
/**
* This file contains classes implementing configuration feature.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/


/**
* CConfiguration represents an array-based configuration.
*
* It can be used to initialize an object's properties.
*
* The configuration data may be obtained from a PHP script. For example,
* <pre>
* return array(
* 'name'=>'My Application',
* 'defaultController'=>'index',
* );
* </pre>
* Use the following code to load the above configuration data:
* <pre>
* $config=new CConfiguration('path/to/config.php');
* </pre>
*
* To apply the configuration to an object, call {@link applyTo()}.
* Each (key,value) pair in the configuration data is applied
* to the object like: $object->$key=$value.
*
* Since CConfiguration extends from {@link CMap}, it can be
* used like an associative array. See {@link CMap} for more details.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.collections
* @since 1.0
*/
class CConfiguration extends CMap
{
/**
* Constructor.
* @param mixed $data if string, it represents a config file (a PHP script returning the configuration as an array);
* If array, it is config data.
*/
public function __construct($data=null)
{
if(is_string($data))
parent::__construct(require($data));
else
parent::__construct($data);
}

/**
* Loads configuration data from a file and merges it with the existing configuration.
*
* A config file must be a PHP script returning a configuration array (like the following)
* <pre>
* return array
* (
* 'name'=>'My Application',
* 'defaultController'=>'index',
* );
* </pre>
*
* @param string $configFile configuration file path (if using relative path, be aware of what is the current path)
* @see mergeWith
*/
public function loadFromFile($configFile)
{
$data=require($configFile);
if($this->getCount()>0)
$this->mergeWith($data);
else
$this->copyFrom($data);
}

/**
* Saves the configuration into a string.
* The string is a valid PHP expression representing the configuration data as an array.
* @return string the string representation of the configuration
*/
public function saveAsString()
{
return str_replace("\r",'',var_export($this->toArray(),true));
}

/**
* Applies the configuration to an object.
* Each (key,value) pair in the configuration data is applied
* to the object like: $object->$key=$value.
* @param object $object object to be applied with this configuration
*/
public function applyTo($object)
{
foreach($this->toArray() as $key=>$value)
$object->$key=$value;
}

/**
* Creates an object and initializes it based on the given configuration.
*
* NOTE: this method has been deprecated since version 1.0.1.
* Please use {@link YiiBase::createComponent Yii::createComponent}, instead.
*
* @param mixed $config the configuration. It can be either a string or an array.
* @return mixed the created object
* @throws CException if the configuration does not have 'class' value
*/
public static function createObject($config)
{
if($config instanceof self)
$config=$config->toArray();
return Yii::createComponent($config);
}
}
Something went wrong with that request. Please try again.