Repository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace app\common\repositories;
  3. use ArrayAccess;
  4. use Illuminate\Support\Arr;
  5. class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository
  6. {
  7. /**
  8. * All of the items.
  9. *
  10. * @var array
  11. */
  12. protected $items;
  13. /**
  14. * All of the option items that is modified.
  15. *
  16. * @var array
  17. */
  18. protected $itemsModified = [];
  19. /**
  20. * Determine if an item exists in the repository.
  21. *
  22. * @param string $key
  23. * @return bool
  24. */
  25. public function has($key)
  26. {
  27. return Arr::has($this->items, $key);
  28. }
  29. /**
  30. * Retrieve an item from the repository by key.
  31. *
  32. * @param string $key
  33. * @param mixed $default
  34. * @return mixed
  35. */
  36. public function get($key, $default = null)
  37. {
  38. return Arr::get($this->items, $key, $default);
  39. }
  40. /**
  41. * Set a given item value.
  42. *
  43. * @param array|string $key
  44. * @param mixed $value
  45. * @return void
  46. */
  47. public function set($key, $value = null)
  48. {
  49. if (is_array($key)) {
  50. // If given key is an array
  51. foreach ($key as $innerKey => $innerValue) {
  52. Arr::set($this->items, $innerKey, $innerValue);
  53. $this->itemsModified[] = $innerKey;
  54. }
  55. } else {
  56. Arr::set($this->items, $key, $value);
  57. $this->itemsModified[] = $key;
  58. }
  59. }
  60. /**
  61. * Push an item into the repository.
  62. *
  63. * @param mixed $item
  64. * @return void
  65. */
  66. public function push($item)
  67. {
  68. array_push($this->items, $item);
  69. }
  70. /**
  71. * Get all of the items stored in the repository.
  72. *
  73. * @return array
  74. */
  75. public function all()
  76. {
  77. return $this->items;
  78. }
  79. /**
  80. * Get an item from the repository, or store the default value.
  81. *
  82. * @param string $key
  83. * @param \Closure $callback
  84. * @return mixed
  85. */
  86. public function remember($key, Closure $callback)
  87. {
  88. // If the item exists in the repository we will just return this immediately
  89. // otherwise we will execute the given Closure and repository the result
  90. // of that execution for the given number of minutes in storage.
  91. if (! is_null($value = $this->get($key))) {
  92. return $value;
  93. }
  94. $this->put($key, $value = $callback());
  95. return $value;
  96. }
  97. /**
  98. * Remove an item from the repository.
  99. *
  100. * @param string $key
  101. * @return bool
  102. */
  103. public function forget($key)
  104. {
  105. Arr::forget($this->items, $key);
  106. }
  107. /**
  108. * Determine if the given option option exists.
  109. *
  110. * @param string $key
  111. * @return bool
  112. */
  113. public function offsetExists($key)
  114. {
  115. return $this->has($key);
  116. }
  117. /**
  118. * Get a option option.
  119. *
  120. * @param string $key
  121. * @return mixed
  122. */
  123. public function offsetGet($key)
  124. {
  125. return $this->get($key);
  126. }
  127. /**
  128. * Set a option option.
  129. *
  130. * @param string $key
  131. * @param mixed $value
  132. * @return void
  133. */
  134. public function offsetSet($key, $value)
  135. {
  136. $this->set($key, $value);
  137. }
  138. /**
  139. * Unset a option option.
  140. *
  141. * @param string $key
  142. * @return void
  143. */
  144. public function offsetUnset($key)
  145. {
  146. $this->forget($key);
  147. }
  148. }