laravel-enso/enums
Composer 安装命令:
composer require laravel-enso/enums
包简介
Enums for Laravel Enso
关键字:
README 文档
README
Description
Enums provides both legacy class-based enumerations and frontend-ready native PHP enum discovery for Laravel Enso.
The package keeps backwards compatibility with Enso's original enum classes while also scanning vendor and application Enums folders for native PHP enums that implement specific contracts. Those enums can then be registered into application state and exposed to the frontend in a normalized shape.
It is used throughout the Enso ecosystem for typed option lists, shared UI state, and enum mapping between backend code and frontend consumers.
::: warning Note Since PHP now provides native enums, we strongly recommend migrating new and existing code to native PHP enums wherever possible.
We still intend to keep supporting the Enso enum registry layer for ecosystem integration and frontend state registration, even as the legacy custom enum implementation is phased out. :::
Installation
Install the package:
composer require laravel-enso/enums
The package auto-registers:
LaravelEnso\Enums\AppServiceProviderLaravelEnso\Enums\EnumServiceProvider
If you want to customize which vendor namespaces are scanned, publish the configuration:
php artisan vendor:publish --tag=enums-config
If you want an application provider for registering legacy enums explicitly, publish the stub:
php artisan vendor:publish --tag=enum-provider
Default configuration:
return [ 'vendors' => ['laravel-enso'], ];
For native frontend enums, place enum classes under your PSR-4 Enums folder. In a standard Laravel application this means app/Enums.
Features
- Provides a base class for legacy class-based enums.
- Supports native PHP enums for frontend/state registration.
- Scans configured vendor packages and the host application for enum classes.
- Registers enum state under a single
enumsstore payload. - Exposes a facade for registering and removing legacy enums.
- Supports optional enum mapping through a dedicated
Mappablecontract. - Includes helper traits for select lists, searching, and random case selection.
Usage
Legacy Class-Based Enums
Extend LaravelEnso\Enums\Services\Enum:
use LaravelEnso\Enums\Services\Enum; class Roles extends Enum { public const Admin = 1; public const Supervisor = 2; public const User = 3; }
Available helpers:
Roles::get(1); Roles::has(2); Roles::keys(); Roles::values(); Roles::select(); Roles::json();
Native PHP Enums For Frontend State
Implement LaravelEnso\Enums\Contracts\Frontend on a backed enum:
use LaravelEnso\Enums\Contracts\Frontend; use LaravelEnso\Enums\Contracts\Mappable; use LaravelEnso\Enums\Traits\Select; enum Status: int implements Frontend, Mappable { use Select; case Draft = 0; case Published = 1; public static function registerBy(): string { return 'statuses'; } public function map(): mixed { return match ($this) { self::Draft => 'Draft', self::Published => 'Published', }; } }
The enum will be discovered from an Enums folder and registered under the key returned by registerBy().
Legacy Enum Registration
Register legacy enums through the facade or EnumServiceProvider:
use LaravelEnso\Enums\Facades\Enums; Enums::register([ 'roles' => \App\Enums\Roles::class, ]);
API
Configuration
config/enums.php
Keys:
vendors
The scanner checks:
vendor/<configured-vendor>/*- the application base path and its PSR-4 root
Service Providers
LaravelEnso\Enums\AppServiceProviderLaravelEnso\Enums\EnumServiceProvider
Responsibilities:
- merge and publish config
- publish the legacy enum provider stub
- register legacy enums listed in the provider's
$registerproperty
Legacy Enum Base Class
LaravelEnso\Enums\Services\Enum
Key methods:
constants(): arrayget($key)has($key): boolkeys()values()json(): stringarray(): arrayall(): arrayobject(): objectcollection()select()localisation(bool $state = true): void
Native Enum Discovery
LaravelEnso\Enums\Services\SourceLaravelEnso\Enums\Services\Enums
Requirements for discovery:
- enum must exist in an
Enumsfolder - enum must implement
LaravelEnso\Enums\Contracts\Frontend
Optional mapping:
- implement
LaravelEnso\Enums\Contracts\Mappable
Legacy Enum Registry
LaravelEnso\Enums\Services\LegacyEnumsLaravelEnso\Enums\Facades\Enums
Key methods:
register($enums)remove($aliases)all(): array
State Provider
LaravelEnso\Enums\State\Enums
Provides merged enum state for frontend consumption under the enums store key.
Traits
LaravelEnso\Enums\Traits\SelectLaravelEnso\Enums\Traits\SearchLaravelEnso\Enums\Traits\Random
Depends On
Required Enso packages:
Framework dependency:
Companion frontend package:
Contributions
are welcome. Pull requests are great, but issues are good too.
Thank you to all the people who already contributed to Enso!
laravel-enso/enums 适用场景与选型建议
laravel-enso/enums 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55.33k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enums」 「laravel-enso」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laravel-enso/enums 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravel-enso/enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laravel-enso/enums 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Company management dependency for Laravel Enso
Person management for Laravel Enso
Image transformer dependency for Laravel Enso
Search and interval helpers for Laravel Enso
Tutorial video management for Laravel Enso
Shared backed enums classifying HTML elements by category (block, inline, list, table, void, metadata, root) for the UI Awesome ecosystem.
统计信息
- 总下载量: 55.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 44
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-27