webfactory/content-mapping

内容映射的迷你框架,用于将源系统内容映射到目标系统,例如从Propel对象映射到Solr或从Doctrine实体映射到ElasticSearch。

3.6.1 2024-09-04 13:53 UTC

README

content-mapping 是一个迷你框架,用于将源系统内容映射到目标系统。例如,从Propel对象映射到Solr,从Doctrine实体映射到ElasticSearch或从一个XML结构映射到另一个。它提供接口引导您进行映射,并提供一些抽象实现助手或魔法实现。

在简单情况下,您可能想考虑其他库。例如,如果您在Symfony2项目中想要将Doctrine实体索引到Solr服务器,您可能想考虑优秀的 floriansemm/solr-bundle。但是,这些和其他库在更复杂的情况中存在不足,例如当您想要索引相关实体的属性时。这就是为什么我们向您介绍了一种更通用的内容映射方法。

安装

通过运行命令添加内容映射依赖项(参见 https://getcomposer.org.cn/

php composer.phar require webfactory/content-mapping

并运行

php composer.phar install

概念

Class diagram

内容映射过程基于四个部分:SynchronizerSourceAdapterMapperDestinationAdapter。入口点是 Synchronizer->synchronize():在那里,Synchronizer 从 SourceAdapter->getObjectsOrderedById() 获取一个迭代器,以及从 DestinationAdapter->getObjectsOrderedById() 获取一个迭代器,并比较每个迭代器中的对象。在比较过程中,它会删除过时的对象(DestinationAdapter->delete()),存储新对象(DestinationAdapter->createObject())并在目标系统中更新现有对象(Mapper->map())。

DestinationAdapter->updated()DestinationAdapter->commit() 仅是外部更改跟踪的钩子,表示对象已被更新或两个迭代器都已处理,即可以持久化更改。

使用

要构建一个 Synchronizer,您需要为 SourceAdaperMapperDestinationAdapter 实现提供实现。请在 webfactory/content-mapping-* 软件包中找到抽象模板以及源适配器和目标适配器的通用实现。通常,Mapper 对于您的项目非常具体,因此您可能需要在您的应用程序中实现它。

use Webfactory\ContentMapping\Synchronizer;
use Webfactory\ContentMapping\SourceAdapter\Propel\GenericPropelSourceAdapter;

$sourceAdapter = ...; // see the readme of the corresponding package on how to construct it
$mapper = ...; // construct your own implementation
$destinationAdapter = ...; // see the readme of the corresponding package on how to construct it
$logger = ...; // just a PSR-3 logger

$classNameToSynchronize = 'MyClass';
$force = false; // if true, objects in destination system will be updated even if no changes are detected

$synchronizer = new Synchronizer($sourceAdapter, $mapper, $destinationAdapter, $logger);
$synchronizer->synchronize($classNameToSynchronize, $force);

致谢、版权和许可证

该项目始于webfactory GmbH,波恩。

版权所有 2015-2022 webfactory GmbH,波恩。代码在 MIT 许可证 下发布。