1. Go to this page and download the library: Download bonditka/yii2-task library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bonditka / yii2-task example snippets
//crontask
$tasks = Task::getTaskToRun();
foreach($tasks as $task){
do {
$arResult = $task->run();
if (!is_array($arResult) && helpers\ArrayHelper::keyExists('errors', $arResult)) {
if(is_array($arResult['errors'])){
$error = implode('. ', $arResult['errors']);
}
else{
$error = $arResult['errors'];
}
throw new yii\base\ErrorException('Задача выполнилась с ошибками: '.$error);
}
if(!helpers\ArrayHelper::keyExists('progress', $arResult) || !helpers\ArrayHelper::keyExists('nextStep', $arResult)){
throw new yii\base\ErrorException('Задача не вернула сигнала о завершении или переходе на следующий шаг');
}
$step = helpers\ArrayHelper::getValue($arResult, 'nextStep');
$task->setStep($step)->taskSave();
} while (!empty($arResult['nextStep']));
$task->complete()->taskSave();
}
//TaskInstance
use yii\base as yiiBase;
use yii\helpers;
class TaskInstance extends yiiBase\Model implements \bonditka\task\components\TaskInstance
{
public $step;
public function rules()
{
return [
[['step'], ' 'second' => [
'methodName' => 'mySecondAwesomeMethod',
'message' => 'myAwesomeMethod is done. Next step is third',
],
'third' => [
'methodName' => 'myThirdAwesomeMethod',
'message' => 'myAwesomeMethod is done. Task is done.',
]
];
/** @param array $arStep contains the steps of the task.
* Every step must contain
* 'methodName' — the name of the method of the current instance of the class that is currently running,
* optional* 'message' — description of actions of the current step for the user
*/
public function setSteps(array $arStep)
{
$this->arStep = $arStep;
}
public function getSteps(){
return $this->arStep;
}
/**
* @return integer between 0 and 100 of progress by current task
*/
public function getProgress()
{
$stepPosition = array_search($this->step, array_keys($this->arStep));
return ceil((($stepPosition + 1) / count($this->arStep)) * 100);
}
protected function getNextStep()
{
$keys = array_keys($this->arStep);
return isset($keys[array_search($this->step, $keys) + 1]) ? $keys[array_search($this->step, $keys) + 1] : null;
}
/**
* @param $param
*/
protected function myFirstAwesomeMethod($param)
{
//do something...
return true;
}
/**
* @param $param
*/
protected function mySecondAwesomeMethod($param)
{
//do something...
return true;
}
/**
* @param $param
*/
protected function myThirdAwesomeMethod($param)
{
//do something...
return true;
}
/**
* @param $param
* @return array $arResult
* @throws yiiBase\ErrorException
* @throws yiiBase\InvalidConfigException
* Основной метод пошагового выполнения задания
*/
public function executeStep($param)
{
if (empty($this->arStep)) {
throw new yiiBase\InvalidConfigException;
}
if (empty($this->step)) {
reset($this->arStep);
$this->step = key($this->arStep);
}
$methodResult = $this->{$this->arStep[$this->step]['methodName']}($param);
if($methodResult === false){
throw new yiiBase\ErrorException;
}
return [
'progress' => $this->getProgress(),
'nextStep' => $this->getNextStep(),
'result' => $methodResult
];
}
}
php composer.phar
bash
php yii migrate
bash
php tests/bin/yii migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.