├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── valuestore.php └── src ├── CachedValuestore.php ├── CodecManager.php ├── Codecs ├── Codec.php ├── Json.php └── Yaml.php ├── Exceptions ├── DecodeException.php ├── EncodeException.php └── UnknownCodecException.php ├── Valuestore.php └── ValuestoreServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-valuestore` will be documented in this file. 4 | 5 | ## 1.0.0 - 2022-05-09 6 | 7 | - added `CachedValuestore` 8 | 9 | ## 0.2.0 - 2022-04-02 10 | 11 | - added Yaml support 12 | 13 | ## 0.1.0 - 2022-03-12 14 | 15 | - initial release 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) bvtterfly 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨 2 | 3 | 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. 4 | 5 | 6 | # Laravel Valuestore 7 | 8 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/bvtterfly/laravel-valuestore.svg?style=flat-square)](https://packagist.org/packages/bvtterfly/laravel-valuestore) 9 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/bvtterfly/laravel-valuestore/run-tests?label=tests)](https://github.com/bvtterfly/laravel-valuestore/actions?query=workflow%3Arun-tests+branch%3Amain) 10 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/bvtterfly/laravel-valuestore/Check%20&%20fix%20styling?label=code%20style)](https://github.com/bvtterfly/laravel-valuestore/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 11 | [![Total Downloads](https://img.shields.io/packagist/dt/bvtterfly/laravel-valuestore.svg?style=flat-square)](https://packagist.org/packages/bvtterfly/laravel-valuestore) 12 | 13 | This package makes it easy to store and retrieve some loose values. The values are saved as JSON/Yaml files. 14 | The package integrates with the Laravel filesystem and adds Yaml support by extending the `spatie/valuestore` package. 15 | 16 | It can be used like this: 17 | 18 | ```php 19 | use Bvttterfly\Valuestore\Valuestore; 20 | 21 | $valuestore = Valuestore::make($filename); 22 | 23 | $valuestore->put('key', 'value'); 24 | 25 | $valuestore->get('key'); // Returns 'value' 26 | 27 | $valuestore->has('key'); // Returns true 28 | 29 | // Specify a default value for when the specified key does not exist 30 | $valuestore->get('non existing key', 'default') // Returns 'default' 31 | 32 | $valuestore->put('anotherKey', 'anotherValue'); 33 | 34 | // Put multiple items in one go 35 | $valuestore->put(['ringo' => 'drums', 'paul' => 'bass']); 36 | 37 | $valuestore->all(); // Returns an array with all items 38 | 39 | $valuestore->forget('key'); // Removes the item 40 | 41 | $valuestore->flush(); // Empty the entire valuestore 42 | 43 | $valuestore->flushStartingWith('somekey'); // remove all items whose keys start with "somekey" 44 | 45 | $valuestore->increment('number'); // $valuestore->get('number') will return 1 46 | $valuestore->increment('number'); // $valuestore->get('number') will return 2 47 | $valuestore->increment('number', 3); // $valuestore->get('number') will return 5 48 | 49 | // Valuestore implements ArrayAccess 50 | $valuestore['key'] = 'value'; 51 | $valuestore['key']; // Returns 'value' 52 | isset($valuestore['key']); // Return true 53 | unset($valuestore['key']); // Equivalent to removing the value 54 | 55 | // Valuestore implements Countable 56 | count($valuestore); // Returns 0 57 | $valuestore->put('key', 'value'); 58 | count($valuestore); // Returns 1 59 | ``` 60 | 61 | 62 | ## Installation 63 | 64 | You can install the package via composer: 65 | 66 | ```bash 67 | composer require bvtterfly/laravel-valuestore 68 | ``` 69 | 70 | You can publish the config file with: 71 | 72 | ```bash 73 | php artisan vendor:publish --tag="valuestore-config" 74 | ``` 75 | 76 | This is the contents of the published config file: 77 | 78 | ```php 79 | return [ 80 | /* 81 | |-------------------------------------------------------------------------- 82 | | Valuestore Filesystem Disk 83 | |-------------------------------------------------------------------------- 84 | | 85 | | Here you may specify the filesystem disk that should be used 86 | | by the Valuestore. 87 | */ 88 | 'disk' => config('filesystems.default'), 89 | ]; 90 | ``` 91 | 92 | ## Usage 93 | 94 | To create a Valuestore use the `make` method. 95 | 96 | ```php 97 | $valuestore = Valuestore::make($pathToFile); 98 | ``` 99 | 100 | You can also pass some values as a second argument. These will be added to the valuestore using the `put` method. 101 | 102 | ```php 103 | $valuestore = Valuestore::make($pathToFile, ['key' => 'value']); 104 | ``` 105 | 106 | All values will be saved as json/yaml in the given file. 107 | 108 | When there are no values stored, the file will be deleted. 109 | 110 | ### Cached Valuestore 111 | 112 | `Valuestore` would call the `all` method behind the scene to get values from the store every time. 113 | 114 | `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. 115 | ```php 116 | $valuestore = CachedValuestore::make($pathToFile); 117 | ``` 118 | 119 | If you want to clear the cache, You can use the `clearCache` method. 120 | It is done during persistence, so you are unlikely to need it. 121 | 122 | ```php 123 | $valuestore->clearCache(); 124 | ``` 125 | 126 | ## Valuestore methods 127 | 128 | You can call the following methods on the `Valuestore` & `CachedValuestore` 129 | 130 | ### put 131 | ```php 132 | /** 133 | * Put a value in the store. 134 | * 135 | * @param string|array $name 136 | * @param string|int|null $value 137 | * 138 | * @return $this 139 | */ 140 | public function put($name, $value = null) 141 | ``` 142 | 143 | ### get 144 | 145 | ```php 146 | /** 147 | * Get a value from the store. 148 | * 149 | * @param string $name 150 | * 151 | * @return null|string 152 | */ 153 | public function get(string $name) 154 | ``` 155 | 156 | ### has 157 | 158 | ```php 159 | /* 160 | * Determine if the store has a value for the given name. 161 | */ 162 | public function has(string $name) : bool 163 | ``` 164 | 165 | ### all 166 | ```php 167 | /** 168 | * Get all values from the store. 169 | * 170 | * @return array 171 | */ 172 | public function all() : array 173 | ``` 174 | 175 | ### allStartingWith 176 | ```php 177 | /** 178 | * Get all values from the store which keys start with the given string. 179 | * 180 | * @param string $startingWith 181 | * 182 | * @return array 183 | */ 184 | public function allStartingWith(string $startingWith = '') : array 185 | ``` 186 | 187 | ### forget 188 | ```php 189 | /** 190 | * Forget a value from the store. 191 | * 192 | * @param string $key 193 | * 194 | * @return $this 195 | */ 196 | public function forget(string $key) 197 | ``` 198 | 199 | ### flush 200 | ```php 201 | /** 202 | * Flush all values from the store. 203 | * 204 | * @return $this 205 | */ 206 | public function flush() 207 | ``` 208 | 209 | ### flushStartingWith 210 | ```php 211 | /** 212 | * Flush all values from the store which keys start with the specified value. 213 | * 214 | * @param string $startingWith 215 | * 216 | * @return $this 217 | */ 218 | public function flushStartingWith(string $startingWith) 219 | ``` 220 | 221 | ### pull 222 | ```php 223 | /** 224 | * Get and forget a value from the store. 225 | * 226 | * @param string $name 227 | * 228 | * @return null|string 229 | */ 230 | public function pull(string $name) 231 | ``` 232 | 233 | ### increment 234 | ```php 235 | /** 236 | * Increment a value from the store. 237 | * 238 | * @param string $name 239 | * @param int $by 240 | * 241 | * @return int|null|string 242 | */ 243 | public function increment(string $name, int $by = 1) 244 | ``` 245 | 246 | ### decrement 247 | ```php 248 | /** 249 | * Decrement a value from the store. 250 | * 251 | * @param string $name 252 | * @param int $by 253 | * 254 | * @return int|null|string 255 | */ 256 | public function decrement(string $name, int $by = 1) 257 | ``` 258 | 259 | ## push 260 | ```php 261 | /** 262 | * Push a new value into an array. 263 | * 264 | * @param string $name 265 | * @param $pushValue 266 | * 267 | * @return $this 268 | */ 269 | public function push(string $name, $pushValue) 270 | ``` 271 | 272 | ## prepend 273 | ```php 274 | /** 275 | * Prepend a new value into an array. 276 | * 277 | * @param string $name 278 | * @param $prependValue 279 | * 280 | * @return $this 281 | */ 282 | public function prepend(string $name, $prependValue) 283 | ``` 284 | 285 | ## count 286 | ```php 287 | /** 288 | * Count elements. 289 | * 290 | * @return int 291 | */ 292 | public function count() 293 | ``` 294 | 295 | ## Testing 296 | 297 | ```bash 298 | composer test 299 | ``` 300 | 301 | ## Changelog 302 | 303 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 304 | 305 | ## Contributing 306 | 307 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 308 | 309 | ## Security Vulnerabilities 310 | 311 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 312 | 313 | ## Credits 314 | 315 | - [ARI](https://github.com/bvtterfly) 316 | - [All Contributors](../../contributors) 317 | 318 | [Tim MacDonald](https://github.com/timacdonald) was the original developer of the `CachedValuestore`. We slightly polished the code he created in the [repo](https://github.com/timacdonald/cached-valuestore). 319 | 320 | ## License 321 | 322 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 323 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bvtterfly/laravel-valuestore", 3 | "description": "Easily store some values", 4 | "keywords": [ 5 | "bvtterfly", 6 | "laravel", 7 | "laravel-valuestore" 8 | ], 9 | "homepage": "https://github.com/bvtterfly/laravel-valuestore", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "ARI", 14 | "email": "thearihdrn@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/contracts": "^9.0", 21 | "spatie/laravel-package-tools": "^1.9.2", 22 | "spatie/valuestore": "^1.3", 23 | "symfony/yaml": "^6.0" 24 | }, 25 | "require-dev": { 26 | "nunomaduro/collision": "^6.0", 27 | "nunomaduro/larastan": "^2.0.1", 28 | "orchestra/testbench": "^7.0", 29 | "pestphp/pest": "^1.21", 30 | "pestphp/pest-plugin-laravel": "^1.1", 31 | "phpstan/extension-installer": "^1.1", 32 | "phpstan/phpstan-deprecation-rules": "^1.0", 33 | "phpstan/phpstan-phpunit": "^1.0", 34 | "phpunit/phpunit": "^9.5", 35 | "spatie/laravel-ray": "^1.26" 36 | }, 37 | "autoload": { 38 | "psr-4": { 39 | "Bvtterfly\\Valuestore\\": "src" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "Bvtterfly\\Valuestore\\Tests\\": "tests" 45 | } 46 | }, 47 | "scripts": { 48 | "analyse": "vendor/bin/phpstan analyse", 49 | "test": "vendor/bin/pest", 50 | "test-coverage": "vendor/bin/pest --coverage" 51 | }, 52 | "config": { 53 | "sort-packages": true, 54 | "allow-plugins": { 55 | "pestphp/pest-plugin": true, 56 | "phpstan/extension-installer": true 57 | } 58 | }, 59 | "extra": { 60 | "laravel": { 61 | "providers": [ 62 | "Bvtterfly\\Valuestore\\ValuestoreServiceProvider" 63 | ], 64 | "aliases": { 65 | "Valuestore": "Bvtterfly\\Valuestore\\Facades\\Valuestore" 66 | } 67 | } 68 | }, 69 | "minimum-stability": "dev", 70 | "prefer-stable": true 71 | } 72 | -------------------------------------------------------------------------------- /config/valuestore.php: -------------------------------------------------------------------------------- 1 | config('filesystems.default'), 13 | ]; 14 | -------------------------------------------------------------------------------- /src/CachedValuestore.php: -------------------------------------------------------------------------------- 1 | cache = $values); 17 | } 18 | 19 | /** 20 | * Get all values from the store. 21 | * 22 | * @return array 23 | */ 24 | public function all(): array 25 | { 26 | return $this->cache ?? $this->cache = parent::all(); 27 | } 28 | 29 | /** 30 | * Clears the local cache. 31 | * 32 | * @return $this 33 | */ 34 | public function clearCache() 35 | { 36 | $this->cache = null; 37 | 38 | return $this; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/CodecManager.php: -------------------------------------------------------------------------------- 1 | app(Json::class), 17 | 'yaml' => app(Yaml::class), 18 | default => throw new UnknownCodecException() 19 | }; 20 | } 21 | 22 | public static function guessType(string $fileName): string 23 | { 24 | return Str::of($fileName) 25 | ->lower() 26 | ->afterLast('.') 27 | ->value(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Codecs/Codec.php: -------------------------------------------------------------------------------- 1 | getMessage()); 17 | } 18 | } 19 | 20 | /** 21 | * @throws DecodeException 22 | */ 23 | public function decode(string $content): ?array 24 | { 25 | try { 26 | return json_decode($content, true, flags: JSON_THROW_ON_ERROR); 27 | } catch (JsonException $exception) { 28 | throw new DecodeException($exception->getMessage()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Codecs/Yaml.php: -------------------------------------------------------------------------------- 1 | getMessage()); 17 | } 18 | } 19 | 20 | public function decode(string $content): ?array 21 | { 22 | try { 23 | return \Symfony\Component\Yaml\Yaml::parse($content); 24 | } catch (Exception $exception) { 25 | throw new DecodeException($exception->getMessage()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Exceptions/DecodeException.php: -------------------------------------------------------------------------------- 1 | setFileName($fileName); 23 | 24 | if (! is_null($values)) { 25 | $valuestore->put($values); 26 | } 27 | 28 | return $valuestore; 29 | } 30 | 31 | protected function __construct(protected Filesystem $filesystem, protected Codec $codec) 32 | { 33 | parent::__construct(); 34 | } 35 | 36 | /** 37 | * @param array $values 38 | * 39 | * @return $this 40 | */ 41 | protected function setContent(array $values) 42 | { 43 | $this->filesystem->put($this->fileName, $this->codec->encode($values)); 44 | 45 | if (! count($values)) { 46 | $this->filesystem->delete($this->fileName); 47 | } 48 | 49 | return $this; 50 | } 51 | 52 | /** 53 | * Get all values from the store. 54 | * 55 | * @return array 56 | */ 57 | public function all(): array 58 | { 59 | if (! $this->filesystem->exists($this->fileName)) { 60 | return []; 61 | } 62 | 63 | return $this->codec->decode($this->filesystem->get($this->fileName)) ?? []; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ValuestoreServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-valuestore') 19 | ->hasConfigFile(); 20 | } 21 | } 22 | --------------------------------------------------------------------------------