1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Purple - Run tasks on collections |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Runner |
13
|
|
|
* @package Jnjxp\Purple |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.purple |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\Purple; |
21
|
|
|
|
22
|
|
|
use Countable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Runner |
26
|
|
|
* |
27
|
|
|
* @category Runner |
28
|
|
|
* @package Jnjxp\Purple |
29
|
|
|
* @author Jake Johns <[email protected]> |
30
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
31
|
|
|
* @link https://github.com/jnjxp/jnjxp.purple |
32
|
|
|
*/ |
33
|
|
|
class Runner implements Countable |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Resolver |
37
|
|
|
* |
38
|
|
|
* @var callable |
39
|
|
|
* |
40
|
|
|
* @access protected |
41
|
|
|
*/ |
42
|
|
|
protected $resolver; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Task factory |
46
|
|
|
* |
47
|
|
|
* @var callable |
48
|
|
|
* |
49
|
|
|
* @access protected |
50
|
|
|
*/ |
51
|
|
|
protected $taskFactory; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tasks |
55
|
|
|
* |
56
|
|
|
* @var QueueInterface |
57
|
|
|
* |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
|
protected $tasks; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* __construct |
64
|
|
|
* |
65
|
|
|
* @param callable $resolver DESCRIPTION |
66
|
|
|
* @param QueueInterface $queue DESCRIPTION |
67
|
|
|
* |
68
|
|
|
* @access public |
69
|
|
|
*/ |
70
|
3 |
|
public function __construct( |
71
|
2 |
|
callable $resolver = null, |
72
|
3 |
|
QueueInterface $queue = null |
73
|
|
|
) { |
74
|
3 |
|
$this->resolver = $resolver; |
75
|
|
|
$this->setTaskFactory([$this, 'newTask']); |
76
|
|
|
$this->tasks = $queue ?: new Queue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set task factory |
81
|
|
|
* |
82
|
|
|
* @param callable $factory factory to create a task |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
* |
86
|
|
|
* @access public |
87
|
|
|
*/ |
88
|
3 |
|
public function setTaskFactory(callable $factory) |
89
|
|
|
{ |
90
|
3 |
|
$this->taskFactory = $factory; |
91
|
3 |
|
return $this; |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add task |
96
|
|
|
* |
97
|
|
|
* @param mixed $task task to be added |
98
|
|
|
* @param int $priority priority of task |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
* |
102
|
|
|
* @access public |
103
|
|
|
*/ |
104
|
2 |
|
public function addTask($task, $priority = 1000) |
105
|
|
|
{ |
106
|
|
|
$this->tasks->insert($task, $priority); |
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add Tasks |
112
|
|
|
* |
113
|
|
|
* @param mixed $tasks tasks to add |
114
|
|
|
* @param int $priority priority of tasks |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
* |
118
|
|
|
* @access public |
119
|
|
|
*/ |
120
|
1 |
|
public function addTasks($tasks, $priority = 1000) |
121
|
|
|
{ |
122
|
|
|
foreach ($tasks as $task) { |
123
|
|
|
$this->addTask($task, $priority); |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set Tasks |
130
|
|
|
* |
131
|
|
|
* @param mixed $tasks Tasks to set |
132
|
|
|
* @param int $priority tasks priority |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
* |
136
|
|
|
* @access public |
137
|
|
|
*/ |
138
|
1 |
|
public function setTasks($tasks, $priority = 1000) |
139
|
|
|
{ |
140
|
|
|
$this->tasks->clear(); |
141
|
|
|
return $this->addTasks($tasks, $priority); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Task |
146
|
|
|
* |
147
|
|
|
* @param mixed $collection collection or collection spec |
148
|
|
|
* @param int $priority priority of task |
149
|
|
|
* |
150
|
|
|
* @return TaskInterface |
151
|
|
|
* |
152
|
|
|
* @access public |
153
|
|
|
*/ |
154
|
2 |
|
public function task($collection, $priority = 1000) |
155
|
|
|
{ |
156
|
|
|
$task = call_user_func($this->taskFactory, $collection); |
157
|
|
|
$this->addTask($task, $priority); |
158
|
2 |
|
return $task; |
159
|
2 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Execute all tasks |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
* |
166
|
|
|
* @access public |
167
|
|
|
*/ |
168
|
2 |
|
public function __invoke() |
169
|
|
|
{ |
170
|
2 |
|
foreach ($this->tasks as $spec) { |
171
|
|
|
$task = $this->resolve($spec); |
172
|
|
|
$this->runTask($task); |
173
|
|
|
} |
174
|
2 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Count |
178
|
|
|
* |
179
|
|
|
* @return int |
180
|
|
|
* |
181
|
|
|
* @access public |
182
|
|
|
*/ |
183
|
|
|
public function count() |
184
|
|
|
{ |
185
|
|
|
return count($this->tasks); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Run task |
190
|
|
|
* |
191
|
|
|
* @param TaskInterface $task task to run |
192
|
|
|
* |
193
|
|
|
* @return void |
194
|
|
|
* |
195
|
|
|
* @access protected |
196
|
|
|
*/ |
197
|
|
|
protected function runTask(TaskInterface $task) |
198
|
|
|
{ |
199
|
|
|
$collection = $this->resolve($task->getCollection()); |
200
|
|
|
|
201
|
|
|
// Item Tasks |
202
|
|
|
$itemTasks = $task->getItemTasks(); |
203
|
|
|
$this->runItemTasks($collection, $itemTasks); |
204
|
|
|
|
205
|
|
|
// Collection Tasks |
206
|
|
|
$collectionTasks = $task->getCollectionTasks(); |
207
|
|
|
$this->runCollectionTasks($collection, $collectionTasks); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Run item tasks |
212
|
|
|
* |
213
|
|
|
* @param mixed $items items on which to execute tasks |
214
|
|
|
* @param mixed $tasks tasks to execute |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
* |
218
|
|
|
* @access protected |
219
|
|
|
*/ |
220
|
2 |
|
protected function runItemTasks($items, $tasks) |
221
|
|
|
{ |
222
|
|
|
foreach ($tasks as $spec) { |
223
|
|
|
$task = $this->resolve($spec); |
224
|
|
|
foreach ($items as $key => $value) { |
225
|
|
|
$task($value, $key, $items); |
226
|
|
|
} |
227
|
|
|
} |
228
|
2 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Run collection tasks |
232
|
|
|
* |
233
|
|
|
* @param mixed $items collection on which to run tasks |
234
|
|
|
* @param mixed $tasks tasks to run |
235
|
|
|
* |
236
|
|
|
* @return void |
237
|
|
|
* |
238
|
|
|
* @access protected |
239
|
|
|
*/ |
240
|
2 |
|
protected function runCollectionTasks($items, $tasks) |
241
|
|
|
{ |
242
|
|
|
foreach ($tasks as $spec) { |
243
|
|
|
$task = $this->resolve($spec); |
244
|
|
|
$task($items); |
245
|
|
|
} |
246
|
2 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* New task |
250
|
|
|
* |
251
|
|
|
* @param mixed $collection collection or collection spec |
252
|
|
|
* |
253
|
|
|
* @return Task |
254
|
|
|
* |
255
|
|
|
* @access protected |
256
|
|
|
*/ |
257
|
|
|
protected function newTask($collection) |
258
|
|
|
{ |
259
|
|
|
return new Task( |
260
|
|
|
$collection, |
261
|
|
|
new Queue, |
262
|
|
|
new Queue |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Resolve |
268
|
|
|
* |
269
|
|
|
* @param mixed $spec spec to resolve |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
* |
273
|
|
|
* @access protected |
274
|
|
|
*/ |
275
|
|
|
protected function resolve($spec) |
276
|
|
|
{ |
277
|
|
|
if (! $this->resolver) { |
278
|
|
|
return $spec; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return call_user_func($this->resolver, $spec); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|