kylearch/ephemeral-users
最新稳定版本:v1.0.0
Composer 安装命令:
composer require kylearch/ephemeral-users
包简介
Laravel package for ephemeral (non-persistent) user instances
README 文档
README
A Laravel package for creating ephemeral (non-persistent) user instances. Perfect for handling anonymous users, session-based users, or any scenario where you need a User object without database persistence.
Features
- 🚫 Prevent Accidental Persistence - Ephemeral users throw exceptions when save attempts are made
- 📝 Automatic Logging - Track code paths that attempt to persist ephemeral users
- 🔧 Configurable Behavior - Choose between throwing exceptions or silently preventing saves
- 🎯 Type Safe - Full type hinting and interface support
- 📦 Easy Integration - Simple trait-based implementation
Installation
Add the package to your Laravel application:
composer require kylearch/ephemeral-users
Publish the configuration file (optional):
php artisan vendor:publish --tag=ephemeral-users-config
Usage
Basic Setup
Implement the interface and use the trait in your User model:
use KyleArch\EphemeralUsers\Contracts\EphemeralUser as EphemeralUserContract; use KyleArch\EphemeralUsers\Concerns\HasEphemeralState; class User extends Authenticatable implements EphemeralUserContract { use HasEphemeralState; // Your existing User model code... }
Creating Ephemeral Users
// Create an ephemeral user instance $ephemeralUser = User::ephemeral([ 'id' => 'session-abc123', 'email' => 'anonymous@example.com', 'name' => 'Anonymous User', ]); // Check if a user is ephemeral if ($ephemeralUser->isEphemeral()) { // Handle ephemeral user logic } // Get the ephemeral identifier $identifier = $ephemeralUser->getEphemeralIdentifier(); // 'session-abc123'
Persistence Protection
Attempting to save an ephemeral user will:
- Log the attempt (if logging is enabled)
- Throw an
EphemeralPersistenceException(if configured) - Prevent the save operation
$ephemeralUser = User::ephemeral(['id' => 'test']); try { $ephemeralUser->save(); // Throws EphemeralPersistenceException } catch (EphemeralPersistenceException $e) { // Handle the exception $user = $e->getEphemeralUser(); }
Configuration
The package supports the following configuration options:
return [ // Throw exception on persist attempts (default: true) 'throw_on_persist' => env('EPHEMERAL_THROW_ON_PERSIST', true), // Log persist attempts (default: true) 'log_attempts' => env('EPHEMERAL_LOG_PERSIST', true), // Log channel to use 'log_channel' => env('EPHEMERAL_LOG_CHANNEL', 'stack'), // Log level for persist attempts 'log_level' => env('EPHEMERAL_LOG_LEVEL', 'warning'), ];
Use Cases
- Anonymous Users: Handle session-based users without database records
- Testing: Create test users without database persistence
- API Integration: Represent external users without local persistence
- Guest Checkout: Allow guest users with full User object interface
- Sample/Demo Flows: Enable trial experiences without account creation
License
MIT
统计信息
- 总下载量: 1.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-20