Download the PHP package jlorente/yii2-command-handler without Composer
On this page you can find all versions of the php package jlorente/yii2-command-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jlorente/yii2-command-handler
More information about jlorente/yii2-command-handler
Files in jlorente/yii2-command-handler
Package yii2-command-handler
Short Description A Yii2 extension that provides the framework with the Command Design Pattern. Useful to create commands in a main process and execute them in another processes such as crons, console controllers, etc.
License MIT
Homepage https://github.com/jlorente/yii2-command-handler
Informations about the package yii2-command-handler
Yii2 Command Handler
A Yii2 extension that provides the framework with the Command Design Pattern. Useful to create commands in a main process and execute them in another processes such as crons, console controllers, etc.
Installation
To install, either run
or add
to the section of your composer.json
file and run the following
commands from your project directory.
The last command will create the table needed to handle the command list.
Basic Usage
First at all you must include the module in your console application in order to execute the command queue processor.
./console/config/main.php
Now you can begin to create your own commands.
Example
Imagine we want multiple emails to be sent to a group of users when the admin of this group clicks a button. The process could be tedious and would increase in time the more users are in the group so our admin will have to wait until the process is finished to continue navigating over the website.
Here, we can create a command and delegate the process to an another non locking process by writting only a few lines.
Receiver
First, the Group class MUST implement \jlorente\command\base\Receiver interface and the method that will send the email to all the group users.
Command
Now we have to create the command that will execute this method. Note that the command MUST implement the \jlorente\command\base\CommandInterface or extend one of its descendants provided along with the package in order to work (\jlorente\command\base\Command for classes that extend form \yii\base\Model and \jlorente\command\db\Command for classes that implement the \yii\db\ActiveRecordInterface).
CommandMapper
Now we will create the controller action that receives the user click, creates and puts the command in the command list.
Console Controller
The console controller will process the command list and execute the commands inside it in a queue mode. You can run the console controller by using the following shell command.
Where is the name you put in the module configuration of your config file and is an optional argument to limit the number of mappers to be processed in the execution.
Maybe you want to put the execution of the action in a cronjob or something similar to be run every minute or in the interval that you want.
/etc/crontab
Advanced Usage
Behavior
Along with the package comes a behavior that can be attached to your models that creates and maps command. Continuing with the previous example, we are going to send the email to the group of users when the group changes its state property instead of having a controller action that creates and maps the command.
By doing this, the GroupSendEmailCommand will be enqueued every time the state property changes. For more configuration params see the documentation in the CommandGeneratorBehavior class.
CommandProcessor
Maybe you want to run the command queue processor by your own instead of using the provided console controller.
You can achieve this by instantiating and running the CommandProcessor class.
You can set the the way that the command processor executes the commands by selecting between queue and stack mode. By default the command processor will handle the execution in queue mode.
The run method also accept two arguments. The first one is an integer that will limit the number of commands to be processed and the second is a boolean that indicates if the erroneous commands have to be restored in the queue to be processed another time. By default the processor will try to process all the commands in the list and will restore the erroneous ones.
Further considerations
Previously I said that commands for ActiveRecord receivers have to inherit from \jlorente\command\db\Command. That's not true at all and depends of the final behavior that you want to achieve. Commands for ActiveRecord receivers could inherit from \jlorente\command\base\Command too, but the receiver of the command will store the state it had when the command was created and not the one that it has when the command will be executed. Those commands that inherit from \jlorente\command\db\Command will always fetch the ActiveRecord object from the database instead of using a copy of the object with the properties that it had when the command was created.
License
Copyright © 2015 José Lorente Martín [email protected].
Licensed under the MIT license. See LICENSE.txt for details.