lionelkouame / sylius-import-export-plugin
Sylius 的导入/导出插件。
Requires
- php: ^8.0.0
- portphp/portphp: ^1.2
- queue-interop/queue-interop: ^0.6.2 || ^0.7 || ^0.8
- sylius/sylius: ~1.8.0 || ~1.9.0 || ~1.10.0 || ~v1.11.0
- symfony/stopwatch: ^4.4 || ^5.2
Requires (Dev)
- behat/behat: ^3.6.1
- behat/mink: ^1.8
- behat/mink-selenium2-driver: ^1.3
- enqueue/redis: ^0.8.23
- friends-of-behat/mink-browserkit-driver: ^1.4
- 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.2
- friends-of-behat/variadic-extension: ^1.1
- friendsofsymfony/oauth-server-bundle: ^1.6 || >2.0.0-alpha.0 ^2.0@dev
- lakion/mink-debug-extension: ^1.2.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
- sensiolabs/security-checker: ^6.0
- sylius-labs/coding-standard: ^3.2
- symfony/browser-kit: ^4.4 || ^5.2
- symfony/debug-bundle: ^4.4 || ^5.2
- symfony/dotenv: ^4.4 || ^5.2
- symfony/intl: ^4.4 || ^5.2
- symfony/web-profiler-bundle: ^4.4 || ^5.2
- symfony/web-server-bundle: ^4.4 || ^5.2
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
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: app.foo, 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: app.foo, 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 文件,这将是在其第一行中定义的标题。如果使用通用 ResourceProcessor,这些 HeaderKeys 必须与资源中的字段相匹配,因为这些键用于构建动态方法名。
或者实现自定义 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 中定义您的 ResourceExporter(目前只支持 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 中定义您的 ResourceExporter 的 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 中定义您的 FooResource 的 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
-
使用之前下载的 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)
包含登录信息的 fixture 文件:https://github.com/Sylius/Sylius/blob/master/src/Sylius/Bundle/CoreBundle/Resources/config/app/fixtures.yml