From 07c5af2173057833a29df2afbe01929605dde9b1 Mon Sep 17 00:00:00 2001 From: Eugene Terentev Date: Tue, 26 Jan 2016 11:49:58 +0200 Subject: [PATCH] closes #354 --- CHANGELOG.md | 3 +- README.md | 7 +- autocompletion.php | 2 +- ...neHandler.php => AddToTimelineCommand.php} | 23 ++++-- common/commands/SendEmailCommand.php | 80 +++++++++++++++++++ .../commands/command/AddToTimelineCommand.php | 24 ------ common/commands/command/SendEmailCommand.php | 56 ------------- common/commands/handler/SendEmailHandler.php | 34 -------- common/config/base.php | 13 +-- common/config/web.php | 4 +- common/models/User.php | 2 +- composer.json | 6 +- console/config/console.php | 25 +++--- .../user/controllers/SignInController.php | 2 +- .../user/models/PasswordResetRequestForm.php | 2 +- 15 files changed, 135 insertions(+), 148 deletions(-) rename common/commands/{handler/AddToTimelineHandler.php => AddToTimelineCommand.php} (61%) create mode 100644 common/commands/SendEmailCommand.php delete mode 100644 common/commands/command/AddToTimelineCommand.php delete mode 100644 common/commands/command/SendEmailCommand.php delete mode 100644 common/commands/handler/SendEmailHandler.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dd91aca6..e8d72ef8c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ Yii Starter Kit Change Log ========================== -2.1.0 under development +2.1.0 ----------------------- +- Enh #354: Command Bus implemented with yii2-command-bus extension - Enh #321: editOwnModel permission - Fixed #326 - API Fixes diff --git a/README.md b/README.md index 2011e2d38..4e71c0b95 100755 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ Password: user - Ready-to-go RESTful API module - [File storage component + file upload widget](https://github.com/trntv/yii2-file-kit) - On-demand thumbnail creation [trntv/yii2-glide](https://github.com/trntv/yii2-glide) +- Command Bus with queued and async tasks support [trntv/yii2-command-bus](https://github.com/trntv/yii2-command-bus) - Useful behaviors (GlobalAccessBehavior, CacheInvalidateBehavior, MaintenanceBehavior) - Yii2 log web interface - Application timeline component @@ -90,8 +91,7 @@ Password: user - Extended IDE autocompletion - Nginx config example - Test-ready -- Docker support -- Vagrant support +- Docker support and Vagrant support - Assets compression and concatenation - [Some useful shortcuts](https://github.com/trntv/yii2-starter-kit/blob/master/common/shortcuts.php) - many other features i'm lazy to write about :-) @@ -220,6 +220,9 @@ Add in your application config: ``` It will allow access to you application only for authentificated users. +### Command Bus +Read more about command bus on in [official repository](https://github.com/trntv/yii2-command-bus#yii2-command-bus) + ### Widgets configurable from backend #### Carousel 1. Create carousel in backend diff --git a/autocompletion.php b/autocompletion.php index beed80080..1164cc751 100644 --- a/autocompletion.php +++ b/autocompletion.php @@ -23,7 +23,7 @@ class Yii extends \yii\BaseYii * @property yii\web\UrlManager $urlManagerBackend UrlManager for backend application. * @property yii\web\UrlManager $urlManagerStorage UrlManager for storage application. * @property trntv\glide\components\Glide $glide - * @property trntv\tactician\Tactician $commandBus + * @property trntv\bus\CommandBus $commandBus */ abstract class BaseApplication extends yii\base\Application { diff --git a/common/commands/handler/AddToTimelineHandler.php b/common/commands/AddToTimelineCommand.php similarity index 61% rename from common/commands/handler/AddToTimelineHandler.php rename to common/commands/AddToTimelineCommand.php index 7331a0843..f6c78c85a 100644 --- a/common/commands/handler/AddToTimelineHandler.php +++ b/common/commands/AddToTimelineCommand.php @@ -1,17 +1,30 @@ */ -class AddToTimelineHandler extends BaseHandler +class AddToTimelineCommand extends Object implements SelfHandlingCommand { + /** + * @var string + */ + public $category; + /** + * @var string + */ + public $event; + /** + * @var mixed + */ + public $data; + /** * @param AddToTimelineCommand $command * @return bool diff --git a/common/commands/SendEmailCommand.php b/common/commands/SendEmailCommand.php new file mode 100644 index 000000000..c58fbda94 --- /dev/null +++ b/common/commands/SendEmailCommand.php @@ -0,0 +1,80 @@ + + */ +class SendEmailCommand extends Object implements SelfHandlingCommand +{ + /** + * @var mixed + */ + public $from; + /** + * @var mixed + */ + public $to; + /** + * @var string + */ + public $subject; + /** + * @var string + */ + public $view; + /** + * @var array + */ + public $params; + /** + * @var string + */ + public $body; + /** + * @var bool + */ + public $html = true; + + /** + * Command init + */ + public function init() + { + $this->from = $this->from ?: \Yii::$app->params['robotEmail']; + } + + /** + * @return bool + */ + public function isHtml() + { + return (bool) $this->html; + } + + /** + * @param \common\commands\SendEmailCommand $command + * @return bool + */ + public function handle($command) + { + if (!$command->body) { + $message = \Yii::$app->mailer->compose($command->view, $command->params); + } else { + $message = new Message(); + if ($command->isHtml()) { + $message->setHtmlBody($command->body); + } else { + $message->setTextBody($command->body); + } + } + $message->setFrom($command->from); + $message->setTo($command->to ?: \Yii::$app->params['robotEmail']); + $message->setSubject($command->subject); + return $message->send(); + } +} diff --git a/common/commands/command/AddToTimelineCommand.php b/common/commands/command/AddToTimelineCommand.php deleted file mode 100644 index 43f5632f6..000000000 --- a/common/commands/command/AddToTimelineCommand.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -class AddToTimelineCommand extends BaseCommand -{ - /** - * @var string - */ - public $category; - /** - * @var string - */ - public $event; - /** - * @var mixed - */ - public $data; -} diff --git a/common/commands/command/SendEmailCommand.php b/common/commands/command/SendEmailCommand.php deleted file mode 100644 index c93e3dac2..000000000 --- a/common/commands/command/SendEmailCommand.php +++ /dev/null @@ -1,56 +0,0 @@ - - */ -class SendEmailCommand extends BaseCommand -{ - /** - * @var mixed - */ - public $from; - /** - * @var mixed - */ - public $to; - /** - * @var string - */ - public $subject; - /** - * @var string - */ - public $view; - /** - * @var array - */ - public $params; - /** - * @var string - */ - public $body; - /** - * @var bool - */ - public $html = true; - - /** - * Command init - */ - public function init() - { - $this->from = $this->from ?: \Yii::$app->params['robotEmail']; - } - - /** - * @return bool - */ - public function isHtml() - { - return (bool) $this->html; - } -} diff --git a/common/commands/handler/SendEmailHandler.php b/common/commands/handler/SendEmailHandler.php deleted file mode 100644 index 4adfeb63d..000000000 --- a/common/commands/handler/SendEmailHandler.php +++ /dev/null @@ -1,34 +0,0 @@ - - */ -class SendEmailHandler extends BaseHandler -{ - /** - * @param \common\commands\command\SendEmailCommand $command - * @return bool - */ - public function handle($command) - { - if (!$command->body) { - $message = \Yii::$app->mailer->compose($command->view, $command->params); - } else { - $message = new Message(); - if ($command->isHtml()) { - $message->setHtmlBody($command->body); - } else { - $message->setTextBody($command->body); - } - } - $message->setFrom($command->from); - $message->setTo($command->to ?: \Yii::$app->params['robotEmail']); - $message->setSubject($command->subject); - return $message->send(); - } -} diff --git a/common/config/base.php b/common/config/base.php index 32bc9461a..6b034838e 100755 --- a/common/config/base.php +++ b/common/config/base.php @@ -22,12 +22,13 @@ ], 'commandBus' => [ - 'class' => '\trntv\tactician\Tactician', - 'commandNameExtractor' => '\League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor', - 'methodNameInflector' => '\League\Tactician\Handler\MethodNameInflector\HandleInflector', - 'commandToHandlerMap' => [ - 'common\commands\command\SendEmailCommand' => '\common\commands\handler\SendEmailHandler', - 'common\commands\command\AddToTimelineCommand' => '\common\commands\handler\AddToTimelineHandler', + 'class' => 'trntv\bus\CommandBus', + 'middlewares' => [ + [ + 'class' => '\trntv\bus\middlewares\BackgroundCommandMiddleware', + 'backgroundHandlerPath' => '@console/yii', + 'backgroundHandlerRoute' => 'command-bus/handle', + ] ] ], diff --git a/common/config/web.php b/common/config/web.php index a9d6ec468..00743d555 100755 --- a/common/config/web.php +++ b/common/config/web.php @@ -17,13 +17,13 @@ $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\Module', - 'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1'], + 'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1', '172.17.0.1'], ]; } if (YII_ENV_DEV) { $config['modules']['gii'] = [ - 'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1'], + 'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1', '172.17.0.1'], ]; } diff --git a/common/models/User.php b/common/models/User.php index 2d50929fe..4d1c7237c 100755 --- a/common/models/User.php +++ b/common/models/User.php @@ -2,7 +2,7 @@ namespace common\models; use cheatsheet\Time; -use common\commands\command\AddToTimelineCommand; +use common\commands\AddToTimelineCommand; use Yii; use yii\behaviors\AttributeBehavior; use yii\behaviors\TimestampBehavior; diff --git a/composer.json b/composer.json index a57421ff0..93cf6a8fc 100755 --- a/composer.json +++ b/composer.json @@ -38,8 +38,8 @@ "trntv/yii2-file-kit": "^1.0.0", "trntv/yii2-glide": "^1.0.0", "trntv/yii2-datetime-widget": "^1.0.0", - "trntv/yii2-tactician": "^0.1.0", "trntv/cheatsheet": "^0.1@dev", + "trntv/yii2-command-bus": "^1.0", "intervention/image": "^2.1", "vlucas/phpdotenv": "^2.0", "league/glide": "^0.3", @@ -47,7 +47,8 @@ "bower-asset/font-awesome": "^4.0", "bower-asset/html5shiv": "^3.0", "bower-asset/jquery-slimscroll": "^1.3", - "bower-asset/flot": "^0.8" + "bower-asset/flot": "^0.8", + "symfony/process": "^3.0" }, "require-dev": { "yiisoft/yii2-debug": "^2.0.0", @@ -57,7 +58,6 @@ "codeception/codeception": "^2.0" }, "suggest": { - "trntv/yii2-deploy": "dev-master@dev", "trntv/yii2-debug-xhprof": "dev-master@dev" }, "config": { diff --git a/console/config/console.php b/console/config/console.php index 7dca1478c..18eabe813 100755 --- a/console/config/console.php +++ b/console/config/console.php @@ -3,19 +3,22 @@ 'id' => 'console', 'basePath' => dirname(__DIR__), 'controllerNamespace' => 'console\controllers', - 'controllerMap'=>[ - 'message'=>[ - 'class'=>'console\controllers\ExtendedMessageController' + 'controllerMap' => [ + 'command-bus' => [ + 'class' => 'trnv\bus\console\BackgroundBusController', ], - 'migrate'=>[ - 'class'=>'yii\console\controllers\MigrateController', - 'migrationPath'=>'@common/migrations/db', - 'migrationTable'=>'{{%system_db_migration}}' + 'message' => [ + 'class' => 'console\controllers\ExtendedMessageController' ], - 'rbac-migrate'=>[ - 'class'=>'console\controllers\RbacMigrateController', - 'migrationPath'=>'@common/migrations/rbac/', - 'migrationTable'=>'{{%system_rbac_migration}}', + 'migrate' => [ + 'class' => 'yii\console\controllers\MigrateController', + 'migrationPath' => '@common/migrations/db', + 'migrationTable' => '{{%system_db_migration}}' + ], + 'rbac-migrate' => [ + 'class' => 'console\controllers\RbacMigrateController', + 'migrationPath' => '@common/migrations/rbac/', + 'migrationTable' => '{{%system_rbac_migration}}', 'templateFile' => '@common/rbac/views/migration.php' ], ], diff --git a/frontend/modules/user/controllers/SignInController.php b/frontend/modules/user/controllers/SignInController.php index 55923ccf4..66a0b4c6e 100755 --- a/frontend/modules/user/controllers/SignInController.php +++ b/frontend/modules/user/controllers/SignInController.php @@ -2,7 +2,7 @@ namespace frontend\modules\user\controllers; -use common\commands\command\SendEmailCommand; +use common\commands\SendEmailCommand; use common\models\User; use frontend\modules\user\models\LoginForm; use frontend\modules\user\models\PasswordResetRequestForm; diff --git a/frontend/modules/user/models/PasswordResetRequestForm.php b/frontend/modules/user/models/PasswordResetRequestForm.php index 26b19707e..a45fdc70a 100755 --- a/frontend/modules/user/models/PasswordResetRequestForm.php +++ b/frontend/modules/user/models/PasswordResetRequestForm.php @@ -1,7 +1,7 @@