~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/collections/CConfiguration.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * This file contains classes implementing configuration feature.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
 
 
12
/**
 
13
 * CConfiguration represents an array-based configuration.
 
14
 *
 
15
 * It can be used to initialize an object's properties.
 
16
 *
 
17
 * The configuration data may be obtained from a PHP script. For example,
 
18
 * <pre>
 
19
 * return array(
 
20
 *     'name'=>'My Application',
 
21
 *     'defaultController'=>'index',
 
22
 * );
 
23
 * </pre>
 
24
 * Use the following code to load the above configuration data:
 
25
 * <pre>
 
26
 * $config=new CConfiguration('path/to/config.php');
 
27
 * </pre>
 
28
 *
 
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.
 
32
 *
 
33
 * Since CConfiguration extends from {@link CMap}, it can be
 
34
 * used like an associative array. See {@link CMap} for more details.
 
35
 *
 
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
 
39
 * @since 1.0
 
40
 */
 
41
class CConfiguration extends CMap
 
42
{
 
43
        /**
 
44
         * Constructor.
 
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.
 
47
         */
 
48
        public function __construct($data=null)
 
49
        {
 
50
                if(is_string($data))
 
51
                        parent::__construct(require($data));
 
52
                else
 
53
                        parent::__construct($data);
 
54
        }
 
55
 
 
56
        /**
 
57
         * Loads configuration data from a file and merges it with the existing configuration.
 
58
         *
 
59
         * A config file must be a PHP script returning a configuration array (like the following)
 
60
         * <pre>
 
61
         * return array
 
62
         * (
 
63
         *     'name'=>'My Application',
 
64
         *     'defaultController'=>'index',
 
65
         * );
 
66
         * </pre>
 
67
         *
 
68
         * @param string $configFile configuration file path (if using relative path, be aware of what is the current path)
 
69
         * @see mergeWith
 
70
         */
 
71
        public function loadFromFile($configFile)
 
72
        {
 
73
                $data=require($configFile);
 
74
                if($this->getCount()>0)
 
75
                        $this->mergeWith($data);
 
76
                else
 
77
                        $this->copyFrom($data);
 
78
        }
 
79
 
 
80
        /**
 
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
 
84
         */
 
85
        public function saveAsString()
 
86
        {
 
87
                return str_replace("\r",'',var_export($this->toArray(),true));
 
88
        }
 
89
 
 
90
        /**
 
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
 
95
         */
 
96
        public function applyTo($object)
 
97
        {
 
98
                foreach($this->toArray() as $key=>$value)
 
99
                        $object->$key=$value;
 
100
        }
 
101
 
 
102
        /**
 
103
         * Creates an object and initializes it based on the given configuration.
 
104
         *
 
105
         * NOTE: this method has been deprecated since version 1.0.1.
 
106
         * Please use {@link YiiBase::createComponent Yii::createComponent}, instead.
 
107
         *
 
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
 
111
         */
 
112
        public static function createObject($config)
 
113
        {
 
114
                if($config instanceof self)
 
115
                        $config=$config->toArray();
 
116
                return Yii::createComponent($config);
 
117
        }
 
118
}