friendsofsylius / sylius-import-export-plugin
Sylius 的导入/导出插件。
Requires
- php: ^8.0
- portphp/portphp: ^1.2
- queue-interop/queue-interop: ^0.6.2 || ^0.7 || ^0.8
- sylius/sylius: ~1.11.0 || ~1.12.0 || ~1.13.0
- symfony/stopwatch: ^5.2 || ^6.0
Requires (Dev)
- behat/behat: ^3.7
- behat/mink-selenium2-driver: ^1.4
- enqueue/redis: ^0.8.23
- friends-of-behat/mink: ^1.8
- friends-of-behat/mink-browserkit-driver: ^1.4
- friends-of-behat/mink-debug-extension: ^2.0
- friends-of-behat/mink-extension: ^2.4
- friends-of-behat/page-object-extension: ^0.3
- friends-of-behat/suite-settings-extension: ^1.0
- friends-of-behat/symfony-extension: ^2.1
- friends-of-behat/variadic-extension: ^1.3
- phpspec/phpspec: ^7.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: 0.12.82
- phpstan/phpstan-doctrine: 0.12.33
- phpstan/phpstan-strict-rules: ^0.12.0
- phpstan/phpstan-webmozart-assert: 0.12.12
- phpunit/phpunit: ^9.5
- portphp/csv: ^1.1.0
- portphp/spreadsheet: ^1.0.0-alpha
- predis/predis: ^1.1
- sylius-labs/coding-standard: ^3.2 || ^4.0
- symfony/browser-kit: ^5.4 || ^6.0
- symfony/debug-bundle: ^5.4 || ^6.0
- symfony/dotenv: ^5.4 || ^6.0
- symfony/intl: ^5.4 || ^6.0
- symfony/mailer: ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^5.4 || ^6.0
- symfony/webpack-encore-bundle: ^1.15
Suggests
- enqueue/enqueue-bundle: To help defining message queuing services
- enqueue/redis: To support message queuing via redis
- enqueue/sqs: To support message queuing via sqs
- portphp/csv: To support importing csv files, use version ^1.1
- portphp/spreadsheet: To support importing Excel and LibreOffice Calc files, use version ^1.1
Conflicts
- sonata-project/core-bundle: >=3.12 <3.13.4
- sylius/mailer-bundle: ^1.8.0
- symfony/browser-kit: 4.1.8
- symfony/dependency-injection: 4.1.8
- symfony/dom-crawler: 4.1.8
- symfony/routing: 4.1.8
- symfony/symfony: 4.1.8
README
FOSSyliusImportExportPlugin
安装
- 需要相关端口php格式支持
- 运行
composer require portphp/csv --no-update
以添加 CSV 格式支持 - 运行
composer require portphp/spreadsheet --no-update
以添加 Excel 格式支持(同时安装zip
PHP 扩展)
- 需要并安装插件
- 运行
composer require friendsofsylius/sylius-import-export-plugin
- 注册包
<?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
开发
添加新的导入类型
注意
- 在以下示例中,将
app
和foo
替换为您想要实现的资源(在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场景)
-
使用之前下载的Chromedriver运行Selenium服务器
$ bin/selenium-server-standalone -Dwebdriver.chrome.driver=chromedriver
-
在
localhost:8080
上运行测试应用程序的web服务器$ (cd tests/Application && bin/console server:run 127.0.0.1:8080 -d web -e test)
-
运行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