jbdevlabs / sylius-cli-context-plugin
轻松管理 CLI 命令上的 Sylius Context 并保持性能
Requires
- php: >=8.1
- sylius/sylius: ^1.11
Requires (Dev)
- behat/behat: ^3.6.1
- behat/mink-selenium2-driver: ^1.4
- dbrekelmans/bdi: ^1.1
- dmore/behat-chrome-extension: ^1.3
- dmore/chrome-mink-driver: ^2.7
- 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
- friendsofsymfony/oauth-server-bundle: ^1.6 || >2.0.0-alpha.0 ^2.0@dev
- phpspec/phpspec: ^7.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^1.8.4
- phpstan/phpstan-doctrine: ^1.3.12
- phpstan/phpstan-symfony: 1.2.9
- phpunit/phpunit: ^9.5
- polishsymfonycommunity/symfony-mocker-container: ^1.0
- sylius-labs/coding-standard: ^4.0
- symfony/browser-kit: ^4.4 || ^5.4 || ^6.0
- symfony/debug-bundle: ^4.4 || ^5.4 || ^6.0
- symfony/dotenv: ^4.4 || ^5.4 || ^6.0
- symfony/intl: ^4.4 || ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^4.4 || ^5.4 || ^6.0
- symfony/webpack-encore-bundle: ^1.12
- vimeo/psalm: 4.7.1||^5.0
README
此插件为您的 Symfony 命令提供默认通道上下文。
当 Sylius 加载实现 Sylius\Component\Resource\Model\TranslatableInterface
的资源时,一个 DoctrinepostLoad
事件监听器将默认本地和当前本地设置到加载的对象中。当前本地由当前 HTTP 请求和 ChannelContext
确定。
在 HTTP 上下文中,ChannelContext
中的当前通道由主机名定义。
在控制台上下文(CLI)中,我们没有 HTTP 请求,并且 ChannelContext
是空的。当您加载可翻译资源时,通道上下文将在对 ChannelRepository
运行 findAll
函数后加载第一个通道。
但是,ChannelContext
将为每个加载的资源执行 findAll
函数。您加载的资源越多,您的命令消耗的内存和使用的 CPU 资源就越多。
已知问题:CLI 命令中的性能问题:每个可翻译实体 postLoad 事件后的通道数据库请求 和 使用 php-cli 的多个通道是不可能的
此 Sylius 插件允许您在 ConsoleCommandEvent
事件上加载通道到 ChannelContext
。ChannelRepository
上的 findAll
函数将永远不会由 ChannelContext
执行,并且您将保持性能。
安装
运行命令 composer require jbdevlabs/sylius-cli-context-plugin dev-main
扩展 Sylius 通道存储库
注意:如果您已经扩展了 ChannelRepository,则实现接口
JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface
并使用特性JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderTrait
来实现接口。转到别名配置步骤。
实现和使用特性
在 src/Repository
中创建一个名为 ChannelRepository
的新 PHP 类,如下所示
<?php declare(strict_types=1); namespace App\Repository; use JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface; use JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderTrait; use Sylius\Bundle\ChannelBundle\Doctrine\ORM\ChannelRepository as BaseChannelRepository; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; class ChannelRepository extends BaseChannelRepository implements CliChannelProviderInterface, ChannelRepositoryInterface { use CliChannelProviderTrait; }
配置新存储库
在 config/packages/_sylius.yaml
文件中声明此存储库,如下所示
sylius_channel: resources: channel: classes: repository: App\Repository\ChannelRepository
修复接口别名
将 JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface
的别名声明为通道存储库。
services: JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface: '@sylius.repository.channel'
配置
在 config/packages
中添加一个名为 jb_dev_labs_sylius_cli_context.yaml
的文件并设置您的配置。
jb_dev_labs_sylius_cli_context: # Sylius channel code of default channel unsed for all Command. If not set, get the first channel. channel_code: null # Set command PHP Class namespace to load Sylius Context include_command: []
在您的命令中使用
默认情况下,不加载通道上下文。
要自动为您的命令加载通道上下文,请在您的命令上实现接口 JbDevLabs\SyliusCliContextPlugin\Command\CliContextAwareInterface
。
示例
<?php declare(strict_types=1); namespace App\Command; use JbDevLabs\SyliusCliContextPlugin\Command\CliContextAwareInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; final class CommandWithContext extends Command implements CliContextAwareInterface { protected function execute(InputInterface $input, OutputInterface $output) { //Your code return self::SUCCESS; } }
在捆绑命令中使用
您想在执行其他插件提供的命令时加载 Sylius 通道上下文吗?
将命令类的命名空间添加到您的配置中,以启用上下文初始化。
jb_dev_labs_sylius_cli_context: # Set command PHP Class namespace to load Sylius Context include_command: - Vendor\SyliusVendorPlugin\Command\StufCommand
程序化使用
请参阅 文档
贡献者安装(Docker)
-
克隆此项目
-
从插件骨架根目录,运行以下命令
$ chmod -Rf 777 tests/Application/var $ docker-compose up -d $ docker-compose exec php php -d memory_limit=-1 /usr/bin/composer install $ docker-compose exec nodejs yarn --cwd tests/Application install $ docker-compose exec php tests/Application/bin/console doctrine:database:create --if-not-exists -vvv $ docker-compose exec php tests/Application/bin/console doctrine:schema:create -vvv $ docker-compose exec php tests/Application/bin/console assets:install tests/Application/public -vvv $ docker-compose exec nodejs yarn --cwd tests/Application build $ docker-compose exec php tests/Application/bin/console cache:warmup -vvv $ docker-compose exec php tests/Application/bin/console sylius:fixtures:load -n
质量工具
$ docker-compose exec php composer validate --ansi --strict $ docker-compose exec php vendor/bin/phpstan analyse -c phpstan.neon -l max src/ $ docker-compose exec php vendor/bin/psalm $ docker-compose exec php vendor/bin/phpspec run --ansi -f progress --no-interaction $ docker-compose exec php vendor/bin/phpunit --colors=always $ docker-compose exec php vendor/bin/behat --profile docker --colors --strict -vvv --no-interaction
ProTip 使用 Makefile
;)
贡献者快速安装(旧版)
-
克隆此项目
-
从插件骨架根目录,运行以下命令
$ (cd tests/Application && yarn install) $ (cd tests/Application && yarn build) $ (cd tests/Application && APP_ENV=test bin/console assets:install public) $ (cd tests/Application && APP_ENV=test bin/console doctrine:database:create) $ (cd tests/Application && APP_ENV=test bin/console doctrine:schema:create)
为了能够设置插件数据库,请记住在 tests/Application/.env
和 tests/Application/.env.test
中配置您的数据库凭据。
用法
运行插件测试
-
PHPUnit
vendor/bin/phpunit
-
PHPSpec
vendor/bin/phpspec run
-
Behat(非JS场景)
vendor/bin/behat --strict --tags="~@javascript"
-
Behat(JS场景)
-
启动无头 Chrome
google-chrome-stable --enable-automation --disable-background-networking --no-default-browser-check --no-first-run --disable-popup-blocking --disable-default-apps --allow-insecure-localhost --disable-translate --disable-extensions --no-sandbox --enable-features=Metal --headless --remote-debugging-port=9222 --window-size=2880,1800 --proxy-server='direct://' --proxy-bypass-list='*' http://127.0.0.1
- 安装 SSL 证书(仅需一次)并在
127.0.0.1:8080
上运行测试应用的 web 服务器
symfony server:ca:install APP_ENV=test symfony server:start --port=8080 --dir=tests/Application/public --daemon
- 运行 Behat
vendor/bin/behat --strict --tags="@javascript"
-
静态分析
-
Psalms
vendor/bin/psalm
-
PHPStan
vendor/bin/phpstan analyse -c phpstan.neon -l max src/
-
-
编码规范
vendor/bin/ecs check src
使用您的插件打开 Sylius
-
使用
test
环境(cd tests/Application && APP_ENV=test bin/console sylius:fixtures:load) (cd tests/Application && APP_ENV=test bin/console server:run -d public)
-
使用
dev
环境(cd tests/Application && APP_ENV=dev bin/console sylius:fixtures:load) (cd tests/Application && APP_ENV=dev bin/console server:run -d public)