juliangut/mapping
Composer 安装命令:
composer require juliangut/mapping
包简介
Base for mapping support
README 文档
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 from PHP's Attributes 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
Examples of packages fully implementing this library can be found at
Installation
Composer
composer require juliangut/mapping
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
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\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();
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
Attribute mapping
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();
Annotation mapping
Annotations are deprecated and discouraged. Use Attribute mapping instead
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();
Factory
Create your driver factory extending from Jgut\Mapping\Driver\AbstractDriverFactory, it allows to automatically get a mapping driver from mapping sources
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()); } }
Resolver
Given mapping source definitions, metadata resolver will resolve final metadata using a driver factory
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);
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
typeone of \Jgut\Mapping\Driver\DriverFactoryInterface constants:DRIVER_ATTRIBUTE,DRIVER_PHP,DRIVER_JSON,DRIVER_XML,DRIVER_YAMLorDRIVER_ANNOTATIONif no driver, defaults to DRIVER_ATTRIBUTEpatha string path or array of paths to where mapping files are located (files or directories) REQUIRED if no driverdriveran 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; /** * @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 defines which annotation property is considered the default ("value" by default). In this previous example event property will be set to "post_deserialize"
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.
juliangut/mapping 适用场景与选型建议
juliangut/mapping 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.08k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「annotations」 「yaml」 「php」 「json」 「xml」 「doctrine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juliangut/mapping 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juliangut/mapping 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juliangut/mapping 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A collection of helpers for PHPUnit to ease testing Tarantool libraries.
The last validation library you will ever need!
SUML for PHP
SUML support for Symfony
provides an facade for php-yaml, syck and symphony/yaml so switching in between is easier
Especificação do formato Cnab240 e Cnab400 traduzida para Yaml
统计信息
- 总下载量: 12.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-10-22