承接 jawitold/enum-attribute-lookup-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jawitold/enum-attribute-lookup-bundle

最新稳定版本:1.0.1

Composer 安装命令:

composer require jawitold/enum-attribute-lookup-bundle

包简介

A Symfony bundle for automated discovery and runtime lookup of PHP 8.1+ Enum case attributes.

README 文档

README

This Symfony bundle provides a way to look up attributes on PHP enum cases at runtime. It gathers metadata from attributes that implement EnumCaseAttributeInterface and makes it accessible through a trait.

Key Features

  • Automated Metadata Collection: A compiler pass scans your project's src directory for enums using the AttributeLookupTrait.
  • Selective Attribution: Only attributes that implement EnumCaseAttributeInterface are gathered, preventing unwanted overhead.
  • Fast Runtime Lookups: Attributes are stored in a static registry, which is initialized once when the Symfony kernel boots.

Installation

Add the bundle to your Symfony project via Composer:

composer require jawitold/enum-attribute-lookup-bundle

The bundle should be automatically registered by Symfony Flex. If not, add it to your config/bundles.php:

return [
    // ...
    JaWitold\EnumAttributeLookupBundle\JaWitoldEnumAttributeLookupBundle::class => ['all' => true],
];

Usage

1. Create a Metadata Attribute

Define an attribute class that implements EnumCaseAttributeInterface:

namespace App\Attribute;

use JaWitold\EnumAttributeLookupBundle\Contract\EnumCaseAttributeInterface;

#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class RoleMetadata implements EnumCaseAttributeInterface
{
    public function __construct(
        public string $label,
        public string $icon = 'user',
    ) {}
}

2. Apply to an Enum

Apply the attribute to your enum cases and use the AttributeLookupTrait:

namespace App\Enum;

use App\Attribute\RoleMetadata;
use JaWitold\EnumAttributeLookupBundle\AttributeLookupTrait;use JaWitold\EnumAttributeLookupBundle\AttributeLookupTrait;

enum UserRole: string
{
    use AttributeLookupTrait;

    #[RoleMetadata(label: 'Administrator', icon: 'shield')]
    case Admin = 'admin';

    #[RoleMetadata(label: 'Standard User')]
    case User = 'user';
}

3. Query the Enum

Use the methods provided by AttributeLookupTrait to retrieve cases or their metadata.

Get Cases by Attribute Class

// Returns a list of cases that have a RoleMetadata attribute
$cases = UserRole::getCasesByAttribute(RoleMetadata::class);

Get Attributes for a Specific Case

// Returns a list of RoleMetadata objects applied to the Admin case
$attributes = UserRole::getAttributes(UserRole::Admin, fn ($attr) => $attr instanceof RoleMetadata);
$label = $attributes[0]->label; // 'Administrator'

Custom Case Filtering

// Returns cases that have a RoleMetadata attribute with a specific label
$cases = UserRole::getCases(fn (object $attr) => $attr instanceof RoleMetadata && $attr->label === 'Administrator');

How It Works

  1. Compilation: The EnumAttributesCompilerPass scans all PHP files in your src/ directory.
  2. Identification: It identifies enums using AttributeLookupTrait.
  3. Extraction: For each identified enum, it extracts attributes implementing EnumCaseAttributeInterface from each case.
  4. Service Registration: Each unique attribute is registered as a service in the container, ensuring that dependencies can be injected if needed (though typically they are just data objects).
  5. Initialization: The EnumRegistry is tagged as a kernel.boot listener and is initialized with the map of enums and their attributes once at runtime.

Requirements

  • PHP 8.2 or higher
  • Symfony 7.0 or higher

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固