/** * @param callable[] $workers * * @return array */ public function values($workers) { Server::getInstance()->listen(); $threads = array_map(function (callable $worker) { return new CoverageCollectorThread(new SimpleWorker($worker)); }, $workers); $result = $this->fetch($threads); Server::getInstance()->close(); return $result; }
/** * @param Thread[] $threads * @return array */ protected function fetch(array $threads) { $count = count($threads); $results = []; $result = []; foreach ($threads as $thread) { $thread->start(); } $server = Server::getInstance(); while ($count) { if ($server->accept()) { $data = $server->getData(); $results[$data[0]] = $data[1]; $count--; } } foreach ($threads as $thread) { $thread->wait(); } foreach ($threads as $key => $thread) { $result[$key] = $results[$thread->getPid()]; } return $result; }