danilovl / async-bundle
Symfony 扩展包,提供简单的延迟函数调用。
v0.4.2
2024-03-30 07:48 UTC
Requires
- php: ^8.3
- symfony/framework-bundle: ^7.0
- symfony/http-kernel: ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.2
README
AsyncBundle
关于
Symfony 扩展包在 Symfony 发送响应后,在 AsyncListener
中提供简单的延迟函数调用。
由于所有不必要的逻辑都在稍后处理,用户可以更快地获得响应。例如:记录日志、创建 rabbitmq 队列或其他不必要的事情。
用户可以从服务器更早地收到响应,无需等待不必要的进程完成。
需求
- PHP 8.3 或更高版本
- Symfony 7.0 或更高版本
1. 安装
使用 Composer 安装 danilovl/async-bundle
包
composer require danilovl/async-bundle
如果未自动添加,请将 AsyncBundle
添加到您的应用程序的包中
<?php // config/bundles.php return [ // ... Danilovl\AsyncBundle\AsyncBundle::class => ['all' => true] ];
2. 使用方法
AsyncService
有三个简单的方法:add
、remove
和 reset
。
<?php declare(strict_types=1); namespace App\Controller; use Danilovl\AsyncBundle\Attribute\PermissionMiddleware; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\{ Request, Response }; class HomeController extends AbstractController { public function __construct(private AsyncService $asyncService) { } public function index(Request $request): Response { $this->asyncService->add(function () { // add callback with priority 0 and without name }); $this->asyncService->add(function () { // add callback with priority 10 // higher means sooner }, 10); $this->asyncService->add(function () { // add callback with priority -10 // less means later }, -10); $this->asyncService->add(function () { // add callback with priority and name }, 90, 'sendEmail'); $this->asyncService->add(function () { // add second callback with priority and same name }, 100, 'sendEmail'); // remove all callbacks with name 'sendEmail' $this->asyncService->remove(['sendEMail']); // remove all callbacks with name 'sendEmail' and priority $this->asyncService->remove(['sendEMail'], 100); // remove all callbacks $this->asyncService->reset(); return $this->render('home/index.html.twig'); } }
许可证
AsyncBundle 是开源软件,许可证为 MIT 许可证。