Skip to content

Commit

Permalink
Список задач к выполнению формируется заранее
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Mar 13, 2019
1 parent 06ab52d commit 372403f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
49 changes: 34 additions & 15 deletions src/Crontask/TaskList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Crontask;

use Crontask\Interfaces\TaskInterface;
Expand Down Expand Up @@ -33,29 +34,51 @@ public function addTasks($tasks)
* @param TaskInterface $task
* @return TaskList $this
*/
public function addTask(TaskInterface $task)
public function addTask(TaskInterface $task): TaskList
{
$this->tasks[] = $task;

return $this;
}

/**
* Set tasks
*
* @param array $tasks
*/
public function setTasks($tasks)
{
$this->tasks = $tasks;
}

/**
* Get Tasks
*
* @return array
*/
public function getTasks()
public function getTasks(): array
{
return $this->tasks;
}

/**
* Get required tasks
*
* @return array
*/
public function getTasksRequired(): array
{
return array_filter($this->tasks, function (TaskInterface $task) {
return $task->isRequired();
});
}

/**
* Get Output
*
* @return array
*/
public function getOutput()
public function getOutput(): array
{
return $this->output;
}
Expand All @@ -65,21 +88,17 @@ public function getOutput()
*
* @return array
*/
public function run()
public function run(): array
{
$this->output = [];

foreach ($this->tasks as $task) {
if ($task->isRequired()) {

$result = $task->run();

$this->output[] = [
'task' => get_class($task),
'output' => $task->getOutput(),
'result' => $result,
];
}
foreach ($this->getTasksRequired() as $task) {
$result = $task->run();
$this->output[] = [
'task' => get_class($task),
'output' => $task->getOutput(),
'result' => $result,
];
}

return $this->output;
Expand Down
10 changes: 5 additions & 5 deletions src/Crontask/Tasks/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Shell extends Task
public function run()
{
$output = null;
exec($this->getCommand().' '.implode(' ', $this->arguments), $output, $result);
exec($this->getCommand() . ' ' . implode(' ', $this->arguments), $output, $result);

$this->setOutput($output);

Expand All @@ -31,7 +31,7 @@ public function run()
* @param string $command
* @return $this
*/
public function setCommand($command)
public function setCommand($command): self
{
$this->command = $command;

Expand All @@ -41,7 +41,7 @@ public function setCommand($command)
/**
* @return string
*/
public function getCommand()
public function getCommand(): string
{
return $this->command;
}
Expand All @@ -50,7 +50,7 @@ public function getCommand()
* @param mixed $argument
* @return $this
*/
public function addArgument($argument)
public function addArgument($argument): self
{
$this->arguments[] = $argument;

Expand All @@ -60,7 +60,7 @@ public function addArgument($argument)
/**
* @return array
*/
public function getArguments()
public function getArguments(): array
{
return $this->arguments;
}
Expand Down
13 changes: 9 additions & 4 deletions src/Crontask/Tasks/Task.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Crontask\Tasks;

use Cron\CronExpression;
Expand All @@ -23,10 +24,11 @@ abstract public function run();

/**
* Sets a cron expression
*
* @param string $expression
* @return Task $this
*/
public function setExpression($expression)
public function setExpression($expression): Task
{
$this->expression = $expression;

Expand All @@ -35,19 +37,21 @@ public function setExpression($expression)

/**
* Gets the current cron expression
*
* @return string
*/
public function getExpression()
public function getExpression(): string
{
return $this->expression;
}

/**
* Sets the output from the task
*
* @param null|string|array $output
* @return Task $this
*/
public function setOutput($output)
public function setOutput($output): Task
{
$this->output = $output;

Expand All @@ -56,6 +60,7 @@ public function setOutput($output)

/**
* Gets the output from the task
*
* @return null|string|array
*/
public function getOutput()
Expand All @@ -67,7 +72,7 @@ public function getOutput()
* Checks whether the task is currently due
* @return bool
*/
public function isRequired()
public function isRequired(): bool
{
$expression = $this->getExpression();

Expand Down

0 comments on commit 372403f

Please sign in to comment.