Repository.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Illuminate\Config;
  3. use ArrayAccess;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Contracts\Config\Repository as ConfigContract;
  6. class Repository implements ArrayAccess, ConfigContract
  7. {
  8. /**
  9. * All of the configuration items.
  10. *
  11. * @var array
  12. */
  13. protected $items = [];
  14. /**
  15. * Create a new configuration repository.
  16. *
  17. * @param array $items
  18. * @return void
  19. */
  20. public function __construct(array $items = [])
  21. {
  22. $this->items = $items;
  23. }
  24. /**
  25. * Determine if the given configuration value exists.
  26. *
  27. * @param string $key
  28. * @return bool
  29. */
  30. public function has($key)
  31. {
  32. return Arr::has($this->items, $key);
  33. }
  34. /**
  35. * Get the specified configuration value.
  36. *
  37. * @param array|string $key
  38. * @param mixed $default
  39. * @return mixed
  40. */
  41. public function get($key, $default = null)
  42. {
  43. if (is_array($key)) {
  44. return $this->getMany($key);
  45. }
  46. return Arr::get($this->items, $key, $default);
  47. }
  48. /**
  49. * Get many configuration values.
  50. *
  51. * @param array $keys
  52. * @return array
  53. */
  54. public function getMany($keys)
  55. {
  56. $config = [];
  57. foreach ($keys as $key => $default) {
  58. if (is_numeric($key)) {
  59. list($key, $default) = [$default, null];
  60. }
  61. $config[$key] = Arr::get($this->items, $key, $default);
  62. }
  63. return $config;
  64. }
  65. /**
  66. * Set a given configuration value.
  67. *
  68. * @param array|string $key
  69. * @param mixed $value
  70. * @return void
  71. */
  72. public function set($key, $value = null)
  73. {
  74. $keys = is_array($key) ? $key : [$key => $value];
  75. foreach ($keys as $key => $value) {
  76. Arr::set($this->items, $key, $value);
  77. }
  78. }
  79. /**
  80. * Prepend a value onto an array configuration value.
  81. *
  82. * @param string $key
  83. * @param mixed $value
  84. * @return void
  85. */
  86. public function prepend($key, $value)
  87. {
  88. $array = $this->get($key);
  89. array_unshift($array, $value);
  90. $this->set($key, $array);
  91. }
  92. /**
  93. * Push a value onto an array configuration value.
  94. *
  95. * @param string $key
  96. * @param mixed $value
  97. * @return void
  98. */
  99. public function push($key, $value)
  100. {
  101. $array = $this->get($key);
  102. $array[] = $value;
  103. $this->set($key, $array);
  104. }
  105. /**
  106. * Get all of the configuration items for the application.
  107. *
  108. * @return array
  109. */
  110. public function all()
  111. {
  112. return $this->items;
  113. }
  114. /**
  115. * Determine if the given configuration option exists.
  116. *
  117. * @param string $key
  118. * @return bool
  119. */
  120. public function offsetExists($key)
  121. {
  122. return $this->has($key);
  123. }
  124. /**
  125. * Get a configuration option.
  126. *
  127. * @param string $key
  128. * @return mixed
  129. */
  130. public function offsetGet($key)
  131. {
  132. return $this->get($key);
  133. }
  134. /**
  135. * Set a configuration option.
  136. *
  137. * @param string $key
  138. * @param mixed $value
  139. * @return void
  140. */
  141. public function offsetSet($key, $value)
  142. {
  143. $this->set($key, $value);
  144. }
  145. /**
  146. * Unset a configuration option.
  147. *
  148. * @param string $key
  149. * @return void
  150. */
  151. public function offsetUnset($key)
  152. {
  153. $this->set($key, null);
  154. }
  155. }