2016-10-21 22:11:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Repositories;
|
|
|
|
|
|
|
|
use ArrayAccess;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
|
|
|
|
class Repository implements ArrayAccess // Illuminate\Contracts\Cache\Repository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* All of the items.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-12-28 12:52:29 +08:00
|
|
|
protected $items = [];
|
2016-10-21 22:11:49 +08:00
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
/**
|
|
|
|
* All of the option items that is modified.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-17 17:07:41 +08:00
|
|
|
protected $itemsModified = [];
|
2016-11-18 16:46:58 +08:00
|
|
|
|
2016-10-21 22:11:49 +08:00
|
|
|
/**
|
|
|
|
* Determine if an item exists in the repository.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($key)
|
|
|
|
{
|
|
|
|
return Arr::has($this->items, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an item from the repository by key.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $default
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key, $default = null)
|
|
|
|
{
|
|
|
|
return Arr::get($this->items, $key, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-30 19:35:07 +08:00
|
|
|
* Set a given item value.
|
2016-10-21 22:11:49 +08:00
|
|
|
*
|
|
|
|
* @param array|string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set($key, $value = null)
|
|
|
|
{
|
|
|
|
if (is_array($key)) {
|
|
|
|
// If given key is an array
|
|
|
|
foreach ($key as $innerKey => $innerValue) {
|
|
|
|
Arr::set($this->items, $innerKey, $innerValue);
|
2016-12-17 17:07:41 +08:00
|
|
|
$this->itemsModified[] = $innerKey;
|
2016-10-21 22:11:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Arr::set($this->items, $key, $value);
|
2016-12-17 17:07:41 +08:00
|
|
|
$this->itemsModified[] = $key;
|
2016-10-21 22:11:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Push an item into the repository.
|
|
|
|
*
|
|
|
|
* @param mixed $item
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function push($item)
|
|
|
|
{
|
|
|
|
array_push($this->items, $item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all of the items stored in the repository.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an item from the repository, or store the default value.
|
|
|
|
*
|
2017-12-28 12:52:29 +08:00
|
|
|
* @param string $key
|
|
|
|
* @param callable $callback
|
2016-10-21 22:11:49 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-12-28 12:52:29 +08:00
|
|
|
public function remember($key, callable $callback)
|
2016-10-21 22:11:49 +08:00
|
|
|
{
|
|
|
|
// If the item exists in the repository we will just return this immediately
|
|
|
|
// otherwise we will execute the given Closure and repository the result
|
|
|
|
// of that execution for the given number of minutes in storage.
|
|
|
|
if (! is_null($value = $this->get($key))) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2017-12-28 12:52:29 +08:00
|
|
|
$this->set($key, $value = $callback());
|
2016-10-21 22:11:49 +08:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the repository.
|
|
|
|
*
|
2017-12-28 12:52:29 +08:00
|
|
|
* @param string|array $key
|
|
|
|
* @return void
|
2016-10-21 22:11:49 +08:00
|
|
|
*/
|
|
|
|
public function forget($key)
|
|
|
|
{
|
|
|
|
Arr::forget($this->items, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the given option option exists.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($key)
|
|
|
|
{
|
|
|
|
return $this->has($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a option option.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($key)
|
|
|
|
{
|
|
|
|
return $this->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a option option.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetSet($key, $value)
|
|
|
|
{
|
|
|
|
$this->set($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset a option option.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function offsetUnset($key)
|
|
|
|
{
|
|
|
|
$this->forget($key);
|
|
|
|
}
|
|
|
|
}
|