vespolina/syncer

Vespolina 同步库,用于与远程服务同步实体

dev-master 2013-11-25 19:22 UTC

This package is auto-updated.

Last update: 2024-09-18 20:20:38 UTC


README

Build Status

此库是Vespolina电子商务框架的一部分,并受MIT许可协议的约束。

描述

此库处理从远程服务到本地应用程序的实体(例如产品、订单、发票、内容)的同步。它还允许同步相关实体。例如,为了检索发票,您还需要远程客户信息和相关产品。

可以持久化部分检索到的实体和相关实体到网关,以便在任何时候停止处理并稍后恢复。

要求

文档

示例用法

// Create a new manager capable of persisting data in memory and default configuration
$syncManager = new SyncManager(new SyncMemoryGateway(), new EventDispatcher(), $this->logger);

// Instantiate your own service adapter, for example for the ZOHO api
$zohoServiceAdapter = new ZohoServiceAdapter($this->config, $this->logger);,

// Register the service adapter. The service adapter will indicate it supports the 'invoice' entity
$syncManager->addServiceAdapter($zohoServiceAdapter);

// Register a local object manager to retrieve local customer instances from the database
$syncManager->addLocalEntityRetriever('customer', $customerManager, 'findById');

// Start synchronisation for entity name 'invoice'
$syncManager->execute(array('invoice'));

服务适配器需要实现抽象方法 fetchEntitiesfetchEntitytransformEntityData

fetchEntities 从远程服务下载实体并为每个远程实体创建一个EntityData实例。此实例包含实体的名称(例如,'invoice')、远程标识(例如,zoho id '234324')和原始实体信息(例如,xml或json数据)。

当同步管理器检测到依赖关系时,它将首先检查该依赖关系是否已存在于本地应用程序中。如果不是这种情况,则将使用为相关远程实体配置的服务适配器来检索和创建本地实体。例如,zoho发票实体需要zoho客户实体。因此,首先需要检索远程客户信息,并创建和持久化本地客户实例。然后才能创建发票。

系统可以在检测到依赖关系时直接解决它,或者首先将主实体的部分数据(例如,'invoice')存储起来。

立即解决依赖关系(默认)

  1. 对于每个远程发票
  2. 在EntityData中注册部分发票数据
  3. 解决依赖的远程实体'客户' 12313
  4. 解决依赖的远程实体'产品' 222和'产品' 333
  5. 在本地创建'客户'和'产品'实体后,使用它们创建发票实体
  6. 结束每个远程发票

延迟解决依赖关系(将delay_dependency_processing设置为true)

  1. 对于每个远程发票
  2. 在EntityData中注册部分发票数据
  3. 结束每个远程发票
  4. 对于每个未解决的依赖关系
  5. 解决依赖关系
  6. 结束每个未解决的依赖关系
  7. 检索部分实体数据发票
  8. 转换为真实发票
  9. 在本地创建'客户'和'产品'实体后,使用它们创建发票实体

所有依赖关系都已解决,使用服务适配器的transformEntityData方法创建请求的实体(例如,'发票')。

您也可以向管理器提供配置选项来定义您的同步

$yamlParser = new Parser();
$config = $yamlParser->parse(file_get_contents(__DIR__ . '/config.yml'));
$this->manager = new SyncManager($gateway, $dispatcher, $logger, $config['syncer']);

并且您的同步配置文件可能看起来像这样

syncer:
    direction: download
    use_id_mapping: true
    delay_dependency_processing: false
    entities:
        customer:
            strategy:  changed_at
        product:
            strategy: incremental_id
        invoice:
            strategy: incremental_id
            dependencies:
                - customer
                - product
    remotes:
        demo_system_1:
            adapter: Vespolina\Sync\Adapter\RemoteAdapter

如果您正在使用Symfony,您可以检查Syncer Bundle

有关安装指南和参考,请参阅

贡献

欢迎提交拉取请求。请参阅我们的CONTRIBUTING指南。

为此库存在单元测试和/或功能测试。请参阅[测试文档]((http://vespolina.org/contributing/testing)),了解运行测试的指南。

感谢所有已经做出贡献的人