Sylius plugin to define a command cli context (ChannelContext)

Overview

Cli Context Plugin

This plugin provide a default channel context for your Symfony Command.

When Sylius load a resource implements Sylius\Component\Resource\Model\TranslatableInterface, a DoctrinepostLoad event listener defines the default local and the current locale into the loaded object. The current local is determined from the current HTTP request and the ChannelContext.

In HTTP context, the current channel into ChannelContext is defined from hostname.

In Console context (cli), we have no HTTP request and the ChannelContext is empty. When you load a translatable resource, the channel context will load the first channel after running the findAll function on ChannelRepository.

But the ChannelContext will execute the findAll function for each resource loaded. The more you load resource more your command consume more memory and use more CPU resources.

Know issues: Performance issue in cli commands: channel db request after each translatable entity postLoad event and Multiple channels with php-cli is not possible

This Sylius plugin allows you to load the channel into the ChannelContext on ConsoleCommandEvent event. The findAll function on ChannelRepository will never be executed by ChannelContext and you preserve your performances.

Installation

Run the command composer require jbdevlabs/sylius-cli-context-plugin dev-main

Extends the Sylius Channel Repository

Note: If you have already extended the ChannelRepository, implement the interface JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface and use the trait JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderTrait for interface implementation. Goto the alias configuration step.

Implement and use trait

Make a new PHP class ChannelRepository in src/Repository like this:



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;
}

Configure the new repository

Declare this repository on config/packages/_sylius.yaml file like this:

sylius_channel:
    resources:
        channel:
            classes:
                repository: App\Repository\ChannelRepository

Fix the interface alias

Declare the alias for JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface to the channel repository.

services:
  JbDevLabs\SyliusCliContextPlugin\Repository\CliChannelProviderInterface: '@sylius.repository.channel'

Configuration

Add a file jb_dev_labs_sylius_cli_context.yaml into config/packages and set your configuration.

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:      []

Usage on your command

By default, no Channel context is loaded.

To automaticaly load channel context for your command, implement the interface JbDevLabs\SyliusCliContextPlugin\Command\CliContextAwareInterface on your command.

Example:



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;
    }
}

Usage on bundle command

You want load the Sylius Channel context when command provided by another plugin is executed ?

Add the command class namespace on your configuration to enable the Context initialisation.

jb_dev_labs_sylius_cli_context:
    # Set command PHP Class namespace to load Sylius Context
    include_command:
        - Vendor\SyliusVendorPlugin\Command\StufCommand

Usage programatically

See the documentation

Contributor Installation (docker)

  1. Clone this project

  2. From the plugin skeleton root directory, run the following commands:

$ 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

Quality tools

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

Contributor quickstart Installation (legacy)

  1. Clone this project

  2. From the plugin skeleton root directory, run the following commands:

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

To be able to setup a plugin's database, remember to configure you database credentials in tests/Application/.env and tests/Application/.env.test.

Usage

Running plugin tests

  • PHPUnit

    vendor/bin/phpunit
  • PHPSpec

    vendor/bin/phpspec run
  • Behat (non-JS scenarios)

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

    1. Install Symfony CLI command.

    2. Start Headless 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. Install SSL certificates (only once needed) and run test application's webserver on 127.0.0.1:8080:
    symfony server:ca:install
    APP_ENV=test symfony server:start --port=8080 --dir=tests/Application/public --daemon
    1. Run Behat:
    vendor/bin/behat --strict --tags="@javascript"
  • Static Analysis

    • Psalm

      vendor/bin/psalm
    • PHPStan

      vendor/bin/phpstan analyse -c phpstan.neon -l max src/  
  • Coding Standard

    vendor/bin/ecs check src

Opening Sylius with your plugin

  • Using test environment:

    (cd tests/Application && APP_ENV=test bin/console sylius:fixtures:load)
    (cd tests/Application && APP_ENV=test bin/console server:run -d public)
  • Using dev environment:

    (cd tests/Application && APP_ENV=dev bin/console sylius:fixtures:load)
    (cd tests/Application && APP_ENV=dev bin/console server:run -d public)
You might also like...
Cilex a lightweight framework for creating PHP CLI scripts inspired by Silex

Cilex, a simple Command Line Interface framework Cilex is a simple command line application framework to develop simple tools based on Symfony2 compon

PHP Version Manager for the CLI on Windows

This package has a much more niche use case than nvm does. When developing on Windows and using the integrated terminal, it's quite difficult to get those terminals to actually listen to PATH changes.

Library for creating CLI commands or applications

Console Motivation: this library purpose is to provide a lighter and more robust API for console commands and/or applications to symfony/console. It c

PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace

MODX Extra Publisher PHP CLI tool which allows publishing zipped MODX extra to modstore.pro marketplace. Installation global? local? To install packag

A handy set of Stringable mixins for CLI text.

Laravel Colorize A mixin for Laravel's Stringable to easily apply colors and styles to CLI text. Installation You can install the package via Composer

PHP CLI project to get an appointment from https://vacunacovid.catsalut.gencat.ca

covid_vaccine_bcn PHP CLI project to get an appointment from https://citavacunacovid19.catsalut.gencat.cat/Vacunacio_Covid/Vacunacio/VacunacioCovidRes

PHP CLI to add latest release notes to a CHANGELOG

changelog-updater A PHP CLI to update a CHANGELOG following the "Keep a Changelog" format with the latest release notes. Want to automate the process

unofficial cli built using php which can be used to upload and download files from anonfiles.com

Anonfiles CLI Table of Contents Introduction Features Screenshots Installation Contributing License Introduction Anon Files CLI can upload and downloa

A CLI program that helps you check your endpoints by requesting the given servers and send a report message in any supported channel like Telegram

API Monitor A CLI program that help you check your endpoints by requesting the given servers and send a report message in any supported channel ( Tele

Comments
Releases(v1.2.0)
Owner
null
☄️ PHP CLI mode development framework, supports Swoole, WorkerMan, FPM, CLI-Server

☄️ PHP CLI mode development framework, supports Swoole, WorkerMan, FPM, CLI-Server / PHP 命令行模式开发框架,支持 Swoole、WorkerMan、FPM、CLI-Server

Mix PHP 1.8k Jan 3, 2023
WP-CLI Trait Package Command

WP-CLI Trait Package Command Generate plugin or php model files e.g. post-type or taxonomy for WP-Trait Package in Develop WordPress Plugin. Installat

Mehrshad Darzi 2 Dec 17, 2021
The Platform.sh CLI is the official command-line interface for Platform.sh

The Platform.sh CLI is the official command-line interface for Platform.sh. Use this tool to interact with your Platform.sh projects, and to build them locally for development purposes.

Platform.sh 222 Dec 29, 2022
Image Optimize Command - Easily optimize images using WP CLI

Image Optimize Command is a WP CLI wrapper for spatie/image-optimizer which optimize gif, jpeg, jpg, png, svg, webp images by running them through a chain of various image optimization tools.

Typist Tech Limited 157 Dec 13, 2022
A powerful command line application framework for PHP. It's an extensible, flexible component, You can build your command-based application in seconds!

CLIFramework CLIFramework is a command-line application framework, for building flexiable, simple command-line applications. Commands and Subcommands

Yo-An Lin 428 Dec 13, 2022
Plugin Machine CLI

PHP CLI For Plugin Machine Install composer global require imaginary-machines/plugin-machine-php-cli:dev-main -W Requires Composer 2.0 or later PHP 7.

Imaginary Machines (Plugin Machine| {Future} Machine(s) 2 Jan 6, 2022
Deactivate and activate a plugin with just one in-game command

DEPlugins | v1.0.0 ✔️ Deactivate and activate a plugin with just one in-game command ✔️ Features Deactivate and activate a plugin with just one in-gam

Noob MCBG 0 Apr 17, 2022
🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

Contents Minimum Requirements Installation Upgrading Usage Quick Setup Examples API Appearance Menu Title Colour Width Padding Margin Borders Exit But

PHP School 1.9k Dec 28, 2022
An Elegant CLI Library for PHP

Commando An Elegant PHP CLI Library Commando is a PHP command line interface library that beautifies and simplifies writing PHP scripts intended for c

Nate Good 793 Dec 25, 2022