Top Related Projects
Annotations Docblock Parser
Extracts information about PHP class' properties using metadata of popular sources
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
A PHP parser written in PHP
Quick Overview
Metadata is a PHP library that provides a unified way to read and write metadata for various objects. It offers a flexible and extensible system for handling metadata across different sources, such as annotations, YAML, XML, or PHP arrays. The library is designed to be lightweight and easy to integrate into existing projects.
Pros
- Flexible and extensible metadata handling system
- Support for multiple metadata sources (annotations, YAML, XML, PHP arrays)
- Easy integration with existing PHP projects
- Well-documented and maintained
Cons
- Requires additional setup and configuration for complex use cases
- Learning curve for advanced features and custom drivers
- Limited built-in drivers compared to some other metadata libraries
- May introduce overhead for simple metadata scenarios
Code Examples
- Reading metadata from a class:
use Metadata\MetadataFactory;
use Metadata\Driver\AnnotationDriver;
$factory = new MetadataFactory(new AnnotationDriver());
$metadata = $factory->getMetadataForClass(MyClass::class);
$propertyMetadata = $metadata->propertyMetadata['myProperty'];
echo $propertyMetadata->getType(); // Output: string
- Creating a custom metadata driver:
use Metadata\Driver\DriverInterface;
class CustomDriver implements DriverInterface
{
public function loadMetadataForClass(\ReflectionClass $class)
{
// Custom logic to load metadata
$classMetadata = new ClassMetadata($class->getName());
// Add property and method metadata
return $classMetadata;
}
}
- Using multiple drivers:
use Metadata\Driver\DriverChain;
$driver = new DriverChain([
new AnnotationDriver(),
new YamlDriver(),
new CustomDriver(),
]);
$factory = new MetadataFactory($driver);
Getting Started
To start using the Metadata library, follow these steps:
-
Install the library using Composer:
composer require jms/metadata
-
Create a metadata factory with your desired driver:
use Metadata\MetadataFactory; use Metadata\Driver\AnnotationDriver; $factory = new MetadataFactory(new AnnotationDriver());
-
Use the factory to read metadata for your classes:
$metadata = $factory->getMetadataForClass(MyClass::class);
-
Access the metadata properties and methods as needed in your application.
Competitor Comparisons
Annotations Docblock Parser
Pros of Annotations
- More widely adopted and integrated with popular PHP frameworks
- Extensive documentation and community support
- Simpler syntax for basic use cases
Cons of Annotations
- Performance overhead due to runtime parsing
- Requires additional configuration for caching in production
- Limited flexibility for complex metadata scenarios
Code Comparison
Annotations:
/**
* @Entity
* @Table(name="users")
*/
class User
{
/** @Id @Column(type="integer") */
private $id;
}
Metadata:
$metadata->addPropertyMetadata('id', [
'type' => 'integer',
'id' => true,
]);
$metadata->addClassMetadata(User::class, [
'entity' => true,
'table' => 'users',
]);
Summary
Annotations offers a more intuitive and widely supported approach for basic metadata definition, making it easier for developers familiar with popular frameworks. However, Metadata provides better performance and flexibility for complex scenarios, albeit with a steeper learning curve and less community support. The choice between the two depends on project requirements, performance needs, and developer preferences.
Extracts information about PHP class' properties using metadata of popular sources
Pros of Property-info
- Part of the Symfony ecosystem, offering better integration with other Symfony components
- More actively maintained with frequent updates and bug fixes
- Provides a broader range of property information extraction methods
Cons of Property-info
- More focused on property information extraction, less flexible for general metadata handling
- Potentially steeper learning curve for developers not familiar with Symfony
Code Comparison
Property-info:
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
$reflectionExtractor = new ReflectionExtractor();
$properties = $reflectionExtractor->getProperties(MyClass::class);
$type = $reflectionExtractor->getTypes(MyClass::class, 'propertyName');
Metadata:
use Metadata\MetadataFactory;
use Metadata\Driver\DriverChain;
$factory = new MetadataFactory(new DriverChain([$driver1, $driver2]));
$metadata = $factory->getMetadataForClass(MyClass::class);
$propertyMetadata = $metadata->propertyMetadata['propertyName'];
Property-info focuses on extracting specific property information, while Metadata provides a more general approach to handling metadata for classes and properties. Property-info is better suited for Symfony-based projects, while Metadata offers more flexibility for standalone use or integration with other frameworks.
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
Pros of Serializer
- Part of the Symfony ecosystem, offering better integration with other Symfony components
- More comprehensive serialization/deserialization features, including support for various formats (JSON, XML, YAML, etc.)
- Active development and regular updates
Cons of Serializer
- Larger footprint and potentially more complex for simple use cases
- Steeper learning curve for developers not familiar with Symfony
Code Comparison
Metadata:
use Metadata\Driver\DriverChain;
use Metadata\MetadataFactory;
$factory = new MetadataFactory(new DriverChain([
new AnnotationDriver(new AnnotationReader()),
new YamlDriver(new FileLocator([__DIR__])),
]));
Serializer:
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
$jsonContent = $serializer->serialize($data, 'json');
Summary
While Metadata focuses on providing a flexible metadata system, Serializer offers a more comprehensive solution for object serialization and deserialization. Serializer is better suited for complex applications within the Symfony ecosystem, while Metadata might be preferred for simpler, standalone metadata management tasks.
A PHP parser written in PHP
Pros of PHP-Parser
- More comprehensive parsing capabilities, allowing for full PHP code analysis and manipulation
- Actively maintained with frequent updates and improvements
- Extensive documentation and examples for various use cases
Cons of PHP-Parser
- Steeper learning curve due to its complexity and extensive feature set
- Potentially overkill for simpler metadata extraction tasks
- Higher resource consumption for parsing large codebases
Code Comparison
PHP-Parser:
use PhpParser\ParserFactory;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse('<?php echo "Hello World!";');
Metadata:
use Metadata\MetadataFactory;
$factory = new MetadataFactory();
$metadata = $factory->getMetadataForClass('MyClass');
Summary
PHP-Parser is a powerful tool for parsing and analyzing PHP code, offering extensive capabilities but with a steeper learning curve. Metadata focuses specifically on extracting metadata from classes, providing a simpler solution for this specific use case. Choose PHP-Parser for comprehensive code analysis and manipulation, or Metadata for straightforward class metadata extraction.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Metadata is a library for class/method/property metadata management in PHP
Master (2.x) | 1.x |
---|---|
Overview
This library provides some commonly needed base classes for managing metadata for classes, methods and properties. The metadata can come from many different sources (annotations, YAML/XML/PHP configuration files).
The metadata classes are used to abstract away that source and provide a common interface for all of them.
Usage
The library provides three classes that you can extend to add your application
specific properties, and flags: ClassMetadata
, MethodMetadata
, and
PropertyMetadata
After you have added, your properties in sub-classes, you also need to add
DriverInterface
implementations which know how to populate these classes
from the different metadata sources.
Finally, you can use the MetadataFactory
to retrieve the metadata::
<?php
use Metadata\MetadataFactory;
use Metadata\Driver\DriverChain;
$driver = new DriverChain(array(
/** Annotation, YAML, XML, PHP, ... drivers */
));
$factory = new MetadataFactory($driver);
$metadata = $factory->getMetadataForClass('MyNamespace\MyObject');
Top Related Projects
Annotations Docblock Parser
Extracts information about PHP class' properties using metadata of popular sources
Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
A PHP parser written in PHP
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot