2017 © Pedro PelΓ‘ez
 

symfony-bundle command-bus-bundle

Command bus

image

itmedia/command-bus-bundle

Command bus

  • Tuesday, June 5, 2018
  • by by25
  • Repository
  • 1 Watchers
  • 1 Stars
  • 102 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

CommandBusBundle

Scrutinizer Code Quality Build Status, (*1)

Install

composer require itmedia/command-bus-bundle 

CommandBus

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ рСгистрации сСрвиса, Ρ‚Π°ΠΊΠΆΠ΅ смотри Π½ΠΈΠΆΠ΅ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ встроСных ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ² ΠΊΠΎΠΌΠΌΠ°Π½Π΄ (Middleware):, (*2)

services:

    app.command_bus:
        class: Itmedia\CommandBusBundle\CommandBus\CommandBus
        arguments: ["@itmedia_command_bus.container_handler_mapper"]

Middleware

Middleware Ρ€Π΅Π°Π»ΠΈΠ·ΡƒΡŽΡ‚ Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΡƒΡŽ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΡƒ сообщСний, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€: Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΡŽ, ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ ΠΏΡ€Π°Π² доступа, Π»ΠΎΠ³ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅. Middleware Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Ρ€Π΅Π°Π»ΠΈΠ·ΠΎΠ²Ρ‹Π²Π°Ρ‚ΡŒ интСрфСйс MiddlewareInterface. Π’ CommandBus ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ сообщСния происходит Π΅Π³ΠΎ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½Π½Ρ‹ΠΌΠΈ Middleware. ΠŸΡ€ΠΈ Π½Π΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΏΡ€Π°Π²ΠΈΠ», Π΄ΠΎΠ»ΠΆΠ½ΠΎ всСгда Π²Ρ‹Π±Ρ€Π°ΡΡ‹Π²Π°Ρ‚ΡŒΡΡ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅., (*3)

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΈ:, (*4)

services:
    app.command_bus:
        class: Itmedia\CommandBusBundle\CommandBus\CommandBus
        arguments: ["@itmedia_command_bus.container_handler_mapper"]
        calls:
            - [addMiddleware, ["@itmedia_command_bus.middleware_validation"]]
            - [addMiddleware, ["@app.middleware_access_control"]] 

    # custom middleware
    app.middleware_access_control:
        class: AppBundle\Middleware\AccessControlMiddleware

Command

Команда Π΄ΠΎΠ»ΠΆΠ½Π° ΠΈΠΌΠ΅Ρ‚ΡŒ интСрфСйс Command., (*5)

use Itmedia\CommandBusBundle\Command\Command;

class TestCommand implements Command
{
    //...

    public function commandName(): string
    {
        return 'test_command';
    }

}

Handlers ΠΌΠΎΠ³ΡƒΡ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΡƒΡŽ структуру, Ссли ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ΡΡ, Π»ΠΈΠ±ΠΎ для Π΅Π΄ΠΈΠ½ΠΈΡ‡Π½ΠΎΠ³ΠΎ handler - CommandHandler, (*6)

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΈ CommandHandler:, (*7)

services:
    # ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½ ΠΌΠ΅Ρ‚ΠΎΠ΄ execute()
    AppBundle\Handler\MyHandler:
        public: true
        tags:
            - {name: command_handler, command: core_register_user } 


    # явноС ΡƒΠΊΠ°Π·Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ²
    AppBundle\Handler\NyHandler2:
        public: true
        tags:
            - {name: command_handler, command: core_register_user1, method: methodName1 }
            - {name: command_handler, command: core_register_user2, method: methodName2 }
            - {name: command_handler, command: core_register_user3, method: methodName3 }

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования:, (*8)

    $command = new CommandTest();
    $this->get('app.command_bus')->handle($command);

Валидация ΠΊΠΎΠΌΠ°Π½Π΄

Для Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ ΠΊΠΎΠΌΠ°Π½Π΄ срСдствами symfony/validator Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ ValidationMiddleware для CommandBus:, (*9)

services:
    Itmedia\CommandBusBundle\CommandBus\CommandBus:
        arguments: ["@itmedia_command_bus.container_handler_mapper"]
        calls:
            - [addMiddleware, ["@itmedia_command_bus.middleware_validation"]]

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΏΡ€Π°Π²ΠΈΠ» Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹:, (*10)

use Itmedia\CommandBusBundle\Command\Command;
use Symfony\Component\Validator\Constraints as Assert;

class TestCommand implements Command
{

    /**
     * @NotBlank()
     */
    private string $username;

    /**
     * @NotBlank()
     * @Assert\Email()
     */
    private string $email;

    public function __construct(string $username, string $email)
    {
        $this->username = $username;
        $this->email = $email;
    }

    public function commandName(): string
    {
        return 'test_command';
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

}

Если ΠΊΠΎΠΌΠΌΠ°Π½Π΄Π° Π½Π΅ ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΡ‚ Π²Π°Π»ΠΈΠ΄Π°Ρ†ΠΈΡŽ выбрасываСтся ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ ValidationException, (*11)

Установка Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ свойств ΠΊΠΎΠΌΠΌΠ°Π½Π΄Ρ‹ ΠΈΠ· массива

HandlePropertiesFormArrayTrait - Π²ΡΠΏΠΎΠΌΠΎΠ³Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ Ρ‚Ρ€Π΅ΠΉΡ‚ для устаовки Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Ρƒ ΠΈΠ· массива Π² свойства ΠΊΠΎΠΌΠ°Π½Π΄Ρ‹. НазваниС ΠΊΠ»ΡŽΡ‡Π° Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΠ²ΠΎΠ²Π°Ρ‚ΡŒ названию свойства., (*12)

use Itmedia\CommandBusBundle\Command\Command;
use Itmedia\CommandBusBundle\Command\HandlePropertiesFormArrayTrait;

class TestCommand implements Command
{

    use HandlePropertiesFormArrayTrait;

    // ....

    public function __construct($id, array $data)
    {
        $this->handleProperties($data);
        $this->id = $id;
    }

}    

The Versions

05/06 2018

dev-master

9999999-dev https://github.com/by25/CommandBusBundle

Command bus

  Sources   Download

MIT

The Requires

 

The Development Requires

05/06 2018

dev-develop

dev-develop https://github.com/by25/CommandBusBundle

Command bus

  Sources   Download

MIT

The Requires

 

The Development Requires

05/06 2018
08/02 2017

v0.2.0

0.2.0.0 https://github.com/by25/CommandBusBundle

Command bus

  Sources   Download

MIT

The Requires

 

The Development Requires

10/10 2016

v0.1.0

0.1.0.0 https://github.com/by25/CommandBusBundle

Command bus

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires