mkd/laravel-state-management
Composer 安装命令:
composer require mkd/laravel-state-management
包简介
a state management package for Laravel, enabling shared and persistent application state across services, requests, and files without the need for reinitialization. It supports casting, default state handling, and custom methods for managing complex state in your Laravel applications
关键字:
README 文档
README
A state management solution for Laravel applications inspired by Redux, designed to manage complex application state across services, caching layers, and requests, with support for casting, default state handling, and custom methods.
Features
- Shared State Across Application:Stores are shared globally across the application, making them easily accessible without reinitialization in multiple files.
- State Persistence and Rehydration: Manage application state easily, persisting and rehydrating data as needed.
- Casting Attributes: Automatically cast attributes to types like collections or custom classes using Laravel's castable functionality.
- Default State Handling: Define fallback states to be used when rehydration fails.
- Custom Store Logic: Add custom methods to interact with specific store states and manage application logic.
Installation
Install the package using Composer:
composer require mkd/laravel-state-management
Basic Usage
Step 1: Create a Store
You can create a store class using the store:make Artisan command:
php artisan store:make UserStore
This command will generate a new store class in your app/Stores directory.
Step 2: Define Your Store
In the generated store, define your attributes and casts. The store will manage the state related to these attributes:
class UserStore extends StoreContract { protected $attributes = [ 'user', 'email', 'status' ]; protected $casts = [ 'email' => StringCast::class, // Custom cast ]; protected $enums = [ 'status' => CustomStatusEnum::class // Use enums for statuses ]; public function default(): array { return ['user' => User::find($this->key)]; // Fallback if rehydration fails } public function updateUserName($name) { $user = $this->getUser(); $user->name = $name; $user->save(); } }
Step 3: Using a Store
To interact with a store and manage the state, you can retrieve the store instance and access its methods:
use App\Stores\UserStore; public function handleState(StateManagement $stateManagement) { // Retrieve the store and set state values $userStore = $stateManagement->store(UserStore::class); $userStore->setUser(User::first()); // Call custom methods to manage store data $userStore->updateUserName('New Name'); }
Step 4: Handling Persistent State
You can persist and rehydrate states based on a unique key, enabling state restoration between requests:
$userStore = StateManagement::use(UserStore::class); $userStore->setKey(1); $userStore->rehydrate(); $status = $userStore->getStatus(); // Retrieve status from the store
Step 5: Defining Custom Casts
If you need custom casting for certain attributes, use the store-cast:make command:
php artisan store-cast:make EmailCast
Then, define the casting logic in the generated cast class:
class EmailCast implements StateCastAttribute { public function get($model, string $key, $value, array $attributes) { return strtolower($value); } public function set($model, string $key, $value, array $attributes) { return strtoupper($value); } }
Store Rehydration Example
$settingsStore = StateManagement::use(SettingsStore::class); $settingsStore->setKey(auth()->user()->id); $settingsStore->rehydrate(); $countries = $settingsStore->getCountries();
Commands
store:make <StoreName>: Generates a new store class.store-cast:make <CastName>: Generates a new custom cast class.
Example Stores
SettingsStore
This store manages application settings and allows for the dynamic update of user preferences.
class SettingsStore extends StoreContract { protected $attributes = ['countries', 'cities', 'user']; protected $casts = ['countries' => CollectionCast::class, 'cities' => CollectionCast::class]; public function default(): array { return ['countries' => ['id' => 1, 'name' => 'USA'], 'cities' => ['id' => 2, 'name' => 'New York']]; } public function updateUserSettings($key, $value) { $this->getUser()->updateSettings($key, $value); } }
UserNotification
This store handles sending notifications, such as emails, to users.
class UserNotification extends StoreContract { protected $attributes = ['user', 'email']; protected $casts = ['email' => EmailCast::class]; public function sendInvoiceEmail(Invoice $invoice) { $this->getUser()->notify(new InvoiceEmail($invoice)); } }
Persist
By Default Persist is saving the state object in cache so it can be easy rehydrated later with the key
use App\Stores\UserStore; public function handleState(StateManagement $stateManagement) { $user = User::first(); // Retrieve the store and set state values $userStore = $stateManagement->store(UserStore::class); $userStore->setUser($user); $userStore->setKey($user->id); // Call custom methods to manage store data $userStore->updateUserName('New Name'); $userStore->persist(); }
You can override persist logic by implementing your own logic in store class
public function persistUsing() { //Your own persist logic //Init custom persist flag $this->initCustomPersist() }
rehydrate
By Default rehydrate is setting the state from cache based on store key
use App\Stores\UserStore; public function handleState(StateManagement $stateManagement) { $user = User::first(); // Retrieve the store and set state values $userStore = $stateManagement->store(UserStore::class); $userStore->setKey($user->id); $userStore->rehydrate(); $userStore->getUser()->name // 'New Name' }
You can override rehydrate logic by implementing your own logic in store class
public function rehydrateUsing() { // your own rehydrating logic //Init custom rehydrate flag $this->initCustomRehydrate(); }
License
This package is open-sourced software licensed under the MIT license.
mkd/laravel-state-management 适用场景与选型建议
mkd/laravel-state-management 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「caching」 「laravel」 「redux」 「laravel package」 「global state」 「state management」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mkd/laravel-state-management 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mkd/laravel-state-management 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mkd/laravel-state-management 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Query caching for Laravel 5
Build better and beautiful sites in WordPress, faster.
Simple PHP caching system that uses a tmp folder in a Linux environment.
Provides support for PSR-6 compatible cache for Yii1 application
Laravel React Boilerplate
Build better and beautiful sites in WordPress, faster.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-17