1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Task\Handler\TaskHandlerFactoryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Run pending tasks. |
22
|
|
|
*/ |
23
|
|
|
class RunHandlerCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TaskHandlerFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $handlerFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @param TaskHandlerFactoryInterface $handlerFactory |
33
|
|
|
*/ |
34
|
|
|
public function __construct($name, TaskHandlerFactoryInterface $handlerFactory) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($name); |
37
|
|
|
|
38
|
|
|
$this->handlerFactory = $handlerFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this->setDescription('Run pending tasks') |
47
|
|
|
->addArgument('handlerClass', InputArgument::REQUIRED) |
48
|
|
|
->addArgument('workload', InputArgument::OPTIONAL); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
55
|
|
|
{ |
56
|
|
|
$handlerClass = $input->getArgument('handlerClass'); |
57
|
|
|
$workload = $input->getArgument('workload'); |
58
|
|
|
|
59
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
60
|
|
|
$output->writeln(sprintf('Run command "%s" with workload "%s"', $handlerClass, $workload)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$result = $this->handlerFactory->create($handlerClass)->handle($workload); |
64
|
|
|
|
65
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
66
|
|
|
$output->writeln(sprintf('Result: %s', json_encode($result))); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|