定制 laravel-enso/enums 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-enso/enums

Composer 安装命令:

composer require laravel-enso/enums

包简介

Enums for Laravel Enso

README 文档

README

License Stable Downloads PHP Issues Merge Requests

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\AppServiceProvider
  • LaravelEnso\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 enums store payload.
  • Exposes a facade for registering and removing legacy enums.
  • Supports optional enum mapping through a dedicated Mappable contract.
  • 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\AppServiceProvider
  • LaravelEnso\Enums\EnumServiceProvider

Responsibilities:

  • merge and publish config
  • publish the legacy enum provider stub
  • register legacy enums listed in the provider's $register property

Legacy Enum Base Class

LaravelEnso\Enums\Services\Enum

Key methods:

  • constants(): array
  • get($key)
  • has($key): bool
  • keys()
  • values()
  • json(): string
  • array(): array
  • all(): array
  • object(): object
  • collection()
  • select()
  • localisation(bool $state = true): void

Native Enum Discovery

  • LaravelEnso\Enums\Services\Source
  • LaravelEnso\Enums\Services\Enums

Requirements for discovery:

  • enum must exist in an Enums folder
  • enum must implement LaravelEnso\Enums\Contracts\Frontend

Optional mapping:

  • implement LaravelEnso\Enums\Contracts\Mappable

Legacy Enum Registry

  • LaravelEnso\Enums\Services\LegacyEnums
  • LaravelEnso\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\Select
  • LaravelEnso\Enums\Traits\Search
  • LaravelEnso\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 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravel-enso/enums 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 55.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 44
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-27