wwwision/import-service

通用的服务,用于将来自不同来源的数据导入到可配置的目标,例如Neos内容库或任意数据库表

3.0.1 2024-06-27 09:56 UTC

This package is auto-updated.

Last update: 2024-08-27 10:16:10 UTC


README

Neos Flow包,用于将来自不同来源的数据导入到可配置的目标,例如Neos内容库或任意数据库表

用法

设置

使用composer安装此包:

composer require wwwision/import-service

定义导入预设

将导入预设配置添加到项目的Settings.yaml文件中,例如

Wwwision:
  ImportService:
    presets:

      'some-prefix:some-name':
        source:
          factory: 'Wwwision\ImportService\DataSource\Http\HttpSourceFactory'
          options:
            endpoint: 'https://some-endpoint.tld/data.json'
        target:
          factory: 'Wwwision\ImportService\DataTarget\Dbal\DbalSourceFactory'
          options:
            table: 'some_table'
        mapping:
          'id': 'id'
          'given_name': 'firstName'
          'family_name': 'lastName'

运行导入

./flow import:run some-prefix:some-name

预处理数据

有时数据在映射之前需要被处理。这可以通过一个dataProcessor来完成。

示例

实现

处理器是任何可以被Flow实例化(无需额外参数)的类的任何公共方法

<?php
declare(strict_types=1);
namespace Some\Package;

use Wwwision\ImportService\ValueObject\DataRecord;
use Wwwision\ImportService\ValueObject\DataRecords;

final class SomeProcessor
{

    public function someMethod(DataRecords $records): DataRecords
    {
        return $records->map(static fn (DataRecord $record) => $record->withAttribute('title', 'overridden'));
    }
}

注意:处理器类可以有依赖项,但应该可以通过ObjectMananger::get($processorClassname)创建一个新的实例,即无需进一步参数,即类应像单例一样行为(见https://flowframework.readthedocs.io/en/stable/TheDefinitiveGuide/PartIII/ObjectManagement.html

配置

Wwwision:
  ImportService:
    presets:
      '<preset-name>':
        # ...
        options:
          dataProcessor: 'Some\Package\SomeProcessor::someMethod'

注意:语法看起来像方法必须是静态的,但这并非如此。它只需要满足PHP的is_callable()函数

验证配置

此包的配置非常详细,因此容易出错。可以通过以下命令验证设置与模式是否匹配

./flow configuration:validate --type Settings --path Wwwision.ImportService

应产生以下输出

Validating Settings configuration on path Wwwision.ImportService

All Valid!

命令行界面

此包提供以下CLI命令

import:run

同步给定预设的数据(添加、更新、删除)

用法

./flow import:run [<options>] <preset>

参数

  --preset             The preset to use (see Wwwision.Import.presets setting)

选项

  --quiet                   If set, no output, apart from errors, will be displayed
  --force-updates           If set, all local records will be updated regardless of their version/timestamp. This is useful for node type changes that require new data to be fetched
  --from-fixture            If set, the data will be loaded from a local fixture file instead of the configured data source
  --override-source-options Allows to override default options for the data source via JSON, e.g. '{"endpoint":"https://some-custom.tld/endpoint"}'
  --override-target-options Allows to override default options for the data target via JSON, e.g. '{"endpoint":"https://some-custom.tld/endpoint"}'

import:prune

删除给定预设的所有数据

用法

./flow import:prune [<options>] <preset>

参数

  --preset             The preset to reset (see Wwwision.Import.presets setting)

选项

  --assume-yes         If set, "yes" will be assumed for the confirmation question

import:presets

列出所有配置的预设名称

用法

./flow import:presets

import:preset

显示给定预设的配置

用法

./flow import:preset <preset>

参数

  --preset             The preset to show configuration for (see Wwwision.Import.presets setting)

import:setup

设置指定预设配置的数据源和目标,并/或显示状态

用法

./flow import:setup <preset>

参数

  --preset             Name of the preset to set up

Neos内容库导入

将数据从第三方API导入到Neos内容库是该包的主要要求之一。

要将记录导入内容库,可以使用提供的ContentRepositoryTarget,它有以下选项

nodeType (string, required): Type of the imported nodes
rootNodePath: (string, optional): Absolute path of the root node underneath imported nodes will placed
parentNodeResolver: (callable string, optional): Callback in the format ('<FQCN>::<methodName>') that is invoked to resolve the parent node for a given record. The signature is: \Closure(\Wwwision\ImportService\ValueObject\DataRecord): \Neos\ContentRepository\Domain\Model\Node
nodeVariantsResolver: (callable string, optional): Callback in the format ('<FQCN>::<methodName>') that is invoked to resolve dimension variants for a given record. The signature is: \Closure(\Wwwision\ImportService\ValueObject\DataRecord): array of dimension values
idPrefix: (string, optional): Optional prefix that is prepended to the dataRecord IDs in order to get globally unique node identifiers
softDelete (boolean, optional): If set, removed records lead to the corresponding node to be _disabled_ only. Otherwise they are deleted from the Content Repository
rootNodeType: (string, optional): Type of the root node, imported nodes are placed in – if set and no root node can be found, the importer tries to create it via ./flow importer:setup

注意:必须指定rootNodePathparentNodeResolver

示例配置

Wwwision:
  ImportService:
    presets:

      'some:preset':
        source:
          # depending on the data source, see above
        target:
          factory: 'Wwwision\ImportService\DataTarget\ContentRepository\ContentRepositoryTargetFactory'
          options:
            nodeType: 'Some.Package:Type.Of.Imported.Nodes'
            rootNodePath: '/sites/some-site/some/path'
            # alternatively:
            # parentNodeResolver: Some`\Package\SomeSingleton::someMethod
            idPrefix: 'product-'
            softDelete: true
        mapping:
          'title': 'title' # just use the record title 1:1
          'price': '${record.priceNet + record.vat}' # arbitrary Eel expressions are supported
          'uriPathSegment': '${Some.Custom.Eelhelper(record.title + "-" + record.id)}' # ...including custom Eel helpers (registered via Neos.Fusion.defaultContext setting)

不使用Neos.ContentRepository包的使用

如果未安装Neos.ContentRepository包,Flow的代理类构建器会抛出UnknownObjectException。在Objects.yaml中禁用自动装配

Wwwision\ImportService\DataTarget\ContentRepositoryTarget:
autowiring: false