mkd/laravel-state-management
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.
Install the package using Composer:
composer require mkd/laravel-state-management
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.
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();
}
}
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');
}
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
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);
}
}
$settingsStore = StateManagement::use(SettingsStore::class);
$settingsStore->setKey(auth()->user()->id);
$settingsStore->rehydrate();
$countries = $settingsStore->getCountries();
store:make <StoreName>: Generates a new store class.store-cast:make <CastName>: Generates a new custom cast class.SettingsStoreThis 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);
}
}
UserNotificationThis 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));
}
}
PersistBy 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()
}
rehydrateBy 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();
}
This package is open-sourced software licensed under the MIT license.
How can I help you explore Laravel packages today?