定制 libelulasoft/yii2-async-await 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

libelulasoft/yii2-async-await

Composer 安装命令:

composer require libelulasoft/yii2-async-await

包简介

About Async await for php yii2 integration, this uses amphp

README 文档

README

Async await for php yii2 integration, this uses amphp and support callbacks and Task

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist libelulasoft/yii2-async-await

or add

"libelulasoft/yii2-async-await": "~1.0.0"

to the require section of your composer.json file.

Migration

Si quieres migrar de la version original taguz91/yii2-async-await a la nueva version libelulasoft/yii2-async-await debes seguir los siguientes pasos:

  1. Eliminar la version original
composer remove taguz91/yii2-async-await
  1. Instalar la nueva version
composer require libelulasoft/yii2-async-await
  1. Actualizar los namespace donde se este utilizando la libreria, en todo el proyecto debemos reemplazar taguz91\AsyncAwait\ a Libelulasoft\AsyncAwait\

  2. Probar que todo funcione de forma correcta.

Usage

Once the extension is installed, simply use it in your code by:

Need to configure the bootstrap configuration app, because the async run in another context.

The following examples was tested in advanced template.

For example:

return [
    'id' => 'app-async',
    'basePath' => dirname(__DIR__),
    // Your controllers, you can change this to backend\controllers
    'controllerNamespace' => 'frontend\controllers',
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    // Required components for async functions 
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        // Config your database 
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
    'params' => [],
];

Also need to create the entry script, for autoload the dependecies and start Yii2 app.

For example:

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
// Autoload for composer an yii2 
require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../common/config/bootstrap.php';


// Your custom configuration for async 
$config = require __DIR__ . '/async-main.php';
// You can change the console application to web application 
// You dont have acces to vars or configuration in parent context
new yii\console\Application($config);

Adding to web app, in components section you need to add this configuration:

[
    ...,
    'components' => [
        // If you want to use callbacks 
        'asyncAwait' => [
            'class' => \Libelulasoft\AsyncAwait\AsyncAwait::class,
            // Your own entry script, see the above examples
            'loader' => __DIR__ . '/async.php'
        ],
        // If you want to use classes, this is more faster 
        'asyncTask' => [
            'class' => \Libelulasoft\AsyncAwait\AsyncTask::class,
            // Your own entry script, see the above examples
            'loader' => __DIR__ . '/async.php'
        ],
    ]
]

Code example for callbacks usage:

use common\models\User;

// Adding you async function 
Yii::$app->asyncAwait->add('sendUserEmail', function (string $idUser, string $sender) {
    $user = User::findOne($idUser);
    // Return any serializable data, is prefer return a basic array response 
    return \common\models\Email::sendUser($user, $sender);
}, $idUser, $sender);

Yii::$app->asyncAwait->add('sendUserMessage', function (string $message, string $number) {
    if ($number === '') return 'Number is required.';
    return \common\models\Phone::sendMessage($message, $number);
}, $message, $number);

// Execute your asynct functions 
$responses = Yii::$app->asyncAwait->run();
// Getting especific response
$emailResponse = $responses['sendUserEmail'];

Code example for Tasks:

// This class is autoloadable 
namespace common\tasks;

use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use yii\helpers\VarDumper;

class PrintTask implements Task
{
    private $text;

    public function __construct(string $text)
    {
        $this->text = $text;
    }

    /**
     * {@inheritdoc}
     */
    public function run(Environment $environment)
    {
        return [
            'response' => "FUTURE PROMISE WORKER {$this->text}",
            'enviroment' => VarDumper::dumpAsString($environment),
        ];
    }
}


/** @var \Libelulasoft\AsyncAwait\AsyncTask */
$async = Yii::$app->asyncTask;

$async->add('1', new PrintTask('THIS IS MY LARGE TEXT'));
$response = $async->run();

libelulasoft/yii2-async-await 适用场景与选型建议

libelulasoft/yii2-async-await 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「extension」 「async」 「yii2」 「await」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 libelulasoft/yii2-async-await 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 libelulasoft/yii2-async-await 我们能提供哪些服务?
定制开发 / 二次开发

基于 libelulasoft/yii2-async-await 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.99k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-11-10