1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Bldr.io |
5
|
|
|
* |
6
|
|
|
* (c) Aaron Scherer <[email protected]> |
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 Bldr\Block\Core\Service; |
13
|
|
|
|
14
|
|
|
use Bldr\Block\Core\Task\AbstractTask; |
15
|
|
|
use Bldr\Definition\JobDefinition; |
16
|
|
|
use Bldr\Definition\TaskDefinition; |
17
|
|
|
use Bldr\Event; |
18
|
|
|
use Bldr\Event\PreTaskEvent; |
19
|
|
|
use Bldr\Exception\TaskRuntimeException; |
20
|
|
|
use Bldr\Registry\JobRegistry; |
21
|
|
|
use Bldr\Registry\TaskRegistry; |
22
|
|
|
use Bldr\Task\TaskInterface; |
23
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Symfony\Component\Console\Helper\FormatterHelper; |
26
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Aaron Scherer <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Builder |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var EventDispatcherInterface $dispatcher |
35
|
|
|
*/ |
36
|
|
|
private $dispatcher; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var OutputInterface $output |
40
|
|
|
*/ |
41
|
|
|
private $output; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @type HelperSet |
45
|
|
|
*/ |
46
|
|
|
private $helperSet; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @type TaskRegistry $taskRegistry |
50
|
|
|
*/ |
51
|
|
|
private $taskRegistry; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param EventDispatcherInterface $dispatcher |
55
|
|
|
* @param OutputInterface $output |
56
|
|
|
* @param HelperSet $helperSet |
57
|
|
|
* @param TaskRegistry $taskRegistry |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
EventDispatcherInterface $dispatcher, |
61
|
|
|
OutputInterface $output, |
62
|
|
|
HelperSet $helperSet, |
63
|
|
|
TaskRegistry $taskRegistry |
64
|
|
|
) { |
65
|
|
|
$this->dispatcher = $dispatcher; |
66
|
|
|
$this->output = $output; |
67
|
|
|
$this->helperSet = $helperSet; |
68
|
|
|
$this->taskRegistry = $taskRegistry; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param JobRegistry $registry |
73
|
|
|
*/ |
74
|
|
|
public function runJobs(JobRegistry $registry) |
75
|
|
|
{ |
76
|
|
|
while ($registry->count() > 0) { |
77
|
|
|
$job = $registry->getNewJob(); |
78
|
|
|
|
79
|
|
|
$this->runJob($job); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$registry->markJobComplete($job); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param JobDefinition $job |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
public function runJob(JobDefinition $job) |
91
|
|
|
{ |
92
|
|
|
$this->output->writeln( |
93
|
|
|
[ |
94
|
|
|
"\n", |
95
|
|
|
sprintf( |
96
|
|
|
'<info>Running the %s job</info><comment>%s</comment>', |
97
|
|
|
$job->getName(), |
98
|
|
|
$job->getDescription() !== '' ? " > ".$job->getDescription() : '' |
99
|
|
|
) |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
foreach ($job->getTasks() as $task) { |
104
|
|
|
$preTaskEvent = new PreTaskEvent($job, $task); |
105
|
|
|
$this->dispatcher->dispatch(Event::PRE_TASK, $preTaskEvent); |
106
|
|
|
|
107
|
|
|
if ($preTaskEvent->isPropagationStopped()) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$service = $this->taskRegistry->findTaskByType($task->getType()); |
112
|
|
|
$this->prepareServiceForTask($service, $task); |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
$service->run($this->output); |
116
|
|
|
} catch (TaskRuntimeException $e) { |
117
|
|
|
if (!$task->continueOnError()) { |
118
|
|
|
throw $e; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @var FormatterHelper $formatter */ |
122
|
|
|
$formatter = $this->helperSet->get('formatter'); |
123
|
|
|
$this->output->writeln( |
124
|
|
|
[ |
125
|
|
|
'', |
126
|
|
|
$formatter->formatBlock( |
127
|
|
|
[ |
128
|
|
|
sprintf('[Warning] Task: %s', $task->getType()), |
129
|
|
|
sprintf('%s', $e->getMessage()) |
130
|
|
|
], |
131
|
|
|
'bg=yellow;fg=black', |
132
|
|
|
true |
133
|
|
|
), |
134
|
|
|
'' |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param TaskInterface $service |
143
|
|
|
* @param TaskDefinition $task |
144
|
|
|
* |
145
|
|
|
* @return TaskInterface |
146
|
|
|
*/ |
147
|
|
|
private function prepareServiceForTask(TaskInterface $service, TaskDefinition $task) |
148
|
|
|
{ |
149
|
|
|
$this->dispatcher->dispatch(Event::PRE_INITIALIZE_TASK, new Event\PreInitializeTaskEvent($service)); |
150
|
|
|
|
151
|
|
|
if ($service instanceof AbstractTask) { |
152
|
|
|
$service->configure(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (method_exists($service, 'setEventDispatcher')) { |
156
|
|
|
$service->setEventDispatcher($this->dispatcher); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (method_exists($service, 'setHelperSet')) { |
160
|
|
|
$service->setHelperSet($this->helperSet); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
foreach ($task->getParameters() as $name => $value) { |
164
|
|
|
$service->setParameter($name, $value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (method_exists($service, 'validate')) { |
168
|
|
|
$service->validate(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $service; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.