clearcode/command-bus-console

CLI for command bus.

v0.2.0 2015-12-11 09:45 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:31:55 UTC


README

Build Status Scrutinizer Code Quality MIT License

Command Bus Console

Command Bus Console is a package exposing your command bus functionality to the CLI. Command Bus Console is based on Symfony Console Form and https://github.com/SimpleBus.

Installation

$ composer require clearcode/command-bus-console

Enable bundles in the kernel of your Symfony application.

    <?php
    // app/AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new SimpleBusCommandBusBundle(), // this one you probably have already registered
            new SymfonyConsoleFormBundle(),
            new Clearcode\CommandBusConsole\Bundle\CommandBusConsoleBundle(),
        );
    }

Usage

Create and register form type for your command.

Assumed that you already have a command class and its handler class, create form type class mapping your command properties:

class SignUpType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('id', TextType::class, [
                'label' => 'Id',
            ])
            ->add('name', TextType::class, [
                'label' => 'Name',
            ])
            ->add('email', TextType::class, [
                'label' => 'email',
            ])
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'data_class' => SignUp::class,
        ]);
    }

    ...
}

And register your form type using command_bus.type with required attributes command which is FQCN of your command and alias which will be used to register console command with name command-bus:alias.

    form_type_service_id:
        class: Fully\Qualified\Class\Name\Of\SignUpType
        tags:
            - { name: form.type }
            - { name: command_bus.type, command: Fully\Qualified\Class\Name\Of\SignUp, alias: sign-up }

Run command in interactive mode

$ bin/console command-bus:sign-up
Id:
Name:
email:

[2015-12-11 10:34:55] The Fully\Qualified\Class\Name\Of\SignUp executed with success.

Run command in non interactive mode

$ bin/console command-bus:alias-for-command --no-interaction --id=1 --name=John --email=john@doe.com

[2015-12-11 10:34:55] The Fully\Qualified\Class\Name\Of\SignUp executed with success.

To Do

  • All fields should be required
  • Add generating form types on the fly
  • Add support for instantiating command objects via __construct
  • Add possibility to use any command bus implementation
  • Introduce abstraction on command bus

License

MIT, see LICENSE.