taguz91 / yii2-async-await
关于 PHP YII2 集成中的异步/await,此项目使用 amphp
1.1.1
2022-03-14 22:02 UTC
Requires
- amphp/parallel: ^1.4
- amphp/parallel-functions: ^1.0
- opis/closure: ^3.6
- yiisoft/yii2: ~2.0.38
This package is auto-updated.
Last update: 2024-09-15 03:29:19 UTC
README
PHP YII2 集成中的异步/await,使用 amphp 支持回调和任务
安装
安装此扩展的首选方式是通过 composer。
可以运行
composer require --prefer-dist taguz91/yii2-async-await
或者在您的 composer.json
文件的 require 部分添加
"taguz91/yii2-async-await": "~1.0.0"
到
使用
扩展安装后,只需在代码中简单地使用即可
需要配置 bootstrap 配置文件 app,因为异步运行在另一个上下文中。
以下示例已在高级模板中测试过。
例如
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' => [], ];
还需要创建入口脚本,用于自动加载依赖项并启动 YII2 应用程序。
例如
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);
添加到 web 应用程序,在 components 部分您需要添加以下配置
[ ..., 'components' => [ // If you want to use callbacks 'asyncAwait' => [ 'class' => \taguz91\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' => \taguz91\AsyncAwait\AsyncTask::class, // Your own entry script, see the above examples 'loader' => __DIR__ . '/async.php' ], ] ]
回调使用的代码示例
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'];
任务使用的代码示例
// 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 \taguz91\AsyncAwait\AsyncTask */ $async = Yii::$app->asyncTask; $async->add('1', new PrintTask('THIS IS MY LARGE TEXT')); $response = $async->run();