juliangut / mapping
Base for mapping support with Doctrine annotations
Installs: 1 906
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.1
- psr/simple-cache: ^1.0
Requires (Dev)
- brainmaestro/composer-git-hooks: ^2.8
- doctrine/annotations: ^1.4
- friendsofphp/php-cs-fixer: ^2.16
- infection/infection: ^0.13|^0.15
- phpmd/phpmd: ^2.0
- phpstan/extension-installer: ^1.0.3
- phpstan/phpstan: ^0.12
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^7.5|^8.0
- povils/phpmnd: ^2.0
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^4.0
- squizlabs/php_codesniffer: ^3.0
- symfony/yaml: ^3.1
- thecodingmachine/phpstan-strict-rules: ^0.12
Suggests
- doctrine/annotations: (^1.4) In order to load mappings from class annotations
- symfony/yaml: (^3.1) In order to load mappings from YAML files
README
Mapping
Base mapping parsing library for any kind of project or library.
This library frees you from the most tedious part of mapping parsing by providing a set of functionalities to easily load mappings either from Doctrine annotations or files of different formats (PHP, JSON, XML, YAML), so you can focus on the actual parsing of mappings into metadata you can use onwards.
Examples
Full implementation examples of this library can be found at
Installation
Composer
composer require juliangut/mapping
To use mappings in class annotations
composer require doctrine/annotations
To use yaml files mappings
composer require symfony/yaml
Usage
Require composer autoload file
require './vendor/autoload.php';
Drivers
Should retrieve parsed metadata stored in a specific format
Annotation mapping
use Doctrine\Common\Annotations\AnnotationReader; use Jgut\Mapping\Driver\AbstractAnnotationDriver; class AnnotationDriver extends AbstractAnnotationDriver { /** * @return \Jgut\Mapping\Metadata\MetadataInterface[] */ public function getMetadata(): array { $mappingClasses = $this->getMappingClasses(); // Annotation reader available on $this->annotationReader // Return your parsed metadata } } $driver = new AnnotationDriver(['path/to/classes'], new AnnotationReader()); $driver->getMetadata();
File mapping
Any kind of format that can be returned on an array can be used
use Jgut\Mapping\Driver\AbstractMappingDriver; use Jgut\Mapping\Driver\Traits\PhpMappingTrait; use Jgut\Mapping\Driver\DriverInterface; class PhpDriver extends AbstractMappingDriver { use PhpMappingTrait; /** * @return \Jgut\Mapping\Metadata\MetadataInterface[] */ public function getMetadata(): array { $mappingData = $this->getMappingData(); // Return your parsed metadata } } $driver = new MappingDriver(['path/to/classes'], DriverInterface::DRIVER_PHP); $driver->getMetadata();
There are mapping traits to easily support four types of mapping files:
- DriverInterface::DRIVER_PHP => Jgut\Mapping\Driver\Traits\PhpMappingTrait
- DriverInterface::DRIVER_JSON => Jgut\Mapping\Driver\Traits\JsonMappingTrait
- DriverInterface::DRIVER_XML => Jgut\Mapping\Driver\Traits\XmlMappingTrait
- DriverInterface::DRIVER_YAML => Jgut\Mapping\Driver\Traits\YamlMappingTrait
Factory
Create your driver factory extending from Jgut\Mapping\Driver\AbstractDriverFactory, it allows to automatically get a mapping driver from mapping sources
use Jgut\Mapping\Driver\AbstractDriverFactory; use Jgut\Mapping\Driver\DriverFactoryInterface; use Jgut\Mapping\Driver\DriverInterface; class DriverFactory extends AbstractDriverFactory { protected function getAnnotationDriver(array $paths): DriverInterface { return new AnnotationDriver($paths); } protected function getPhpDriver(array $paths): DriverInterface { return new PhpDriver($paths); } // ... }
Resolver
Given mapping source definitions, metadata resolver will resolve final metadata using a driver factory
use Jgut\Mapping\Metadata\MetadataResolver; $mappingSources = [ [ 'type' => DriverFactoryInterface::DRIVER_ANNOTATION, 'path' => '/path/to/mapping/files', ] ]; $metadataResolver = new MetadataResolver(new DriverFactory(), new PSR16Cache()); $metadata = $metadataResolver->getMetadata($mappingSources);
It's not mandatory but highly recommended to add a PSR-16 cache implementation to metadata resolver, collecting mapping data from annotations and/or files and transforming them into metadata objects can be an intensive operation that benefits vastly of caching
Mapping source
Define where your mapping data is and how it will be parsed
type
one of \Jgut\Mapping\Driver\DriverFactoryInterface constants:DRIVER_ANNOTATION
,DRIVER_PHP
,DRIVER_JSON
,DRIVER_XML
orDRIVER_YAML
defaults to DRIVER_ANNOTATION if no driverpath
a string path or array of paths to where mapping files are located (files or directories) REQUIRED if no driverdriver
an already created \Jgut\Mapping\Driver\DriverInterface object REQUIRED if no type AND path
Annotations
Base AbstractAnnotation class is provided to ease annotations creation.
use Jgut\Mapping\Annotation\AbstractAnnotation; /** * Custom annotation. * * @Annotation * @Target("PROPERTY") */ class Custom extends AbstractAnnotation { }
Contributing
Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.
See file CONTRIBUTING.md
License
See file LICENSE included with the source code for a copy of the license terms.