phpはyii 2フレームワークに基づいてプラグインを作成しcomposerにコミットする


yii 2フレームワークでswiftmailerとredisに基づいて、カスタム非同期送信メールのプラグインを作成し、composerにコミットします.
1、vendorでディレクトリ構造を作成するには、次のようにします.
lvfk
このフォルダの下には複数のプラグインがあります
└── mailerqueue
プラグイン名
    └── src
プラグインのソースを保存
        ├── Mailer.php
yiiswiftmailerMailerを継承し、パラメータをカスタマイズし、Redisに格納されているメールを送信しています.
        └── Message.php
yiiswiftmailerMessageを継承し、既存の送信メール機能をRedisリストに格納するために書き換える
2、Mailを書く.phpとMessage.php
Message.php
redis;
        if(empty($redis)){
            throw new InvalidConfigException('redis not found in config');
        }

        //  mailer
        $mailer = \Yii::$app->mailer;
        if(empty($redis) || !$redis->select($mailer->db) || empty($mailer->key)){
            throw new InvalidConfigException('mailer not found in config');
        }
        if(empty($mailer->key)){
            throw new InvalidConfigException('parameter key not found in mailer config');
        }
        if(!$redis->select($mailer->db)){
            throw new InvalidConfigException('parameter db not found in mailer config');
        }


        //  
        $message = [];
        $message['from'] = array_keys($this->from);
        $message['to'] = array_keys($this->getTo());
        $message['cc'] = !empty($this->getCc())?array_keys($this->getCc()):[];
        $message['bcc'] = !empty($this->getBcc())?array_keys($this->getBcc()):[];
        $message['reply_to'] = !empty($this->getReplyTo())?array_keys($this->getReplyTo()):[];
        $message['charset'] = $this->getCharset();
        $message['subject'] = $this->getSubject();
        //    
        $parts = $this->getSwiftMessage()->getChildren();
        if(!is_array($parts) || count($parts) == 0){
            $parts = [$this->getSwiftMessage()];
        }
        foreach ($parts as $part){
            if(!$parts instanceof \Swift_Mime_Attachment){//       
                switch ($part->getContentType()){//      
                    case "text/html":
                        $message['html_body'] = $part->getBody();
                        break;
                    case "text/plain":
                        $message['text_body'] = $part->getBody();
                        break;
                }
            }
            if(!empty($message['charset'])){
                $message['charset'] = $part->getCharset();
            }
        }

        return $redis->rpush($mailer->key, json_encode($message));
    }
}
Mail.php 
setFrom($message['from'])->setTo($message['to']);
            if (!empty($message['cc'])) {
                $messageObj->setCc($message['cc']);
            }
            if (!empty($message['bcc'])) {
                $messageObj->setBcc($message['bcc']);
            }
            if (!empty($message['reply_to'])) {
                $messageObj->setReplyTo($message['reply_to']);
            }
            if (!empty($message['charset'])) {
                $messageObj->setCharset($message['charset']);
            }
            if (!empty($message['subject'])) {
                $messageObj->setSubject($message['subject']);
            }
            if (!empty($message['html_body'])) {
                $messageObj->setHtmlBody($message['html_body']);
            }
            if (!empty($message['text_body'])) {
                $messageObj->setTextBody($message['text_body']);
            }
            return $messageObj;
        }
        return false;
    }

    /**
     *    
     */
    public function process(){
        //  redis
        $redis = \Yii::$app->redis;
        if(empty($redis)){
            throw new InvalidConfigException('redis not found in config');
        }
        //      ,   
        if($redis->select($this->db) && $messages = $redis->lrange($this->key, 0, -1)){
            $messageObj = new Message();
            foreach ($messages as $message){
                $message = json_decode($message, true);
                if(empty($message) || !$this->setMessage($messageObj, $message)){
                    throw new ServerErrorHttpException('message error');
                }
                if($messageObj->send()){//      ,       
                    $redis->lrem($this->key, -1, json_encode($message));
                }
            }
        }

        return true;
    }
}

3、web.phpで構成し、ビジネスコードでカスタムプラグインを使用できます.
'aliases' => [//     ,  mailer class             
	'@lvfk/mailerqueue' => '@vendor/lvfk/mailerqueue/src'
],

     components     :
//   mailer  
'mailer' => [
	'class' => 'lvfk\mailerqueue\Mailer',
	'db' => 9,//  redis   
	'key' => 'mails',//redis     
	'useFileTransport' => false,
	'transport' => [
		'class' => 'Swift_SmtpTransport',
		'host' => 'smtp.163.com',
		'username' => '[email protected]',
		'password' => 'xxxx',
		'port' => '465',
		'encryption' => 'ssl',
	],
],

//redis       
'redis' => [
	'class' => 'yii\redis\Connection',
	'hostname' => 'localhost',
	'port' => 6379,
	'database' => 9,
	'password'  => '123456',
],
4、ビジネスコードで呼び出す
//    redis,    
\Yii::$app->mailer->compose('reply', [
	'username'=>$username,
	'userpass'=>$userpass,
	'useremail'=>$this->useremail,
	'time'=>$time,
	'token'=>$token
])
->setSubject('        ')
->setFrom(['[email protected]'=>'lvfk'])
->setTo($this->useremail)
->queue();
5、非同期送信、yii 2のcommandsコマンドを使用
5.1、yii 2のcommandコマンドはconsoleを構成するため使用する.phpはwebではなくphpですので、ステップ3の3つの構成をconsoleに移動します.phpで
5.2.commandsでファイルを作成するMailerController.php
class MailerController extends Controller
{
    /**
     *     
     */
    public function actionSend()
    {
        \Yii::$app->mailer->process();
        var_dump('    ');
    }
}
5.3、送信
プロジェクトの更なるディレクトリで実行:./yii mailer/send
6、crontabタスクタイミングでメールを送る
*/3 * * * */var/www/yii2/yii mailer/send >>/var/log/mailer.log
7、プラグインコミットcomposer
7.1、githubのウェブサイトに新しいライブラリyii 2_を作成するmailerqueue
7.2、新しいライブラリをローカルにクローンする
git clone https://github.com/Justshunjian/yii2_mailerqueue.git
7.3、プラグインsrcをローカルのyii 2_にコピーするmailerqueue
cp -r/var/www/yii2-shop/vendor/lvfk/mailerqueue/src/yii2_mailerqueue/
7.4、composerを作成する.json
{
	"name": "lvfk/mailerqueue",
	"type": "yii2-extension",
	"description": " async mailer by swiftmailer and redis base on yii2",
	"keywords": ["yii2","redis","async","mailer"],
	"license": "MIT",
	"support": {
		"issues":"https://github.com/Justshunjian/yii2_mailerqueue/issues",
		"forum":"",
		"source":"https://github.com/Justshunjian/yii2_mailerqueue"
	},
	"authors":[
		{
			"name":"lvfk",
			"email":"[email protected]"
		}
	],
	"require":{
		"yiisoft/yii2":"*",
		"yiisoft/yii2-redis":"~2.0.0"
	},
	"autoload":{
		"psr-4":{
			"lvfk\\mailerqueue\\":"src/"
		}
	}
}
7.5、githubへ提出
7.6、composer公式サイトに入るhttps://packagist.org/、githubアカウントでログイン
7.7、ログイン成功後、右上隅submitをクリック
A、提出対象項目githubのアドレスをホームページテキストボックスにコピーする
https://github.com/Justshunjian/yii2_mailerqueue.git
B、A経由でコミットは成功しましたが、githubが更新された後、composerのデータが自動的に更新されるように、ページ上でヒントに従って設定する必要があります.
curl -XPOST -H'content-type:application/json' 'https://packagist.org/api/update-package?username=Justshunjian&apiToken=TOKEN' -d'{"repository":{"url":"https://github.com/Justshunjian/yii2_mailerqueue.git"}}'
TOKENがProfileのYour API Tokenに隠されている
8、ダウンロードしてインストールして使用する
8.1、ダウンロードインストール
composer require lvfk/mailerqueue
8.2、配置は微調整があり、業務コードの使用は調整する必要がない
ダウンロードインストールなので、コンフィギュレーションファイルの3つのコンフィギュレーションで別名設定aliasesを設定する必要はありません.composerが自動的にロードするので、保持するだけです.
次の2つはcomponents配列で構成されています.
//   mailer  
'mailer' => [
	'class' => 'lvfk\mailerqueue\Mailer',
	'db' => 9,//  redis   
	'key' => 'mails',//redis     
	'useFileTransport' => false,
	'transport' => [
		'class' => 'Swift_SmtpTransport',
		'host' => 'smtp.163.com',
		'username' => '[email protected]',
		'password' => 'xxxx',
		'port' => '465',
		'encryption' => 'ssl',
	],
],

//redis       
'redis' => [
	'class' => 'yii\redis\Connection',
	'hostname' => 'localhost',
	'port' => 6379,
	'database' => 9,
	'password'  => '123456',
],
9、完了