Download the PHP package zenstruck/class-metadata without Composer
On this page you can find all versions of the php package zenstruck/class-metadata. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zenstruck/class-metadata
More information about zenstruck/class-metadata
Files in zenstruck/class-metadata
Package class-metadata
Short Description Add human readable class aliases & metadata with efficient lookups.
License MIT
Homepage https://github.com/zenstruck/class-metadata
Informations about the package class-metadata
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 or composer.json configuration.
- Alias: short name for a class (could be used as an alternative to storing a FQCN in the database).
- 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.
Installation
Note: This package requires composer 2.4+.
composer require zenstruck/class-metadata
- When asked to enable the Composer plugin, choose
y
(yes).
Configuration
Metadata and aliases can be added to class' via attributes or
composer.json
config.
- Aliases must be a non-empty string.
- Only 1 alias allowed per class.
- Multiple classes cannot have the same alias.
- Metadata keys must be strings.
- 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
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
& Metadata
attributes. These are
parsed and a mapping file is generated.
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
:
You can disable path scanning entirely and only configure via your
composer.json
:
composer.json
Metadata and aliases can also be configured in your composer.json
. This allows
adding aliases and metadata to 3rd party classes:
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
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.
Alias Lookup
Metadata Lookup
list-class-metadata
Command
A custom Composer command is provided to debug/list all classes that have metadata/aliases configured:
Using the User example above, the following would be output:
Doctrine Bridge
AliasManagerRegistry
This is a decorated ManagerRegistry
that allows using aliases for the
getRepository()
and getManagerForClass()
methods:
All versions of class-metadata with dependencies
composer-plugin-api Version ^2.0