friendsofsylius/sylius-import-export-plugin

Sylius 的导入/导出插件。

0.26.0 2024-04-26 18:14 UTC

README

License Version Build

FOSSyliusImportExportPlugin

安装

  1. 需要相关端口php格式支持
  • 运行 composer require portphp/csv --no-update 以添加 CSV 格式支持
  • 运行 composer require portphp/spreadsheet --no-update 以添加 Excel 格式支持(同时安装 zip PHP 扩展)
  1. 需要并安装插件
  • 运行 composer require friendsofsylius/sylius-import-export-plugin
  1. 注册包
<?php

// config/bundles.php

return [
    // ...
    FriendsOfSylius\SyliusImportExportPlugin\FOSSyliusImportExportPlugin::class => ['all' => true],
];

配置

应用程序配置

# config/packages/fos_sylius_import_export.yaml

fos_sylius_import_export:
    importer:
        # set to false to not add an upload form to the entity overview pages
        web_ui:               true
        # set to an integer value bigger than 0 to flush the object manager in regular intervals
        batch_size:           0
        # if incomplete rows (ie. missing required fields) should be considered failures
        fail_on_incomplete:   false
        # if to stop the import process in case of a failure
        stop_on_failure:      false
    exporter:
      # set to false to not add export buttons
        web_ui:               true      

路由配置(仅当 web_ui 设置为 true 时必要)

# config/routes/fos_sylius_import_export.yaml

sylius_import_export:
    resource: "@FOSSyliusImportExportPlugin/Resources/config/routing.yml"
    prefix: /admin

消息队列配置

任何实现 "queue-interop/queue-interop" 的库都可以用作消息队列。以下是以 "enqueue/redis" 库为例的用法。

# config/services.yaml

# define a service which will be used as the queue
services:
    redis_connection_factory:
        class: Enqueue\Redis\RedisConnectionFactory
# config/packages/fos_sylius_import_export.yaml

# use the defined service
fos_sylius_import_export:
    message_queue:
        service_id: 'redis_connection_factory'

用法

可用的导入类型

  • 国家(csv、excel、json)
  • 客户组(csv、excel、json)
  • 支付方式(csv、excel、json)
  • 税收类别(csv、excel、json)
  • 客户(json)
  • 产品(csv)

可用的导出类型

  • 国家(csv、excel、json)
  • 订单(csv、excel、json)
  • 客户(csv、excel、json)
  • 产品(csv)

示例导入文件

请参阅 Behat 测试中的 fixtures:tests/Behat/Resources/fixtures

用户界面

对于所有可用的导入器,使用事件钩子系统自动将上传文件的表单注入到相关的管理员概览面板中,即 admin/tax-categories/

CLI 命令

  • 获取可用的导入器列表

    $ bin/console sylius:import
    
  • 使用 tax_category 导入器导入文件

    $ bin/console sylius:import tax_category my/tax/categories/csv/file.csv --format=csv
    
  • 使用 country 导入器从消息队列中导入

    $ bin/console sylius:import-from-message-queue country
    
  • 使导入器等待 1 秒以获取消息进入消息队列(默认,不等待)

    $ bin/console sylius:import-from-message-queue country --timeout=1000
    
  • 使用 country 导出器将资源数据导出到文件

    $ bin/console sylius:export country my/countries/export/csv/file.csv --format=csv
    
  • 使用 country 导出器将资源数据导出到消息队列

    $ bin/console sylius:export-to-message-queue country
    

开发

添加新的导入类型

注意

  • 在以下示例中,将 appfoo 替换为您想要实现的资源(在 sylius_resource 配置下)的名称。
  • 在以下示例中,将 bar 替换为您想要在以下示例中实现格式的名称(csv、json、...)。
  • 请注意,当然也可以实现针对 app.foo 资源和格式 bar 的专用导入器,以防无法实现通用类型实现。

添加资源导入器

在 services_bar.yml 中为通用导入器定义导入器服务,并使用 ResourceImporter
# config/services.yaml

sylius.importer.foo.bar:
    class: FriendsOfSylius\SyliusImportExportPlugin\Importer\ResourceImporter
    arguments:
        - "@sylius.factory.bar_reader"
        - "@sylius.manager.foo"
        - "@sylius.processor.foo"
        - "@sylius.importer.result"
    tags:
        - { name: sylius.importer, type: foo, domain: app, format: csv }
或者实现自定义 ResourceImporter FooImporter
class FooImporter implements ImporterInterface
定义服务而不是上述提到的
# config/services.yaml

sylius.importer.foo.bar:
  class: App\FooImporter
  arguments:
      - "@sylius.factory.bar_reader"
      - "@sylius.manager.foo"
      - "@sylius.processor.foo"
      - "@sylius.importer.result"
  tags:
      - { name: sylius.importer, type: foo, domain: app, format: bar }

添加资源处理器

在 services.yml 中使用通用 ResourceProcessor 定义处理器服务
# config/services.yaml

sylius.processor.foo:
    class: FriendsOfSylius\SyliusImportExportPlugin\Processor\ResourceProcessor
    arguments:
        - "@app.factory.foo"
        - "@app.repository.foo"
        - "@property_accessor"
        - "@sylius.importer.metadata_validator"
        - "@doctrine.orm.entity_manager"
        - ["HeaderKey0", "HeaderKey1", "HeaderKey2"]

HeaderKey0 是将在数据库中搜索的键,以避免冗余。因此,最好将 HeaderKey0 设为一个唯一的键。

第四个参数表示要导入的数据的标题。对于 csv 文件,这将是其第一行中定义的标题。这些 HeaderKeys 必须等于要导入的资源中的字段,如果使用通用 ResourceProcessor,因为键用于构建动态方法名。

或者实现自定义 ResourceProcessor FooProcessor
class FooProcessor implements ResourceProcessorInterface
在services.yml中用FooProcessor定义处理器服务,而不是上述提到的通用服务
# config/services.yaml

 sylius.processor.tax_categories:
     class: FriendsOfSylius\SyliusImportExportPlugin\Processor\FooProcessor
     arguments:
         - "@app.factory.foo"
         - "@app.repository.foo"
         - "@sylius.importer.metadata_validator"
         - "@doctrine.orm.entity_manager"
         - ["HeaderKey0", "HeaderKey1", "HeaderKey2"]

验证元数据

每个处理器都定义了强制性的'HeaderKeys'。对于这些HeaderKeys的基本验证,您可以使用"@sylius.importer.metadata_validator"。当然,您也可以通过实现MetadataValidatorInterface并在FooProcessor中注入它来代替通用验证器,以实现您自己的验证器。

定义新的导出器

注意

  • 在以下示例中,将foo替换为您想要实现的类型的名称。
  • 在以下示例中,将bar替换为您想要实现的格式的名称。
  • 当然,如果通用类型实现不可行,您也可以为foo类型和格式bar实现一个专门的导出器。

导出器

添加资源导出器

在services_bar.yml中定义您的资源导出器(目前只支持csv格式导出)

# config/services.yaml

  sylius.exporter.foo.bar:
     class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\ResourceExporter
     arguments:
        - "@sylius.exporter.bar_writer"
        - "@sylius.exporter.pluginpool.foo"
        - ["HeaderKey0", "HeaderKey1" ,"HeaderKey2"]
        - "@sylius.exporters_transformer_pool" # Optional
     tags:
        - { name: sylius.exporter, type: app.foo, format: bar }

注意,app.foo是您命名的资源的别名

# config/packages/_sylius.yaml

sylius_resource:
    resources:
        app.foo:

在services.yml中定义您的资源导出器的PluginPool

# config/services.yaml

# PluginPools for Exporters. Can contain multiple Plugins
  sylius.exporter.pluginpool.foo:
      class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\PluginPool
      arguments:
          - ["@sylius.exporter.plugin.resource.foo"]
          - ["HeaderKey0", "HeaderKey1" ,"HeaderKey2"]

在services.yml中定义您的Foo资源的Plugin

# config/services.yaml

  # Plugins for Exporters
  sylius.exporter.plugin.resource.foo:
      class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\ResourcePlugin
      arguments:
          - "@sylius.repository.foo"
          - "@property_accessor"
          - "@doctrine.orm.entity_manager"

如果您想使用网格过滤器(在管理员中)来过滤输出,请添加到您的路由中

# config/services.yaml

app_export_data_foo:
    path: /admin/export/sylius.resource/{format}
    methods: [GET]
    defaults:
        resource: sylius.foo
        _controller: sylius.controller.export_data_foo:exportAction
        _sylius:
            filterable: true
            grid: sylius_admin_foo # Name of defined grid here

并将相关的控制器服务定义添加到您的services中

# config/services.yaml

sylius.controller.export_data_foo:
    public: true
    class: FriendsOfSylius\SyliusImportExportPlugin\Controller\ExportDataController
    arguments:
        - "@sylius.exporters_registry"
        - "@sylius.resource_controller.request_configuration_factory"
        - "@sylius.resource_controller.resources_collection_provider"
        - "@sylius.repository.foo"
        - "%sylius.resources%"
    tags: ['controller.service_arguments']

如果您没有添加它,UI导出器仍然可以工作。它们将简单地为导出加载该资源的所有数据(类似于CLI)。

一个实际的例子

在services_csv.yml中定义Countries-Exporter

# config/services.yaml

  sylius.exporter.countries.csv:
     class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\ResourceExporter
     arguments:
        - "@sylius.exporter.csv_writer"
        - "@sylius.exporter.pluginpool.countries"
        - ["Id", "Code" ,"Enabled"]
        - "@sylius.exporters_transformer_pool" # Optional
     tags:
        - { name: sylius.exporter, type: sylius.country, format: csv }

在services.yml中定义Countries-Exporter的PluginPool

# config/services.yaml

# PluginPools for Exporters. Can contain multiple Plugins
  sylius.exporter.pluginpool.countries:
      class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\PluginPool
      arguments:
          - ["@sylius.exporter.plugin.resource.country"]
          - ["Id", "Code" ,"Enabled"]

在services.yml中定义Country-Resource的Plugin

# config/services.yaml

  # Plugins for Exporters
  sylius.exporter.plugin.resource.country:
      class: FriendsOfSylius\SyliusImportExportPlugin\Exporter\Plugin\ResourcePlugin
      arguments:
          - "@sylius.repository.country"
          - "@property_accessor"
          - "@doctrine.orm.entity_manager"

导出器将立即作为命令行导出器可用。

$ bin/console sylius:export country my/countries/export/csv/file.csv --format=csv

可选地添加路由

# config/routes.yaml

app_export_data_country:
    path: /admin/export/sylius.country/{format}
    methods: [GET]
    defaults:
        resource: sylius.country
        _controller: sylius.controller.export_data_contry:exportAction
        _sylius:
            filterable: true
            grid: sylius_admin_country

并将相关的控制器服务定义添加到您的services中

# config/services.yaml

sylius.controller.export_data_country:
    public: true
    class: FriendsOfSylius\SyliusImportExportPlugin\Controller\ExportDataController
    arguments:
        - "@sylius.exporters_registry"
        - "@sylius.resource_controller.request_configuration_factory"
        - "@sylius.resource_controller.resources_collection_provider"
        - "@sylius.repository.country"
        - "%sylius.resources%"
    tags: ['controller.service_arguments']

PluginPool

插件池背后的想法是可以有不同的插件类型,这些插件可能基于自定义SQL查询,查询导出资源额外的数据,例如客户的首选品牌。目前只有'ResourcePlugin',它允许导出一个资源的所有数据。通过提供的键,您可以影响资源导出的哪些字段。

运行插件测试

  • 测试应用程序安装

    $ composer require sylius/sylius symfony/symfony
    $ (cd tests/Application && yarn install)
    $ (cd tests/Application && yarn run gulp)
    $ (cd tests/Application && bin/console assets:install web -e test)
    
    $ (cd tests/Application && bin/console doctrine:database:create -e test)
    $ (cd tests/Application && bin/console doctrine:schema:create -e test)
    
  • PHPUnit

    $ bin/phpunit
    
  • PHPSpec

    $ bin/phpspec run
    
  • Behat(非JS场景)

    $ bin/behat features --tags="~@javascript"
    
  • Behat(JS场景)

    1. 下载Chromedriver

    2. 使用之前下载的Chromedriver运行Selenium服务器

      $ bin/selenium-server-standalone -Dwebdriver.chrome.driver=chromedriver
      
    3. localhost:8080上运行测试应用程序的web服务器

      $ (cd tests/Application && bin/console server:run 127.0.0.1:8080 -d web -e test)
      
    4. 运行Behat

      $ bin/behat features --tags="@javascript"
      

使用您的插件打开Sylius

  • 使用test环境

    $ (cd tests/Application && bin/console sylius:fixtures:load -e test)
    $ (cd tests/Application && bin/console server:run -d web -e test)
    
  • 使用dev环境

    $ (cd tests/Application && bin/console sylius:fixtures:load -e dev)
    $ (cd tests/Application && bin/console server:run -d web -e dev)
    

包含登录信息的固定文件:https://github.com/Sylius/Sylius/blob/master/src/Sylius/Bundle/CoreBundle/Resources/config/app/fixtures.yml