Group.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * The group wrapper acts as an interface to all the config directives
  4. * gathered from across the system.
  5. *
  6. * This is the object returned from Kohana_Config::load
  7. *
  8. * Any modifications to configuration items should be done through an instance of this object
  9. *
  10. * @package Kohana
  11. * @category Configuration
  12. * @author Kohana Team
  13. * @copyright (c) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  15. */
  16. class Kohana_Config_Group extends ArrayObject {
  17. /**
  18. * Reference the config object that created this group
  19. * Used when updating config
  20. * @var Kohana_Config
  21. */
  22. protected $_parent_instance = NULL;
  23. /**
  24. * The group this config is for
  25. * Used when updating config items
  26. * @var string
  27. */
  28. protected $_group_name = '';
  29. /**
  30. * Constructs the group object. Kohana_Config passes the config group
  31. * and its config items to the object here.
  32. *
  33. * @param Kohana_Config $instance "Owning" instance of Kohana_Config
  34. * @param string $group The group name
  35. * @param array $config Group's config
  36. */
  37. public function __construct(Kohana_Config $instance, $group, array $config = [])
  38. {
  39. $this->_parent_instance = $instance;
  40. $this->_group_name = $group;
  41. parent::__construct($config, ArrayObject::ARRAY_AS_PROPS);
  42. }
  43. /**
  44. * Return the current group in serialized form.
  45. *
  46. * echo $config;
  47. *
  48. * @return string
  49. */
  50. public function __toString()
  51. {
  52. return serialize($this->getArrayCopy());
  53. }
  54. /**
  55. * Alias for getArrayCopy()
  56. *
  57. * @return array Array copy of the group's config
  58. */
  59. public function as_array()
  60. {
  61. return $this->getArrayCopy();
  62. }
  63. /**
  64. * Returns the config group's name
  65. *
  66. * @return string The group name
  67. */
  68. public function group_name()
  69. {
  70. return $this->_group_name;
  71. }
  72. /**
  73. * Get a variable from the configuration or return the default value.
  74. *
  75. * $value = $config->get($key);
  76. *
  77. * @param string $key array key
  78. * @param mixed $default default value
  79. * @return mixed
  80. */
  81. public function get($key, $default = NULL)
  82. {
  83. return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
  84. }
  85. /**
  86. * Sets a value in the configuration array.
  87. *
  88. * $config->set($key, $new_value);
  89. *
  90. * @param string $key array key
  91. * @param mixed $value array value
  92. * @return $this
  93. */
  94. public function set($key, $value)
  95. {
  96. $this->offsetSet($key, $value);
  97. return $this;
  98. }
  99. /**
  100. * Overrides ArrayObject::offsetSet()
  101. * This method is called when config is changed via
  102. *
  103. * $config->var = 'asd';
  104. *
  105. * // OR
  106. *
  107. * $config['var'] = 'asd';
  108. *
  109. * @param string $key The key of the config item we're changing
  110. * @param mixed $value The new array value
  111. */
  112. #[\ReturnTypeWillChange]
  113. public function offsetSet($key, $value)
  114. {
  115. $this->_parent_instance->_write_config($this->_group_name, $key, $value);
  116. return parent::offsetSet($key, $value);
  117. }
  118. }