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