gupalo/item-syncer

项目同步器

1.2.1 2023-05-28 15:40 UTC

This package is auto-updated.

Last update: 2024-08-28 18:16:02 UTC


README

同步远程项目到本地项目。

安装

composer require gupalo/item-syncer

如何使用

创建两个实现 \Gupalo\ItemSyncer\SyncableEntityInterface 接口的数组

  • remoteItems: 通常来自外部API - 真实来源
  • localItems: 来自您的数据库的项目

更新逻辑

  • 本地缺少的远程项目 - 创建
  • 本地存在的远程项目 - 更新(您实现逻辑以确定哪些属性应该更新)
  • 远程缺少的本地项目 - 通过选择同步方法来决定
    • syncKeeping: 不做任何操作
    • syncArchiving: 如果本地项目有 archive 方法,则存档本地项目
    • syncRemoving: 删除本地项目

如果您使用 Doctrine 并将差异保存到数据库,则使用 DbItemSyncer。如果您有自己的处理差异的逻辑,则使用 ItemSyncer 及其 diffKeepingdiffArchivingdiffRemoving 方法。

示例

$remoteItems = array_map(
    static fn(array $a) => Country::createFromApi($a),
    $this->countryApiClient->getCountries()
);
$localItems = $this->countryRepository->findAll();
$diff = $this->dbItemSyncer->syncArchiving($remoteItems, $localItems);

print_r($diff->stat()); // something like ['created' => 2, 'updated' => 180]