guennichi / performist
轻量级的PHP库,优雅地执行一系列操作
v2.1.0
2021-03-07 01:19 UTC
Requires
- php: >=7.4
Requires (Dev)
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ^9.5
README
无依赖的轻量级同步动作执行器库。
安装
composer require guennichi/performist
使用
动作执行器
class RegisterUser { protected string $username; protected string $password; public function __construct(string $username, string $password) { $this->username = $username; $this->password = $password; } public function getUsername(): string { return $this->username; } public function getPassword(): string { return $this->password; } } class RegisterUserHandler implements \Guennichi\Performist\HandlerInterface { // Inject DB and other services here... public function __invoke(RegisterUser $action) { $user = new User($action->getUsername(), $action->getPassword()); // Encode password $this->repository->add($user); // Send email notification etc... return $user; } } // Register actions/handlers $registry = new \Guennichi\Performist\Registry(); $registry->add(RegisterUser::class, new RegisterUserHandler()); $registry->add(OtherAction::class, new OtherActionHandler()); // Instantiate the performer. $performer = new \Guennichi\Performist\Performer($registry, new \Guennichi\Performist\HandlerPeeler()); // Do the job $user = $performer->perform(new RegisterUser('foo@bar.com', 'password'), [ new DoctrineTransactionMiddleware(), new LoggerMiddleware(), // ... ]); // Generic job $result = $performer->perform(new OtherAction(/** some data */), [ new BetweenMiddleware(), new BeforeMiddleware(), new AfterMiddleware() ]);
中间件
为了能够使用此库的中间件功能,您需要创建一个实现了 \Performist\MiddlewareInterface
的类
class MyMiddleware implements \Guennichi\Performist\MiddlewareInterface { public function handle($action, Closure $next) { // Do something here before performing the action... // ... $result = $next($action); // Do something here after performing the action... // ... return $result; } } $result = $performer->perform(new MyAction(/** some data */), [ new MyMiddleware() ]);
支持PHP >= 7.4
此客户端库仅支持PHP >= 7.4的最新版本,更多详细信息请查看 支持版本