🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨
I no longer use Laravel and cannot justify the time needed to maintain this package. That's why I have chosen to abandon it. Feel free to fork my code and maintain your own copy.
This package makes it easy to store and retrieve some loose values. The values are saved as JSON/Yaml files.
The package integrates with the Laravel filesystem and adds Yaml support by extending the spatie/valuestore
package.
It can be used like this:
use Bvttterfly\Valuestore\Valuestore;
$valuestore = Valuestore::make($filename);
$valuestore->put('key', 'value');
$valuestore->get('key'); // Returns 'value'
$valuestore->has('key'); // Returns true
// Specify a default value for when the specified key does not exist
$valuestore->get('non existing key', 'default') // Returns 'default'
$valuestore->put('anotherKey', 'anotherValue');
// Put multiple items in one go
$valuestore->put(['ringo' => 'drums', 'paul' => 'bass']);
$valuestore->all(); // Returns an array with all items
$valuestore->forget('key'); // Removes the item
$valuestore->flush(); // Empty the entire valuestore
$valuestore->flushStartingWith('somekey'); // remove all items whose keys start with "somekey"
$valuestore->increment('number'); // $valuestore->get('number') will return 1
$valuestore->increment('number'); // $valuestore->get('number') will return 2
$valuestore->increment('number', 3); // $valuestore->get('number') will return 5
// Valuestore implements ArrayAccess
$valuestore['key'] = 'value';
$valuestore['key']; // Returns 'value'
isset($valuestore['key']); // Return true
unset($valuestore['key']); // Equivalent to removing the value
// Valuestore implements Countable
count($valuestore); // Returns 0
$valuestore->put('key', 'value');
count($valuestore); // Returns 1
You can install the package via composer:
composer require bvtterfly/laravel-valuestore
You can publish the config file with:
php artisan vendor:publish --tag="valuestore-config"
This is the contents of the published config file:
return [
/*
|--------------------------------------------------------------------------
| Valuestore Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the filesystem disk that should be used
| by the Valuestore.
*/
'disk' => config('filesystems.default'),
];
To create a Valuestore use the make
method.
$valuestore = Valuestore::make($pathToFile);
You can also pass some values as a second argument. These will be added to the valuestore using the put
method.
$valuestore = Valuestore::make($pathToFile, ['key' => 'value']);
All values will be saved as json/yaml in the given file.
When there are no values stored, the file will be deleted.
Valuestore
would call the all
method behind the scene to get values from the store every time.
CachedValuestore
is an extension of Valuestore
with a local cache. With CachedValuestore
, the all
method gets values from the cache instead of reading the file to get the store values.
$valuestore = CachedValuestore::make($pathToFile);
If you want to clear the cache, You can use the clearCache
method.
It is done during persistence, so you are unlikely to need it.
$valuestore->clearCache();
You can call the following methods on the Valuestore
& CachedValuestore
/**
* Put a value in the store.
*
* @param string|array $name
* @param string|int|null $value
*
* @return $this
*/
public function put($name, $value = null)
/**
* Get a value from the store.
*
* @param string $name
*
* @return null|string
*/
public function get(string $name)
/*
* Determine if the store has a value for the given name.
*/
public function has(string $name) : bool
/**
* Get all values from the store.
*
* @return array
*/
public function all() : array
/**
* Get all values from the store which keys start with the given string.
*
* @param string $startingWith
*
* @return array
*/
public function allStartingWith(string $startingWith = '') : array
/**
* Forget a value from the store.
*
* @param string $key
*
* @return $this
*/
public function forget(string $key)
/**
* Flush all values from the store.
*
* @return $this
*/
public function flush()
/**
* Flush all values from the store which keys start with the specified value.
*
* @param string $startingWith
*
* @return $this
*/
public function flushStartingWith(string $startingWith)
/**
* Get and forget a value from the store.
*
* @param string $name
*
* @return null|string
*/
public function pull(string $name)
/**
* Increment a value from the store.
*
* @param string $name
* @param int $by
*
* @return int|null|string
*/
public function increment(string $name, int $by = 1)
/**
* Decrement a value from the store.
*
* @param string $name
* @param int $by
*
* @return int|null|string
*/
public function decrement(string $name, int $by = 1)
/**
* Push a new value into an array.
*
* @param string $name
* @param $pushValue
*
* @return $this
*/
public function push(string $name, $pushValue)
/**
* Prepend a new value into an array.
*
* @param string $name
* @param $prependValue
*
* @return $this
*/
public function prepend(string $name, $prependValue)
/**
* Count elements.
*
* @return int
*/
public function count()
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
Tim MacDonald was the original developer of the CachedValuestore
. We slightly polished the code he created in the repo.
The MIT License (MIT). Please see License File for more information.