juliangut / mapping
映射支持的基础
1.4.2
2023-10-03 17:56 UTC
Requires
- php: ^8.0
- psr/simple-cache: ^2.0|^3.0
Requires (Dev)
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- doctrine/annotations: ^2.0
- infection/infection: ~0.25|~0.27
- juliangut/easy-coding-standard-config: ^1.12
- juliangut/phpstan-config: ^1.1
- overtrue/phplint: ^4.0|^5.0|^6.0
- phpcompatibility/php-compatibility: ^9.3
- phpmd/phpmd: ^2.13
- phpunit/phpunit: ^9.6|^10.3
- povils/phpmnd: ^3.2
- roave/security-advisories: dev-master
- symfony/yaml: ^6.0
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
映射
为任何类型的项目或库提供基础映射解析库。
这个库通过提供一系列功能,使您能够轻松地从PHP的属性或不同格式的文件(PHP、JSON、XML、YAML)中加载映射,从而让您专注于将映射解析成可进一步使用的元数据。
示例
可以在此找到完全实现此库的包的示例
安装
Composer
composer require juliangut/mapping
使用yaml文件映射
composer require symfony/yaml
用法
要求composer自动加载文件
require './vendor/autoload.php';
驱动程序
应检索存储在特定格式中的解析元数据
文件映射
任何可以返回到数组中的格式都可以使用
use Jgut\Mapping\Driver\AbstractMappingDriver; use Jgut\Mapping\Driver\Traits\PhpMappingTrait; use Jgut\Mapping\Metadata\MetadataInterface; class PhpDriver extends AbstractMappingDriver { use PhpMappingTrait; public function getMetadata(): array { $mappingData = $this->getMappingData(); // Return your parsed metadata } } $driver = new PhpDriver(['path/to/classes']); $driver->getMetadata();
有映射特性以轻松支持四种类型的映射文件
- 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
属性映射
use Jgut\Mapping\Driver\AbstractClassDriver; use Jgut\Mapping\Metadata\MetadataInterface; class AttributeDriver extends AbstractClassDriver { public function getMetadata(): array { $mappingClasses = $this->getMappingClasses(); // Parse class attributes with PHP Reflection // Return your parsed metadata } } $driver = new AttributeDriver(['path/to/classes']); $driver->getMetadata();
注解映射
注解已被弃用并不建议使用。请使用属性映射代替
composer require doctrine/annotations
use Doctrine\Common\Annotations\AnnotationReader; use Jgut\Mapping\Driver\AbstractAnnotationDriver; use Jgut\Mapping\Metadata\MetadataInterface; class AnnotationDriver extends AbstractAnnotationDriver { public function getMetadata(): array { $mappingClasses = $this->getMappingClasses(); // Parse class annotations. Annotation reader available on $this->annotationReader // Return your parsed metadata } } $driver = new AnnotationDriver(['path/to/classes'], new AnnotationReader()); $driver->getMetadata();
工厂
通过从Jgut\Mapping\Driver\AbstractDriverFactory扩展创建您的驱动程序工厂,它允许自动从映射源获取映射驱动程序
use Doctrine\Common\Annotations\AnnotationReader; use Jgut\Mapping\Driver\AbstractDriverFactory; use Jgut\Mapping\Driver\DriverInterface; class DriverFactory extends AbstractDriverFactory { protected function getPhpDriver(array $paths): DriverInterface { return new PhpDriver($paths); } protected function getAttributeDriver(array $paths): DriverInterface { return new AttributeDriver($paths); } protected function getAnnotationDriver(array $paths): DriverInterface { return new AnnotationDriver($paths, new AnnotationReader()); } }
解析器
给定映射源定义,元数据解析器将使用驱动程序工厂解析最终元数据
use Jgut\Mapping\Driver\DriverFactoryInterface; use Jgut\Mapping\Metadata\MetadataResolver; $mappingSources = [ [ 'type' => DriverFactoryInterface::DRIVER_ATTRIBUTE, 'path' => '/path/to/mapping/files', ] ]; $metadataResolver = new MetadataResolver(new DriverFactory(), new PSR16Cache()); $metadata = $metadataResolver->getMetadata($mappingSources);
虽然不是必需的,但强烈建议添加PSR-16缓存实现到元数据解析器,从注解和/或文件中收集映射数据并将其转换为元数据对象可能是一项密集型操作,缓存可以带来很大好处
映射源
定义您的映射数据的位置以及如何解析它
type
\Jgut\Mapping\Driver\DriverFactoryInterface 常量之一:DRIVER_ATTRIBUTE
,DRIVER_PHP
,DRIVER_JSON
,DRIVER_XML
,DRIVER_YAML
或DRIVER_ANNOTATION
如果没有驱动程序,则默认为 DRIVER_ATTRIBUTEpath
字符串路径或映射文件所在的路径数组(文件或目录) 如果没有驱动程序则必须提供driver
已创建的 \Jgut\Mapping\Driver\DriverInterface 对象 如果没有类型和路径则必需
注解
提供了一个基础抽象注解类以简化注解创建。
use Jgut\Mapping\Annotation\AbstractAnnotation; /** * @Annotation * * @Target("CLASS") */ class Event extends AbstractAnnotation { protected string $event; protected bool $enabled; public function setEvent(string $event): void { $this->event = $event; } public function setEnabled(bool $enabled): void { $this->enabled = $enabled; } protected function getDefaultProperty(): ?string { return 'event'; } }
/** * @Event("post_deserialize", enabled=true) */ class Example { }
getDefaultParameter
定义哪个注解属性被视为默认值(默认为 "value")。在此之前的示例中,event
属性将被设置为 "post_deserialize"。
贡献
发现了一个错误或有功能请求? 请打开一个新问题。在使用之前先查看现有的问题。
请参阅文件 CONTRIBUTING.md
许可证
有关许可证条款的副本,请参阅包含源代码的文件 LICENSE