danilovl/async-bundle

Symfony 扩展包,提供简单的延迟函数调用。

安装: 163

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v0.4.2 2024-03-30 07:48 UTC

This package is auto-updated.

Last update: 2024-09-30 08:56:38 UTC


README

phpunit downloads latest Stable Version license

AsyncBundle

关于

Symfony 扩展包在 Symfony 发送响应后,在 AsyncListener 中提供简单的延迟函数调用。

由于所有不必要的逻辑都在稍后处理,用户可以更快地获得响应。例如:记录日志、创建 rabbitmq 队列或其他不必要的事情。

Alt text

用户可以从服务器更早地收到响应,无需等待不必要的进程完成。

Alt text

需求

  • 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 有三个简单的方法:addremovereset

<?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 许可证