bvtterfly/laravel-valuestore

该包已被废弃,不再维护。未建议替代包。

轻松存储一些值

1.0.0 2022-05-09 20:45 UTC

README

🚨 本包已废弃 🚨

我不再使用Laravel,无法证明维护此包所需的时间。因此,我选择放弃它。您可以随意fork我的代码并维护自己的副本。

Laravel Valuestore

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

此包可以轻松存储和检索一些散值。值以JSON/Yaml文件的形式保存。该包与Laravel文件系统集成,并通过扩展spatie/valuestore包添加了Yaml支持。

使用方法

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

安装

您可以通过composer安装此包

composer require bvtterfly/laravel-valuestore

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="valuestore-config"

这是已发布配置文件的内容

return [
    /*
    |--------------------------------------------------------------------------
    | Valuestore Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the filesystem disk that should be used
    | by the Valuestore.
    */
    'disk' => config('filesystems.default'),
];

使用

要创建Valuestore,请使用make方法。

$valuestore = Valuestore::make($pathToFile);

您还可以将一些值作为第二个参数传递。这些值将使用put方法添加到valuestore中。

$valuestore = Valuestore::make($pathToFile, ['key' => 'value']);

所有值都将保存为json/yaml文件。

如果没有存储值,则文件将被删除。

缓存Valuestore

Valuestore会在每次调用时在幕后调用all方法从存储中获取值。

CachedValuestoreValuestore的扩展,具有本地缓存。使用CachedValuestoreall方法会从缓存中获取值,而不是读取文件以获取存储值。

$valuestore = CachedValuestore::make($pathToFile);

如果您想清除缓存,可以使用clearCache方法。它在持久化期间完成,因此您可能不需要它。

$valuestore->clearCache();

Valuestore方法

您可以在ValuestoreCachedValuestore上调用以下方法

put

/**
 * Put a value in the store.
 *
 * @param string|array $name
 * @param string|int|null $value
 * 
 * @return $this
 */
public function put($name, $value = null)

get

/**
 * Get a value from the store.
 *
 * @param string $name
 *
 * @return null|string
 */
public function get(string $name)

has

/*
 * Determine if the store has a value for the given name.
 */
public function has(string $name) : bool

all

/**
 * Get all values from the store.
 *
 * @return array
 */
public function all() : array

allStartingWith

/**
 * 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

/**
 * Forget a value from the store.
 *
 * @param string $key
 *
 * @return $this
 */
public function forget(string $key)

flush

/**
 * Flush all values from the store.
 *
 * @return $this
 */
 public function flush()

flushStartingWith

/**
 * Flush all values from the store which keys start with the specified value.
 *
 * @param string $startingWith
 *
 * @return $this
 */
 public function flushStartingWith(string $startingWith)

pull

/**
 * Get and forget a value from the store.
 *
 * @param string $name 
 *
 * @return null|string
 */
public function pull(string $name)

increment

/**
 * 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

/**
 * 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

/**
 * Push a new value into an array.
 *
 * @param string $name
 * @param $pushValue
 *
 * @return $this
 */
public function push(string $name, $pushValue)

prepend

/**
 * Prepend a new value into an array.
 *
 * @param string $name
 * @param $prependValue
 *
 * @return $this
 */
public function prepend(string $name, $prependValue)

count

/**
 * Count elements.
 *
 * @return int
 */
public function count()

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

请查看我们的安全策略,了解如何报告安全漏洞。

鸣谢

Tim MacDonaldCachedValuestore的原始开发者。我们对他在repo中创建的代码进行了轻微的润色。

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件