next-sentence / import-export-bundle
Sylius Resource 的导入/导出组件
Requires
- php: ^7.2 || ^8.0
- doctrine/orm: ^2.7
- portphp/portphp: ^1.2
- queue-interop/queue-interop: ^0.6.2|^0.7|^0.8
- sylius/grid-bundle: ^1.4
- sylius/resource-bundle: ^1.4
- sylius/ui-bundle: ^1.4
- symfony/stopwatch: ^3.3 | ^4.1 | ^5.0 | ^6.0
Requires (Dev)
- behat/behat: ^3.4
- behat/mink: ^1.7@dev
- behat/mink-browserkit-driver: ^1.3
- behat/mink-extension: ^2.2
- behat/mink-selenium2-driver: ^1.3
- enqueue/redis: ^0.8.23
- friends-of-behat/page-object-extension: ^0.3
- friends-of-behat/suite-settings-extension: ^1.0
- friends-of-behat/symfony-extension: ^2.0
- friends-of-behat/variadic-extension: ^1.1
- lakion/mink-debug-extension: ^1.2.3
- phpspec/phpspec: ^5.0
- phpstan/phpstan-doctrine: ^0.11.2
- phpstan/phpstan-shim: ^0.11.4
- phpstan/phpstan-symfony: ^0.11.2
- phpstan/phpstan-webmozart-assert: ^0.11.1
- phpunit/phpunit: ^6.5
- portphp/csv: ^1.1.0
- portphp/spreadsheet: ^1.0.0-alpha
- predis/predis: ^1.1
- sensiolabs/security-checker: ^5.0
- sylius-labs/coding-standard: ^2.0
- symfony/browser-kit: ^3.4|^4.1
- symfony/debug-bundle: ^3.4|^4.1
- symfony/dotenv: ^4.2
- symfony/intl: ^3.4|^4.1
- symfony/web-profiler-bundle: ^3.4|^4.1
- symfony/web-server-bundle: ^3.4|^4.1
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
- 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
This package is auto-updated.
Last update: 2024-09-08 07:07:16 UTC
README
安装
- 需要支持相关端口php格式
- 运行
composer require portphp/csv --no-update
以添加 CSV 格式支持 - 运行
composer require portphp/spreadsheet --no-update
以添加 Excel 格式支持(同时安装zip
PHP 扩展)
- 需要并安装插件
- 运行
composer require next-sentence/import-export-bundle
- 注册组件
<?php // config/bundles.php return [ // ... LWC\ImportExportBundle\LWCImportExportBundle::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/
。
命令行命令
-
获取可用导入器的列表
$ 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 中为通用导入器定义导入器服务
# 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: app.foo, format: csv }
或者实现自定义资源导入器 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: app.foo, format: bar }
添加资源处理器
在 services.yml 中定义带有通用资源处理器的处理器服务
# 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 文件,这将是在其第一行中定义的标题。如果使用通用资源处理器,这些标题键必须等于要导入的资源中的字段,因为这些键用于构建动态方法名。
或者实现自定义资源处理器 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 中定义您的资源导出器的插件池
# 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 资源的插件
# 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 的插件池
# 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 的插件
# 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']
插件池
插件池背后的想法是,能够拥有不同类型的插件,这些插件可能基于自定义的 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
-
使用之前下载的 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