kylearch/ephemeral-users
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
kylearch/ephemeral-users 适用场景与选型建议
kylearch/ephemeral-users 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.36k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kylearch/ephemeral-users 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kylearch/ephemeral-users 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-20