PHPackages                             jawitold/enum-attribute-lookup-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jawitold/enum-attribute-lookup-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

jawitold/enum-attribute-lookup-bundle
=====================================

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

1.0.1(2mo ago)010↓90%MITPHPPHP &gt;=8.1

Since Mar 30Pushed 2mo agoCompare

[ Source](https://github.com/JaWitold/enum-attribute-lookup-bundle)[ Packagist](https://packagist.org/packages/jawitold/enum-attribute-lookup-bundle)[ RSS](/packages/jawitold-enum-attribute-lookup-bundle/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (10)Versions (3)Used By (0)

Enum Attribute Lookup Bundle
============================

[](#enum-attribute-lookup-bundle)

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
------------

[](#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
------------

[](#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
-----

[](#usage)

### 1. Create a Metadata Attribute

[](#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

[](#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

[](#3-query-the-enum)

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

#### Get Cases by Attribute Class

[](#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

[](#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

[](#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
------------

[](#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
------------

[](#requirements)

- PHP 8.2 or higher
- Symfony 7.0 or higher

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance86

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~23 days

Total

2

Last Release

67d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f1b988f57767688751592b78c64abb618d7339ab4a9e060c32b5d40c8e6d694?d=identicon)[JaWitold](/maintainers/JaWitold)

---

Top Contributors

[![JaWitold](https://avatars.githubusercontent.com/u/47141012?v=4)](https://github.com/JaWitold "JaWitold (3 commits)")

---

Tags

symfonybundleenummetadataattributeslookupphp8

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jawitold-enum-attribute-lookup-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/jawitold-enum-attribute-lookup-bundle/health.svg)](https://phpackages.com/packages/jawitold-enum-attribute-lookup-bundle)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
