PropelConfiguration.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * PropelConfiguration is a container for all Propel's configuration data.
  11. *
  12. * PropelConfiguration implements ArrayAccess interface so the configuration
  13. * can be accessed as an array or using a simple getter and setter. The whole
  14. * configuration can also be retrieved as a nested arrays, flat array or as a
  15. * PropelConfiguration instance.
  16. *
  17. * @author Veikko Mäkinen <veikko@veikko.fi>
  18. * @version $Revision: 1612 $
  19. * @package propel.runtime.config
  20. */
  21. class PropelConfiguration implements ArrayAccess
  22. {
  23. const TYPE_ARRAY = 1;
  24. const TYPE_ARRAY_FLAT = 2;
  25. const TYPE_OBJECT = 3;
  26. /**
  27. * @var array An array of parameters
  28. */
  29. protected $parameters = array();
  30. /**
  31. * Construct a new configuration container
  32. *
  33. * @param array $parameters
  34. */
  35. public function __construct(array $parameters = array())
  36. {
  37. $this->parameters = $parameters;
  38. }
  39. /**
  40. * @see http://www.php.net/ArrayAccess
  41. */
  42. public function offsetExists($offset)
  43. {
  44. return array_key_exists($offset, $this->parameters);
  45. }
  46. /**
  47. * @see http://www.php.net/ArrayAccess
  48. */
  49. public function offsetSet($offset, $value)
  50. {
  51. $this->parameter[$offset] = $value;
  52. }
  53. /**
  54. * @see http://www.php.net/ArrayAccess
  55. */
  56. public function offsetGet($offset)
  57. {
  58. return $this->parameters[$offset];
  59. }
  60. /**
  61. * @see http://www.php.net/ArrayAccess
  62. */
  63. public function offsetUnset($offset)
  64. {
  65. unset($this->parameters[$offset]);
  66. }
  67. /**
  68. * Get parameter value from the container
  69. *
  70. * @param string $name Parameter name
  71. * @param mixed $default Default value to be used if the
  72. * requested value is not found
  73. * @return mixed Parameter value or the default
  74. */
  75. public function getParameter($name, $default = null)
  76. {
  77. $ret = $this->parameters;
  78. $parts = explode('.', $name); //name.space.name
  79. while ($part = array_shift($parts)) {
  80. if (isset($ret[$part])) {
  81. $ret = $ret[$part];
  82. } else {
  83. return $default;
  84. }
  85. }
  86. return $ret;
  87. }
  88. /**
  89. * Store a value to the container
  90. *
  91. * @param string $name Configuration item name (name.space.name)
  92. * @param mixed $value Value to be stored
  93. */
  94. public function setParameter($name, $value)
  95. {
  96. $param = &$this->parameters;
  97. $parts = explode('.', $name); //name.space.name
  98. while ($part = array_shift($parts)) {
  99. $param = &$param[$part];
  100. }
  101. $param = $value;
  102. }
  103. /**
  104. *
  105. *
  106. * @param int $type
  107. * @return mixed
  108. */
  109. public function getParameters($type = PropelConfiguration::TYPE_ARRAY)
  110. {
  111. switch ($type) {
  112. case PropelConfiguration::TYPE_ARRAY:
  113. return $this->parameters;
  114. case PropelConfiguration::TYPE_ARRAY_FLAT:
  115. return $this->toFlatArray();
  116. case PropelConfiguration::TYPE_OBJECT:
  117. return $this;
  118. default:
  119. throw new PropelException('Unknown configuration type: '. var_export($type, true));
  120. }
  121. }
  122. /**
  123. * Get the configuration as a flat array. ($array['name.space.item'] = 'value')
  124. *
  125. * @return array
  126. */
  127. protected function toFlatArray()
  128. {
  129. $result = array();
  130. $it = new PropelConfigurationIterator(new RecursiveArrayIterator($this->parameters), RecursiveIteratorIterator::SELF_FIRST);
  131. foreach($it as $key => $value) {
  132. $ns = $it->getDepth() ? $it->getNamespace() . '.'. $key : $key;
  133. if ($it->getNodeType() == PropelConfigurationIterator::NODE_ITEM) {
  134. $result[$ns] = $value;
  135. }
  136. }
  137. return $result;
  138. }
  139. }
  140. ?>