1 | <?php |
||
32 | class Tracker |
||
33 | { |
||
34 | const UNKNOWN = -1; |
||
35 | |||
36 | const NOT_STARTED = 0; |
||
37 | const RUNNING = 1; |
||
38 | const FINISHED = 2; |
||
39 | const ABORTED = 3; |
||
40 | |||
41 | /** |
||
42 | * @var EventDispatcherInterface |
||
43 | */ |
||
44 | private $dispatcher; |
||
45 | |||
46 | /** |
||
47 | * @var int The number of total item (-1 for infinite/unknown) |
||
48 | */ |
||
49 | private $numTotalItems; |
||
50 | |||
51 | /** |
||
52 | * @var array Array holding processed items by Tick type |
||
53 | */ |
||
54 | private $numProcessedItems; |
||
55 | |||
56 | /** |
||
57 | * @var float The start time (a float value) |
||
58 | */ |
||
59 | private $startTime; |
||
60 | |||
61 | /** |
||
62 | * @var Tick Holds the last tick |
||
63 | */ |
||
64 | private $lastTick; |
||
65 | |||
66 | /** |
||
67 | * @var int One of the constants defined above |
||
68 | */ |
||
69 | private $status; |
||
70 | |||
71 | /** |
||
72 | * Build a task tracker using a list of subscribers |
||
73 | * |
||
74 | * This is an alternate constructor, to be used when constructing a |
||
75 | * Tracker object using a collection of subscribers |
||
76 | * |
||
77 | * @param array|EventSubscriberInterface[] $subscribers |
||
78 | * @param int $totalItems |
||
79 | * @return static |
||
80 | */ |
||
81 | public static function build(array $subscribers = [], $totalItems = self::UNKNOWN) |
||
94 | |||
95 | /** |
||
96 | * Constructor |
||
97 | * |
||
98 | * @param EventDispatcherInterface $dispatcher |
||
99 | * @param int $totalItems Default is unknown (-1) |
||
100 | */ |
||
101 | public function __construct($totalItems = self::UNKNOWN, EventDispatcherInterface $dispatcher = null) |
||
108 | |||
109 | /** |
||
110 | * Iterate over items and track them |
||
111 | * |
||
112 | * @param \Traversable $items |
||
113 | * @param callable $itemCallback Callback accepts arguments: (Tracker $tracker, $item) |
||
114 | * @return Report The final report |
||
115 | */ |
||
116 | public function run(\Traversable $items, callable $itemCallback) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @return EventDispatcherInterface |
||
128 | */ |
||
129 | public function getDispatcher() |
||
133 | |||
134 | /** |
||
135 | * Add a subscriber to this Tracker instance |
||
136 | * |
||
137 | * @param EventSubscriberInterface $subscriber |
||
138 | */ |
||
139 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
143 | |||
144 | /** |
||
145 | * Get number of total items to be processed (-1 for unknown) |
||
146 | * |
||
147 | * @return int |
||
148 | */ |
||
149 | public function getNumTotalItems() |
||
153 | |||
154 | /** |
||
155 | * Return the number of items processed, including failed/succeeded |
||
156 | * |
||
157 | * @param int $tickType Tick::SUCCESS, Tick::FAIL, Tick::SKIP, or null for all |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getNumProcessedItems($tickType = null) |
||
171 | |||
172 | /** |
||
173 | * Get the start time in microseconds (returns NULL if not yet started) |
||
174 | * |
||
175 | * @return float|null |
||
176 | */ |
||
177 | public function getStartTime() |
||
181 | |||
182 | /** |
||
183 | * Get the last report |
||
184 | * |
||
185 | * Returns null if not started |
||
186 | * |
||
187 | * @return Report|null |
||
188 | */ |
||
189 | public function getLastTick() |
||
193 | |||
194 | /** |
||
195 | * Get the status |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | public function getStatus() |
||
203 | |||
204 | /** |
||
205 | * Is the tracker running? |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function isRunning() |
||
213 | |||
214 | /** |
||
215 | * Start processing |
||
216 | * |
||
217 | * If this method is not called explicitely, it will automatically |
||
218 | * be called upon first tick |
||
219 | * |
||
220 | * @param string $msg Optional message to include |
||
221 | * @param array $extraInfo |
||
222 | */ |
||
223 | public function start($msg = null, array $extraInfo = []) |
||
236 | |||
237 | /** |
||
238 | * Indicate progress to the tracker |
||
239 | * |
||
240 | * Builds a report and send it to the tick method in the output handlers |
||
241 | * |
||
242 | * @param int $status SUCCESS (default), SKIP, or FAIL |
||
243 | * @param string $msg Message to include for this report |
||
244 | * @param array $extraInfo |
||
245 | * @param int $incrementBy |
||
246 | * @return Report |
||
247 | */ |
||
248 | public function tick($status = Tick::SUCCESS, $msg = null, array $extraInfo = [], $incrementBy = 1) |
||
268 | |||
269 | /** |
||
270 | * Finish processing |
||
271 | * |
||
272 | * Builds a report and sends it to the finish method in the output handlers |
||
273 | * |
||
274 | * @param string $msg Optional message to include |
||
275 | * @param array $extraInfo |
||
276 | * @return Report |
||
277 | */ |
||
278 | public function finish($msg = null, array $extraInfo = []) |
||
292 | |||
293 | /** |
||
294 | * Abort processing |
||
295 | * |
||
296 | * Builds a reports and send it to the abort method in the output handlers |
||
297 | * |
||
298 | * @param string $msg Optional message to include |
||
299 | * @param array $extraInfo |
||
300 | * @return Report |
||
301 | */ |
||
302 | public function abort($msg = null, array $extraInfo = []) |
||
316 | } |
||
317 |