jms/metadata
jms/metadata is a PHP library for managing class, method, and property metadata independent of its source (annotations, YAML/XML/PHP config, etc.). Extend base metadata classes, plug in drivers, and fetch metadata via MetadataFactory.
Start by installing the package via Composer:
composer require jms/metadata
The core abstraction is Metadata\MetadataFactory, which retrieves structured metadata for classes, methods, and properties. The first step is typically setting up a DriverChain to aggregate metadata sources—like annotations, YAML, XML, or PHP configuration files. For example, to begin reading from YAML files:
use Metadata\Driver\YamlDriver;
use Metadata\Driver\DriverChain;
use Metadata\MetadataFactory;
$chain = new DriverChain([
new YamlDriver('/path/to/metadata'),
]);
$factory = new MetadataFactory($chain);
$metadata = $factory->getMetadataForClass(MyClass::class);
Begin with simple use cases: inspecting class-level metadata (e.g., serialization groups, route options) or enriching DTOs with runtime-configurable behavior. Check the MetadataFactory return value—it will be a ClassMetadata instance holding method and property metadata arrays if defined.
Metadata is often used as a foundational layer for higher-level frameworks (e.g., Serializer, Doctrine, security annotations). Common patterns include:
AbstractFileDriver to load metadata from custom sources (e.g., environment variables, database records, or remote APIs). This is useful when metadata must be toggled without redeploying code.MetadataFactory in a caching or profiling decorator (e.g., using TraceableFileLocatorInterface for debugging file resolution).PropertyMetadata::getExtra() to attach arbitrary runtime data—like form field hints or validation rules—then inject those into UI builders or formatters.AnnotationDriver with YamlDriver for flexibility: use annotations for convenience and YAML for ops-friendly overrides.Integration with PSR-11 containers is seamless—pass your container to MetadataFactory via its constructor to resolve driver dependencies dynamically.
ReadOnlyCache. Failures often surface as cryptic rename() errors on Windows—upgrade to latest 2.x to benefit from robust fallback handling.NullMetadata is cached—ensure you’re not re-querying the same class name repeatedly in hot loops. Profile with TraceableFileLocatorInterface.MethodMetadata and PropertyMetadata lazily initialize their internal Reflection* objects. Avoid calling getReflection() unnecessarily—properties/methods already parsed in ClassMetadata are often enough.global scope, ensure your file locator can resolve them. See GitHub issue #5 for related fixes.PsrCacheAdapter. PHP 8.1+ compatibility is handled—don’t override setAccessible() manually.How can I help you explore Laravel packages today?