jbdevlabs/sylius-cli-context-plugin

轻松管理 CLI 命令上的 Sylius Context 并保持性能

资助包维护!
macintoshplus

安装次数: 5,159

依赖关系: 0

建议者: 0

安全: 0

星标: 4

关注者: 1

分支: 1

开放问题: 0

类型:sylius-plugin

This package is auto-updated.

Last update: 2024-09-04 13:43:24 UTC


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 事件上加载通道到 ChannelContextChannelRepository 上的 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)

  1. 克隆此项目

  2. 从插件骨架根目录,运行以下命令

$ 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 ;)

贡献者快速安装(旧版)

  1. 克隆此项目

  2. 从插件骨架根目录,运行以下命令

    $ (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/.envtests/Application/.env.test 中配置您的数据库凭据。

用法

运行插件测试

  • PHPUnit

    vendor/bin/phpunit
  • PHPSpec

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

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

    1. 安装 Symfony CLI 命令.

    2. 启动无头 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
    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
    1. 运行 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)