Exemple #1
0
 /**
  * @coroutine
  *
  * @param string $data
  * @param float|int $timeout
  * @param bool $end
  *
  * @return \Generator
  *
  * @resolve int Number of bytes written to the file.
  *
  * @throws \Icicle\Stream\Exception\UnwritableException
  */
 protected function send(string $data, float $timeout, bool $end = false) : \Generator
 {
     if (!$this->isWritable()) {
         throw new UnwritableException('The file is no longer writable.');
     }
     if ($this->queue->isEmpty()) {
         $awaitable = $this->push($data);
     } else {
         $awaitable = $this->queue->top();
         $awaitable = $awaitable->then(function () use($data) {
             return $this->push($data);
         });
     }
     $this->queue->push($awaitable);
     if ($end) {
         $this->writable = false;
     }
     if ($timeout) {
         $awaitable = $awaitable->timeout($timeout);
     }
     $this->poll->listen();
     try {
         $result = (yield $awaitable);
     } catch (\Exception $exception) {
         $this->close();
         throw $exception;
     } finally {
         if ($end) {
             $this->close();
         }
         $this->poll->done();
     }
     return $result;
 }
Exemple #2
0
 /**
  * @coroutine
  *
  * @param string $data
  * @param bool $end
  *
  * @return \Generator
  *
  * @resolve int
  *
  * @throws \Icicle\File\Exception\FileException
  * @throws \Icicle\Stream\Exception\UnwritableException
  */
 protected function send(string $data, float $timeout, bool $end = false) : \Generator
 {
     if (!$this->isWritable()) {
         throw new UnwritableException('The file is no longer writable.');
     }
     $task = new Internal\FileTask('fwrite', [$data], $this->id);
     if ($this->queue->isEmpty()) {
         $awaitable = new Coroutine($this->worker->enqueue($task));
         $this->queue->push($awaitable);
     } else {
         $awaitable = $this->queue->top();
         $awaitable = $awaitable->then(function () use($task) {
             return new Coroutine($this->worker->enqueue($task));
         });
     }
     if ($end) {
         $this->writable = false;
     }
     if ($timeout) {
         $awaitable = $awaitable->timeout($timeout);
     }
     try {
         $written = (yield $awaitable);
         if ($this->append) {
             $this->size += $written;
         } else {
             $this->position += $written;
             if ($this->position > $this->size) {
                 $this->size = $this->position;
             }
         }
     } catch (TaskException $exception) {
         $this->close();
         throw new FileException('Write to the file failed.', $exception);
     } finally {
         if ($end) {
             $this->close();
         }
     }
     return $written;
 }
Exemple #3
0
 /**
  * Add page to breadcrumb queue
  *
  * @uses $_[SESSION]
  * @uses session_id()
  * @uses Wakka::GetConfigValue()
  * @uses Config::$num_breadcrumb_nodes
  * @uses Config::$enable_breadcrumbs
  */
 function AddBreadcrumb($page)
 {
     if (0 != $this->GetConfigValue('enable_breadcrumbs')) {
         if (isset($_SESSION['breadcrumbs'])) {
             $q = new SplQueue();
             $q->unserialize($_SESSION['breadcrumbs']);
             if ($page != $q->top()) {
                 while ($q->count() >= $this->GetConfigValue('num_breadcrumb_nodes')) {
                     $q->dequeue();
                 }
                 $q->enqueue($page);
                 $_SESSION['breadcrumbs'] = $q->serialize();
             }
         } else {
             if (isset($_SESSION['user'])) {
                 $q = new SplQueue();
                 $q->enqueue($page);
                 $_SESSION['breadcrumbs'] = $q->serialize();
             }
         }
     }
 }
Exemple #4
0
 /**
  * Advance the internal collection pointer, and return the current value.
  *
  * @return  void
  */
 public function next()
 {
     $this->_current = null;
     if (0 === count($this->_stack)) {
         return;
     }
     while (0 === $this->_pop->top()) {
         $this->_pop->pop();
         $this->_path->pop();
         $this->_pop->push($this->_pop->pop() - 1);
     }
     list($behavior, $state) = array_values($this->_stack->pop());
     switch ($state) {
         case static::STATE_REQUIRES:
             ++$this->_key;
             if (true === $behavior->clauseExists('requires')) {
                 $this->_current = $behavior->getClause('requires');
                 $this->_path->push($this->_current);
             } else {
                 $this->_current = true;
                 $this->_path->push(null);
             }
             if (true === $behavior->clauseExists('behavior')) {
                 $behaviors = $behavior->getClause('behavior')->getIterator();
                 $this->_stack->push(['behavior' => $behavior, 'state' => static::STATE_BEHAVIOR]);
                 $this->_stack->push(['behavior' => $behaviors, 'state' => static::STATE_BEHAVIOR]);
                 $this->_pop->push(count($behaviors) + 2 * $behavior->clauseExists('default'));
             } else {
                 $this->_stack->push(['behavior' => $behavior, 'state' => static::STATE_ENSURES]);
                 $this->_pop->push(2);
                 $this->next();
             }
             break;
         case static::STATE_BEHAVIOR:
             if (true === $behavior->valid()) {
                 $this->_stack->push(['behavior' => $behavior, 'state' => static::STATE_BEHAVIOR]);
                 $this->_stack->push(['behavior' => $behavior->current(), 'state' => static::STATE_REQUIRES]);
                 $behavior->next();
                 $this->next();
                 break;
             }
             list($parentBehavior, ) = array_values($this->_stack->pop());
             if (true === $parentBehavior->clauseExists('default')) {
                 $this->_stack->push(['behavior' => $parentBehavior->getClause('default'), 'state' => static::STATE_ENSURES]);
             }
             $this->next();
             break;
         case static::STATE_ENSURES:
             $this->_stack->push(['behavior' => $behavior, 'state' => static::STATE_THROWABLE]);
             if (false === $behavior->clauseExists('ensures') || 0 === (Coverage::CRITERIA_NORMAL & $this->getCriteria())) {
                 $this->_pop->push($this->_pop->pop() - 1);
                 $this->next();
                 break;
             }
             ++$this->_key;
             $this->_current = $behavior->getClause('ensures');
             $this->_path->push($this->_current);
             $this->_pop->push(0);
             break;
         case static::STATE_THROWABLE:
             if (false === $behavior->clauseExists('throwable') || 0 === (Coverage::CRITERIA_EXCEPTIONAL & $this->getCriteria())) {
                 $this->_pop->push($this->_pop->pop() - 1);
                 $this->next();
                 break;
             }
             ++$this->_key;
             $this->_current = $behavior->getClause('throwable');
             $this->_path->push($this->_current);
             $this->_pop->push(0);
             break;
     }
     return;
 }
<?php

/**
 * Queue implementation using SplQueue class.
 *
 * Copyright (c) 2012 Akeda Bagus (admin@gedex.web.id).
 *
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * license.
 */
$queue = new SplQueue();
$queue->enqueue("Element");
$queue->enqueue(23);
$queue->enqueue(array(3, 4, 6));
while (!$queue->isEmpty()) {
    var_dump($queue->dequeue());
}
if ($queue->isEmpty()) {
    // Element can be any type
    $queue->enqueue(new SplStack());
}
// Throws RuntimeException if the queue is empty
var_dump($queue->top());
Exemple #6
0
} catch (RuntimeException $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
try {
    $queue->shift();
} catch (RuntimeException $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
// data consistency
$a = 2;
$queue->enqueue($a);
echo $queue->dequeue() . "\n";
// peakable
$queue->enqueue(1);
$queue->enqueue(2);
echo $queue->top() . "\n";
// iterable
foreach ($queue as $elem) {
    echo "[{$elem}]\n";
}
// countable
$queue->enqueue(NULL);
$queue->enqueue(NULL);
echo count($queue) . "\n";
echo $queue->count() . "\n";
var_dump($queue->dequeue());
var_dump($queue->dequeue());
// clonable
$queue->enqueue(2);
$queue_clone = clone $queue;
$queue_clone->dequeue();
Exemple #7
0
 /**
  * peek at top of queue
  *
  * @return mixed
  */
 public function peekTop()
 {
     return $this->queue->top();
 }