CConfiguration.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CConfiguration represents an array-based configuration.
  12. *
  13. * It can be used to initialize an object's properties.
  14. *
  15. * The configuration data may be obtained from a PHP script. For example,
  16. * <pre>
  17. * return array(
  18. * 'name'=>'My Application',
  19. * 'defaultController'=>'index',
  20. * );
  21. * </pre>
  22. * Use the following code to load the above configuration data:
  23. * <pre>
  24. * $config=new CConfiguration('path/to/config.php');
  25. * </pre>
  26. *
  27. * To apply the configuration to an object, call {@link applyTo()}.
  28. * Each (key,value) pair in the configuration data is applied
  29. * to the object like: $object->$key=$value.
  30. *
  31. * Since CConfiguration extends from {@link CMap}, it can be
  32. * used like an associative array. See {@link CMap} for more details.
  33. *
  34. * @author Qiang Xue <qiang.xue@gmail.com>
  35. * @package system.collections
  36. * @since 1.0
  37. */
  38. class CConfiguration extends CMap
  39. {
  40. /**
  41. * Constructor.
  42. * @param mixed $data if string, it represents a config file (a PHP script returning the configuration as an array);
  43. * If array, it is config data.
  44. */
  45. public function __construct($data=null)
  46. {
  47. if(is_string($data))
  48. parent::__construct(require($data));
  49. else
  50. parent::__construct($data);
  51. }
  52. /**
  53. * Loads configuration data from a file and merges it with the existing configuration.
  54. *
  55. * A config file must be a PHP script returning a configuration array (like the following)
  56. * <pre>
  57. * return array
  58. * (
  59. * 'name'=>'My Application',
  60. * 'defaultController'=>'index',
  61. * );
  62. * </pre>
  63. *
  64. * @param string $configFile configuration file path (if using relative path, be aware of what is the current path)
  65. * @see mergeWith
  66. */
  67. public function loadFromFile($configFile)
  68. {
  69. $data=require($configFile);
  70. if($this->getCount()>0)
  71. $this->mergeWith($data);
  72. else
  73. $this->copyFrom($data);
  74. }
  75. /**
  76. * Saves the configuration into a string.
  77. * The string is a valid PHP expression representing the configuration data as an array.
  78. * @return string the string representation of the configuration
  79. */
  80. public function saveAsString()
  81. {
  82. return str_replace("\r",'',var_export($this->toArray(),true));
  83. }
  84. /**
  85. * Applies the configuration to an object.
  86. * Each (key,value) pair in the configuration data is applied
  87. * to the object like: $object->$key=$value.
  88. * @param object $object object to be applied with this configuration
  89. */
  90. public function applyTo($object)
  91. {
  92. foreach($this->toArray() as $key=>$value)
  93. $object->$key=$value;
  94. }
  95. }