CommandBusBundle
, (*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;
}
}