PHPackages                             zenstruck/class-metadata - 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. zenstruck/class-metadata

ActiveComposer-plugin[Utility &amp; Helpers](/categories/utility)

zenstruck/class-metadata
========================

Add human readable class aliases &amp; metadata with efficient lookups.

v1.1.1(5mo ago)142.8k2[1 PRs](https://github.com/zenstruck/class-metadata/pulls)3MITPHPPHP &gt;=8.0CI passing

Since Nov 29Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/zenstruck/class-metadata)[ Packagist](https://packagist.org/packages/zenstruck/class-metadata)[ Docs](https://github.com/zenstruck/class-metadata)[ GitHub Sponsors](https://github.com/kbond)[ GitHub Sponsors](https://github.com/nikophil)[ RSS](/packages/zenstruck-class-metadata/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (4)Dependencies (6)Versions (5)Used By (3)

zenstruck/class-metadata
========================

[](#zenstruckclass-metadata)

[![CI](https://github.com/zenstruck/class-metadata/actions/workflows/ci.yml/badge.svg)](https://github.com/zenstruck/class-metadata/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/f279c00ab2f849928c234f6787ad293a06174139b9afa60417844c0e8a8ddd11/68747470733a2f2f636f6465636f762e696f2f67682f7a656e73747275636b2f636c6173732d6d657461646174612f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d4138424b434d49364b44)](https://codecov.io/gh/zenstruck/class-metadata)

Add human-readable class aliases and scalar metadata to your classes with an efficient runtime lookup. These can be added via [attributes](#attributes) or [composer.json configuration](#composerjson).

1. **Alias**: short name for a class (could be used as an alternative to storing a FQCN in the database).
2. **Metadata**: key-value map of scalar or array values specific to a class (could be used to mark a class as *trackable* in an auditing system).

This library provides a Composer plugin that hooks into Composer's dump-autoload event to generate a lookup map for use at [runtime](#runtime-api).

Installation
------------

[](#installation)

> **Note**: This package requires composer 2.4+.

1. `composer require zenstruck/class-metadata`
2. When asked to enable the Composer plugin, choose `y` (yes).

Configuration
-------------

[](#configuration)

Metadata and aliases can be added to class' via [attributes](#attributes) or [`composer.json`](#composerjson) config.

1. Aliases must be a non-empty string.
2. Only 1 alias allowed per class.
3. Multiple classes cannot have the same alias.
4. Metadata keys must be strings.
5. Metadata values must be scalar (`bool|float|int|string`).

> **Note**: During development, when adding/changing/removing aliases and metadata, you need to run `composer dump-autoload` for the changes to take effect.

### Attributes

[](#attributes)

When creating the autoload configuration for your application, the composer plugin scans your PSR-4 `autoload` path(s) (defined in your `composer.json`) to look for classes with the `Alias` &amp; `Metadata` attributes. These are parsed and a mapping file is generated.

```
namespace App\Entity;

use Zenstruck\Alias;
use Zenstruck\Metadata;

#[Alias('user')]
#[Metadata('track', true)]
#[Metadata('identifier', 'getId')]
class User
{
    // ...
}
```

#### Customize Paths

[](#customize-paths)

If you have a large code-base, scanning all the files could be time-consuming. You can configure specific paths to scan in your `composer.json`:

```
{
    "extra": {
        "class-metadata": {
            "paths": [
                "src/Domain/*/Entity"
            ]
        }
    }
}
```

You can disable path scanning entirely and only configure via your [`composer.json`](#composerjson):

```
{
    "extra": {
        "class-metadata": {
            "paths": false
        }
    }
}
```

### `composer.json`

[](#composerjson)

Metadata and aliases can also be configured in your `composer.json`. This allows adding aliases and metadata to 3rd party classes:

```
{
    "extra": {
        "class-metadata": {
            "map": {
                "Vendor\\Code\\Class1": "class1",
                "Vendor\\Code\\Class2": {
                    "alias": "class2",
                    "key1": "value",
                    "key2": 7
                }
            }
        }
    }
}
```

For the mapping, a string is a shortcut for an alias (`class1` in the example above). An array will be used as the metadata except the special `alias` key. This value will be used as the *alias* (`class2` in the example above).

Runtime API
-----------

[](#runtime-api)

Since the alias/metadata maps are created when Composer creates the autoload files, runtime lookups are fast - just fetching from generated PHP *constant* array's.

The following examples use the [user class shown above](#attributes).

### Alias Lookup

[](#alias-lookup)

```
use Zenstruck\Alias;

// alias for class lookup:
Alias::for(User::class); // "user" (string|null - the alias for User or null if none)
Alias::for(new User()); // "user" (alternatively, you can pass the object directly)

// class for alias lookup:
Alias::classFor('user'); // "App\Entity\User" (class-string|null - the FQCN whose alias is "user")
```

### Metadata Lookup

[](#metadata-lookup)

```
use Zenstruck\Metadata;

// metadata for a class
Metadata::for(User::class); // ['track' => true, 'identifier' => 'getId'] (array - metadata array for User or empty array if none)
Metadata::for(new User()); // ['track' => true, 'identifier' => 'getId'] (alternatively, you can pass the object directly)
Metadata::for('user'); // ['track' => true, 'identifier' => 'getId'] (alternatively, fetch metadata by a class' alias)

// metadata value for key
Metadata::get(User::class, 'track'); // true (scalar|null - the metadata value for "key" or null if none)
Metadata::get(new User(), 'track'); // true (alternatively, you can pass the object directly)
Metadata::get('user', 'track'); // true (alternatively, fetch metadata by a class' alias)

// "first" metadata value for list of keys
Metadata::first(User::class, 'audit_id', 'identifier', 'id'); // "getId" (scalar|null - first metadata value found for" keys" (left to right) or null if none)
Metadata::first(new User(), 'audit_id', 'identifier', 'id'); // "getId" (alternatively, you can pass the object directly)
Metadata::first('user', 'audit_id', 'identifier', 'id'); // "getId" (alternatively, fetch metadata by a class' alias)

// all classes with metadata key
Metadata::classesWith('identifier'); // ["App\Entity\User"] (class-string[] - FQCN's that have metadata with key "identifier")
```

`list-class-metadata` Command
-----------------------------

[](#list-class-metadata-command)

A custom Composer command is provided to debug/list all classes that have metadata/aliases configured:

```
composer list-class-metadata
```

Using the [User example above](#attributes), the following would be output:

```
 ----------------- ------- -------------------------------------
  Class             Alias   Metadata
 ----------------- ------- -------------------------------------
  App\Entity\User   user    {"track":true,"identifier":"getId"}
 ----------------- ------- -------------------------------------

```

Doctrine Bridge
---------------

[](#doctrine-bridge)

### `AliasManagerRegistry`

[](#aliasmanagerregistry)

This is a decorated `ManagerRegistry` that allows using aliases for the `getRepository()` and `getManagerForClass()` methods:

```
use Zenstruck\Metadata\Bridge\Doctrine\AliasManagerRegistry;

/** @var \Doctrine\Persistence\ManagerRegistry $inner */

$registry = new AliasManagerRegistry($inner);

$registry->getRepository('user'); // converts "user" alias to FQCN
$registry->getRepository(User::class); // can still use FQCN

$registry->getManagerForClass('user'); // converts "user" alias to FQCN
$registry->getManagerForClass(User::class); // can still use FQCN
```

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance78

Regular maintenance activity

Popularity30

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.7% 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 ~293 days

Total

5

Last Release

92d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/707369cc916e0ea1aacbf077dcba464f611cef879f024d8944311a54a15224b3?d=identicon)[kbond](/maintainers/kbond)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (42 commits)")[![tacman](https://avatars.githubusercontent.com/u/619585?v=4)](https://github.com/tacman "tacman (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zenstruck-class-metadata/health.svg)

```
[![Health](https://phpackages.com/badges/zenstruck-class-metadata/health.svg)](https://phpackages.com/packages/zenstruck-class-metadata)
```

###  Alternatives

[vaimo/composer-patches

Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and patch command for testing/troubleshooting added patches.

2994.3M16](/packages/vaimo-composer-patches)[mglaman/composer-drupal-lenient

1317.4M15](/packages/mglaman-composer-drupal-lenient)[drupal/core-composer-scaffold

A flexible Composer project scaffold builder.

5341.9M446](/packages/drupal-core-composer-scaffold)[drupal/core-project-message

Adds a message after Composer installation.

2122.6M172](/packages/drupal-core-project-message)[olvlvl/composer-attribute-collector

A convenient and near zero-cost way to retrieve targets of PHP 8 attributes

184108.8k8](/packages/olvlvl-composer-attribute-collector)[lullabot/drainpipe

An automated build tool to allow projects to have a set standardized operations scripts.

41716.4k2](/packages/lullabot-drainpipe)

PHPackages © 2026

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