wearesho-team/yii2-message-delivery

消息传递 Yii2 实现

2.0.0 2024-09-09 15:50 UTC

README

Test & Lint Latest Stable Version Total Downloads codecov

此存储库包含使用 Yii2 ActiveRecord 实现的 RepositoryInterface 实现。

安装

composer require wearesho-team/yii2-message-delivery:^1.8.0

使用

配置

<?php
// common/config/main.php

use Wearesho\Delivery;

return [
    'bootstrap' => [
        Delivery\Yii2\Bootstrap::class, // registers migrations and configures container        
    ],
];

队列

此包提供可选的 yii2-queue 集成。要使用它,您必须安装 yii2-queue 包。

composer require yiisoft/yii2-queue:^2.0

然后您可以配置您的应用程序

<?php
// common/config/main.php

use Wearesho\Delivery;

return [
    'bootstrap' => [
        [
            'class' => Delivery\Yii2\Bootstrap::class,
            'service' => [
                'class' => Delivery\Yii2\Queue\Service::class,
                'service' => Delivery\ServiceMock::class, // you your custom Delivery\ServiceInterface implementation
            ],
        ],
    ],
];

注意:使用 Queue\Service 发送的邮件必须正确处理 serialize() 和 unserialize()。有关详细信息,请参阅 yii2-queue。

SwitchService

您可以配置多个交付服务,并使用环境变量选择其中一个。

<?php

use Wearesho\Delivery;
use App;

$service = new Delivery\Yii2\SwitchService([
    'environmentKeyPrefix' => 'DELIVERY_', // by default,
    'services' => [
        'default' => [
            'class' => Delivery\ServiceMock::class,
        ],
        'production' => [
            'class' => App\Delivery\Service::class, // some Delivery\ServiceInterface implementation
        ],
    ],
]);

putenv('DELIVERY_SERVICE'); // clean environment
$message = new Delivery\Message('text', 'recipient');
$service->send($message); // default service will be used if no environment variable set
putenv('DELIVERY_SERVICE=production');
$service->send($message); // production service will be used if it was configured

RepositoryService

您可以将任何您的服务封装到具有仓库的 RepositoryService 中。如果包装服务发送消息没有抛出任何异常,则消息将存储为已发送。

<?php

use Wearesho\Delivery;
use App;

$service = new Delivery\Yii2\RepositoryService([
    'service' => App\CustomService::class,
    'repository' => Delivery\Yii2\Repository::class, // or your own implementation
]);

// do what you want using Delivery\ServiceInterface

作者

许可证

MIT