gupalo / item-syncer
项目同步器
1.2.1
2023-05-28 15:40 UTC
Requires
- php: ^8.1
- doctrine/orm: ^2.14
- gupalo/dateutils: ^1.13
- phpspec/prophecy-phpunit: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
同步远程项目到本地项目。
安装
composer require gupalo/item-syncer
如何使用
创建两个实现 \Gupalo\ItemSyncer\SyncableEntityInterface
接口的数组
remoteItems
: 通常来自外部API - 真实来源localItems
: 来自您的数据库的项目
更新逻辑
- 本地缺少的远程项目 - 创建
- 本地存在的远程项目 - 更新(您实现逻辑以确定哪些属性应该更新)
- 远程缺少的本地项目 - 通过选择同步方法来决定
syncKeeping
: 不做任何操作syncArchiving
: 如果本地项目有archive
方法,则存档本地项目syncRemoving
: 删除本地项目
如果您使用 Doctrine 并将差异保存到数据库,则使用 DbItemSyncer
。如果您有自己的处理差异的逻辑,则使用 ItemSyncer 及其 diffKeeping
、diffArchiving
、diffRemoving
方法。
示例
$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]