minhlhst/command-bus
最新稳定版本:1.0.0
Composer 安装命令:
composer require minhlhst/command-bus
包简介
Simple Command Bus for Laravel framework from ST team
README 文档
README
ST team Simple Command Bus for Laravel framework
Installation
composer require minhlhst/command-bus
If your Laravel version is less than 5.5, add the following line to the providers array in config / app.php:
MinhlhSt\CommandBus\CommandBusServiceProvider::class,
Example
class UserController extends Controller { public function store(Request $request) { $user = $this->dispatch(new RegisterUserCommand( $request->input('email'), $request->input('password') )); return $user; } }
Usage
Command
use MinhlhSt\CommandBus\Command; class RegisterUserCommand implements Command { public function __construct( private string $name, private string $password ) { } public function email(): string { return $this->email; } public function password(): string { return $this->password; } public function setName(string $name): void { $this->name = $name; } public function setPassword(string $password): void { $this->password = $password; } }
Handler
use MinhlhSt\CommandBus\Handler; use MinhlhSt\CommandBus\Command; class RegisterUserHandler implements Handler { private $userRepository; public function __construct(UserRepository $userRepository) { $this->userRepository = $userRepository; } public function handle(Command $command): User { $user = new User( $command->email(), $command->password() ); $this->userRepository->store($user); return $user; } }
Controllers
use MinhlhSt\CommandBus\AbsctractController; use MinhlhSt\CommandBus\CommandBus; use MinhlhSt\CommandBus\Command; class Controller extends AbsctractController { private $dispatcher; public function __construct(CommandBus $dispatcher) { $this->dispatcher = $dispatcher; } public function dispatch(Command $command) { return $this->dispatcher->execute($command); } }
class UserController extends Controller { public function store(Request $request) { $user = $this->dispatch(new RegisterUserCommand( $request->input('email'), $request->input('password') )); return $user; } }
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-30