/** * Implements the when() method of the Promise interface. * * @param callable $onResolved */ public function when(callable $onResolved) { switch ($this->state) { case Awaitable::RESOLVED: try { $onResolved(null, ...$this->result); } catch (\Throwable $ex) { Loop::defer(function () use($ex) { throw $ex; }); } break; case Awaitable::FAILED: try { $onResolved($this->result); } catch (\Throwable $ex) { Loop::defer(function () use($ex) { throw $ex; }); } break; default: if (!$this->callbacks) { $this->callbacks = new \SplQueue(); $this->callbacks->setIteratorMode(\SplQueue::IT_MODE_FIFO | \SplQueue::IT_MODE_DELETE); } $this->callbacks->enqueue($onResolved); } }
public function __construct() { parent::__construct(); $this->scope = new EventEmitter(); $this->queue = new \SplQueue(); $this->queue->setIteratorMode(\SplQueue::IT_MODE_DELETE); }
public function __construct(\Iterator $it) { $this->it = $it; $this->statement = new \SplQueue(); $this->statement->setIteratorMode(\SplDoublyLinkedList::IT_MODE_DELETE); $this->blocks = new \SplStack(); }
/** * @param Zend_Log_Writer_Abstract $writer * @param int $flushPriority * @param int $limit - If greater than 0 will only buffer that many events */ public function __construct(Zend_Log_Writer_Abstract $writer, $flushPriority = Zend_Log::DEBUG, $limit = 0) { $this->writer = $writer; $this->flushPriority = $flushPriority; $this->limit = $limit; $this->buffer = new SplQueue(); $this->buffer->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE); $this->flushed = false; }
/** * Copies for the audits and audits_logs table into the elastic search storage. * * @return bool */ public function main() { $table = $this->loadModel('Audits'); $table->hasMany('AuditDeltas'); $table->schema()->columnType('created', 'string'); $map = []; $meta = []; $featureList = function ($element) { list($k, $v) = explode(':', $element); (yield $k => $v); }; if (!empty($this->params['type-map'])) { $map = explode(',', $this->params['type-map']); $map = collection($map)->unfold($featureList)->toArray(); } if (!empty($this->params['extra-meta'])) { $meta = explode(',', $this->params['extra-meta']); $meta = collection($meta)->unfold($featureList)->toArray(); } $from = (new Time($this->params['from']))->modify('midnight'); $until = (new Time($this->params['until']))->modify('23:59:59'); $currentId = null; $buffer = new \SplQueue(); $buffer->setIteratorMode(\SplDoublyLinkedList::IT_MODE_DELETE); $queue = new \SplQueue(); $queue->setIteratorMode(\SplDoublyLinkedList::IT_MODE_DELETE); $index = ConnectionManager::get('auditlog_elastic')->getConfig('index'); $eventsFormatter = function ($audit) use($index, $meta) { return $this->eventFormatter($audit, $index, $meta); }; $changesExtractor = [$this, 'changesExtractor']; $query = $table->find()->where(function ($exp) use($from, $until) { return $exp->between('Audits.created', $from, $until, 'datetime'); })->where(function ($exp) { if (!empty($this->params['exclude-models'])) { $exp->notIn('Audits.model', explode(',', $this->params['exclude-models'])); } if (!empty($this->params['models'])) { $exp->in('Audits.model', explode(',', $this->params['models'])); } return $exp; })->matching('AuditDeltas')->order(['Audits.created', 'AuditDeltas.audit_id'])->bufferResults(false)->hydrate(false)->unfold(function ($audit) use($buffer, &$currentId) { if ($currentId && $currentId !== $audit['id']) { (yield collection($buffer)->toList()); } $currentId = $audit['id']; $buffer->enqueue($audit); })->map($changesExtractor)->map($eventsFormatter)->unfold(function ($audit) use($queue) { $queue->enqueue($audit); if ($queue->count() >= 50) { (yield collection($queue)->toList()); } }); $query->each([$this, 'persistBulk']); // There are probably some un-yielded results, let's flush them $rest = collection(count($buffer) ? [collection($buffer)->toList()] : [])->map($changesExtractor)->map($eventsFormatter)->append($queue); $this->persistBulk($rest->toList()); return true; }
/** * {@inheritdoc} */ public function __construct($queue = null) { parent::setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO & \SplDoublyLinkedList::IT_MODE_DELETE); if (null !== $queue) { foreach ($queue as $item) { $this->enqueue($item); } } }
/** * Create a new uv loop. * * @param LoopConfig $config */ public function __construct(LoopConfig $config = null) { parent::__construct($config); // $this->loop = $loop ?? \uv_loop_new(); // TODO: Investigate why new uv loops cannot be deleted (segfault) and leak file descriptors... $this->loop = \uv_default_loop(); $this->dispose = new \SplQueue(); $this->dispose->setIteratorMode(\SplQueue::IT_MODE_FIFO | \SplQueue::IT_MODE_DELETE); $this->config->setHostResolver(new Resolver($this)); $this->config->setFilesystem(new Filesystem($this, $this->config->getFilesystemObserver())); if (\defined('PHP_ZTS') && \PHP_ZTS) { // TODO: Implement a pool based on uv_queue_work() if ZTS is enabled (unusable: "zend_mm_heap corrupted"). } $this->delayCallback = function ($event) { $watcher = $this->timerWatchers[(int) $event] ?? null; if ($watcher) { $this->info[Watcher::TYPE_DELAY]--; unset($this->timerWatchers[(int) $event], $this->watchers[$watcher->id], $this->enable[$watcher->id]); if ($watcher->referenced) { $this->watchersReferenced--; } $watcher->event = null; $this->dispose->enqueue($event); ($watcher->callback)($watcher->id, $watcher->data); } }; $this->repeatCallback = function ($event) { $watcher = $this->timerWatchers[(int) $event] ?? null; if ($watcher) { ($watcher->callback)($watcher->id, $watcher->data); } }; $this->streamCallback = function ($event, int $status, int $events) { $id = (int) $event; if (($events === 0 || $events & \UV::READABLE) && isset($this->readWatchers[$id])) { foreach ($this->readWatchers[$id] as $watcher) { ($watcher->callback)($watcher->id, $watcher->stream, $watcher->data); } } if (($events === 0 || $events & \UV::WRITABLE) && isset($this->writeWatchers[$id])) { foreach ($this->writeWatchers[$id] as $watcher) { ($watcher->callback)($watcher->id, $watcher->stream, $watcher->data); } } }; $this->signalCallback = function ($event, int $signo) { if (isset($this->signalWatchers[$signo])) { foreach ($this->signalWatchers[$signo] as $watcher) { ($watcher->callback)($watcher->id, $watcher->signo, $watcher->data); } } }; }
function iterate_queue_with_mode($mode) { $x = new SplQueue(); $x->setIteratorMode($mode); $x->push(1); $x->push(2); $x->push(3); foreach ($x as $y) { var_dump($y); } var_dump(count($x)); }
/** * Load memberOf for the current user - 1 LDAP query * * @param Manager $ldap * @param array $userData * @param array $allowedGroups * @return array (groupDn => groupName) */ protected function getUserMemberOf(Manager $ldap, array $userData, array $allowedGroups) { $memberOf = array(); $processed = array(); $iterator = new \SplQueue(); $iterator->setIteratorMode(\SplQueue::IT_MODE_DELETE); // Load membership $raw = $ldap->search(null, Utils::getUserLookup($userData['username']), true, array('memberof')); if (!$raw->current() instanceof Node) { return array(); } $attrs = $raw->current()->getAttributes(); if (!array_key_exists('memberOf', $attrs) || !$attrs['memberOf'] instanceof NodeAttribute) { return array(); } // Post process all groups into memberOf array, put them into iterator for inheritance loading foreach ($attrs['memberOf'] as $groupDn) { $memberOf[$groupDn] = Utils::loadNameFromDn($groupDn); if (isset($processed[$groupDn])) { continue; } $iterator->enqueue($groupDn); $processed[$groupDn] = true; } // Loop iterator & load all inherits while (!$iterator->isEmpty()) { $groupDn = $iterator->dequeue(); $inherits = $this->getGroupMemberOf($ldap, $groupDn); foreach ($inherits as $one) { /** @var Node $one */ if (isset($processed[$one->getDn()])) { continue; } $processed[$one->getDn()] = true; $iterator->enqueue($one->getDn()); $memberOf[$one->getDn()] = Utils::loadNameFromDn($one->getDn()); } } return $memberOf; }
<?php $queue = new SplQueue(); try { $queue->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO); } catch (Exception $e) { echo $e->getMessage(); }
/* SPL QUEUE */ $qoperatingsystems = new SplQueue(); $qoperatingsystems->push(array('Windows 7', 'desktop', 'NT', 56.11)); $qoperatingsystems->push(array('Windows XP', 'desktop', 'NT', 10.59)); $qoperatingsystems->push(array('Windows 8', 'desktop', 'NT', 2.88)); $qoperatingsystems->push(array('Windows 8.1', 'desktop', 'NT', 11.15)); $qoperatingsystems->push(array('Windows 10', 'desktop', 'NT', 9)); $qoperatingsystems->push(array('Windows Vista', 'desktop', 'NT', 0)); $qoperatingsystems->push(array('Mac OS X 11', 'desktop', 'Unix', 2.66)); $qoperatingsystems->push(array('Mac OS X 10', 'desktop', 'Unix', 2.45)); $qoperatingsystems->push(array('Linux Mint', 'desktop', 'Linux', 0)); $qoperatingsystems->push(array('Linux Debian', 'desktop', 'Linux', 0)); $qoperatingsystems->push(array('Android', 'mobile', 'Linux', 48.12)); $qoperatingsystems->push(array('iOS', 'mobile', 'Unix', 34.71)); echo "\nSPL QUEUE ARRAY FOR LOOP: FIFO\n"; $qoperatingsystems->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); for ($qoperatingsystems->rewind(); $qoperatingsystems->valid(); $qoperatingsystems->next()) { echo $qoperatingsystems->current()[0] . "\n"; echo $qoperatingsystems->current()[1] . "\n"; echo $qoperatingsystems->current()[2] . "\n"; echo $qoperatingsystems->current()[3] . "\n"; } /* ------------------BASIC OPERATIONS------------------ */ // SORT // BY LANGUAGE RANKING usort($nlanguages, function ($a, $b) { return $b[2] - $a[2]; }); // FILTER // BY DATABASE SIZE function serveronly($var)
<?php $queue = new SplQueue(); $queue->push(1); $queue->push(2); $queue->push(4); $queue->push(3); $queue->setIteratorMode(SplQueue::IT_MODE_DELETE); foreach ($queue as $k => $v) { printf("%s => %s\n", $k, $v); }
/** * Return n independent iterators from a single iterable, useful when you * dealing with no rewind iterator, but need to traverse more that 1 time. */ function tee($iterable, $n = 2) { $iter = iter($iterable); $deques = array_map(function ($_) { $queue = new \SplQueue(); $queue->setIteratorMode(\SplQueue::IT_MODE_DELETE); return $queue; }, \range(0, $n - 1)); return \array_map(function ($locdeque) use($iter, $deques) { while (true) { // in case the local deque is empty we will fecth data from // original iterator, and pass them to all dequeues. if ($locdeque->isEmpty()) { if (!$iter->valid()) { return; } $newval = $iter->current(); foreach ($deques as $d) { $d->enqueue($newval); } $iter->next(); } (yield $locdeque->dequeue()); } }, $deques); }
public function __construct() { $this->handlers = []; $this->signalQueue = new \SplQueue(); $this->signalQueue->setIteratorMode(\SplQueue::IT_MODE_DELETE); }
/** * executes the container a optional array may be passed to this method that keeps additional parameters * which will be passed after the actor as parameters to each entity call. * * @param array $executeArguments * @throws QueueException|FatalQueueException */ public function execute(array $executeArguments = []) { $priorities = array_combine(array_keys($this->items), array_column($this->items, 'priority')); $priorityStack = new \SplQueue(); $priorityStack->setIteratorMode(\SplQueue::IT_MODE_DELETE); array_map([$priorityStack, 'enqueue'], array_keys($priorities)); foreach ($priorityStack as $dequeuedItem) { if (is_int($priorities[$dequeuedItem])) { continue; } if (!array_key_exists(current($priorities[$dequeuedItem]), $priorities)) { throw new QueueException(sprintf('Foundry container item with name `%s` points to an unavailable container item with name `%s`', $dequeuedItem, current($priorities[$dequeuedItem]))); } if (current($priorities[$dequeuedItem]) === $dequeuedItem) { throw new QueueException(sprintf('Foundry container item with name `%s` points to `%s`, self-recursion detected', $dequeuedItem, current($priorities[$dequeuedItem]))); } if (is_array($priorities[current($priorities[$dequeuedItem])]) && current($priorities[current($priorities[$dequeuedItem])]) === $dequeuedItem) { throw new QueueException(sprintf('Foundry container item with name `%s` points to `%s` which points to `%s`, recursion detected', $dequeuedItem, current($priorities[$dequeuedItem]), current($priorities[current($priorities[$dequeuedItem])]))); } if (!is_int($priorities[current($priorities[$dequeuedItem])])) { $priorityStack->enqueue($dequeuedItem); continue; } if ('before' === key($priorities[$dequeuedItem])) { $priorities[$dequeuedItem] = $priorities[current($priorities[$dequeuedItem])] - 1; } else { if ('after' === key($priorities[$dequeuedItem])) { $priorities[$dequeuedItem] = $priorities[current($priorities[$dequeuedItem])] + 1; } } } asort($priorities); $priorityStack->setIteratorMode(\SplQueue::IT_MODE_KEEP); array_map([$priorityStack, 'enqueue'], array_keys($priorities)); array_unshift($executeArguments, new Actor($priorityStack)); foreach ($priorityStack as $current) { if (false !== $this->closureBinding && $this->items[$current]['callable'] instanceof \Closure) { $callable = call_user_func_array([$this->items[$current]['callable'], 'bindTo'], $this->closureBinding); } else { $callable = $this->items[$current]['callable']; } try { call_user_func_array($callable, $executeArguments); } catch (\LogicException $exception) { throw new QueueException('Callable logic execution failed', 0, $exception); } catch (\RuntimeException $exception) { throw new FatalQueueException('Callable logic ends in fatal state', 500, $exception); } } }