Example #1
0
function render($app, $page, $data = array())
{
    global $loop, $WPGLOBAL;
    $loop = new Loop();
    foreach ($data as $key => $value) {
        if ($key == 'posts') {
            $loop->setResponse($value);
        }
        if ($key == 'single_post') {
            $loop->setPosts(array($value));
        }
        if ($key != 'posts') {
            $WPGLOBAL[$key] = $value;
        }
    }
    // Optional helpers that theme developers can provide
    try {
        include_once views_dir() . '/functions.php';
    } catch (Exception $ex) {
    }
    $file_path = views_dir() . '/' . $page . '.php';
    if (file_exists($file_path)) {
        require $file_path;
    } else {
        not_found($app);
    }
}
 /**
  * Adds a Loop to the stack
  *
  * @param Loop $stackItem
  */
 protected static function addLoopStack(Loop $stackItem)
 {
     // Check stack for parent loop to register it with this loop
     if (count(static::$stack) > 0) {
         $stackItem->setParentLoop(last(static::$stack));
     }
     array_push(static::$stack, $stackItem);
 }
 function testRandom()
 {
     $l = new Loop('foo', 'bar', 'charlie');
     $arr = array('foo', 'bar', 'charlie');
     $this->true(in_array($l->rand(), $arr));
     $this->true(in_array($l->rand(), $arr));
     $this->true(in_array($l->rand(), $arr));
     $this->true(in_array($l->rand(), $arr));
 }
Example #4
0
 /**
  * Creates a new loop with the given array and adds it to the stack
  *
  * @param array $items The array that will be iterated
  * @return Loop
  */
 public function newLoop($items)
 {
     $loop = new Loop($items);
     // Check stack for parent loop to register it with this loop
     if (count($this->stack) > 0) {
         $loop->setParentLoop(last($this->stack));
     }
     array_push($this->stack, $loop);
     return $loop;
 }
Example #5
0
 public function testSwimdataLoop()
 {
     $Loop = new Loop(new Object(array(Object::STROKE => array(25, 20, 15, 20), Object::STROKETYPE => array(2, 2, 2, 2))));
     $Loop->nextStep();
     $this->assertEquals(20, $Loop->stroke());
     $this->assertEquals(2, $Loop->stroketype());
     $Loop->setStepSize(2);
     $Loop->nextStep();
     $this->assertEquals(20, $Loop->stroke());
     $this->assertEquals(2, $Loop->stroketype());
     $Loop->nextStep();
     $this->assertTrue($Loop->isAtEnd());
 }
Example #6
0
 /**
  * Initialize a new stream listener
  */
 public function __construct($stream)
 {
     $this->stream = $stream;
     stream_set_blocking($this->stream, 0);
     $meta = stream_get_meta_data($this->stream);
     if (substr($meta['mode'], -1) === '+') {
         $this->writable = true;
         $this->readable = true;
     } else {
         if (strpos($meta['mode'], 'r') !== false) {
             $this->readable = true;
         }
         if (strpos($meta['mode'], 'w') !== false) {
             $this->writable = true;
         }
     }
     if ($this->readable) {
         $this->ev_read = event_new();
         event_set($this->ev_read, $this->stream, EV_READ | EV_PERSIST, array($this, '_read'));
         Loop::attachEvent($this->ev_read);
     }
     if ($this->writable) {
         $this->ev_write = event_new();
         event_set($this->ev_write, $this->stream, EV_WRITE | EV_PERSIST, array($this, '_write'));
         Loop::attachEvent($this->ev_write);
     }
 }
Example #7
0
 function testGetInstance()
 {
     $loop = Loop::getInstance();
     $this->assertTrue($loop instanceof Loop);
     $loop2 = Loop::getInstance();
     $this->assertTrue($loop === $loop2);
 }
Example #8
0
 /**
  * Initialize a new stream listener
  */
 public function __construct($fn, $interval)
 {
     $this->fn = $fn;
     $this->interval = $interval;
     $this->event = event_timer_new();
     event_timer_set($this->event, array($this, 'tick'));
     Loop::attachEvent($this->event, $this->interval * 1000);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function when(callable $onResolved)
 {
     try {
         $onResolved(null, $this->value);
     } catch (\Throwable $exception) {
         Loop::defer(static function () use($exception) {
             throw $exception;
         });
     }
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public function reset()
 {
     parent::reset();
     $this->scheduler = new \SplPriorityQueue();
     $this->readStreams = [];
     $this->readWatchers = [];
     $this->writeStreams = [];
     $this->writeWatchers = [];
     $this->signalWatchers = [];
 }
Example #11
0
 /**
  * @param \Generator $generator
  */
 public function __construct(\Generator $generator)
 {
     $this->generator = $generator;
     /**
      * @param \Throwable|null $exception Exception to be thrown into the generator.
      * @param mixed $value Value to be sent into the generator.
      */
     $this->when = function ($exception, $value) {
         if ($this->depth > self::MAX_CONTINUATION_DEPTH) {
             // Defer continuation to avoid blowing up call stack.
             Loop::defer(function () use($exception, $value) {
                 ($this->when)($exception, $value);
             });
             return;
         }
         try {
             if ($exception) {
                 // Throw exception at current execution point.
                 $yielded = $this->generator->throw($exception);
             } else {
                 // Send the new value and execute to next yield statement.
                 $yielded = $this->generator->send($value);
             }
             if ($yielded instanceof Promise) {
                 ++$this->depth;
                 $yielded->when($this->when);
                 --$this->depth;
                 return;
             }
             if ($this->generator->valid()) {
                 $got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
                 throw new InvalidYieldError($this->generator, \sprintf("Unexpected yield (%s expected, got %s)", Promise::class, $got));
             }
             $this->resolve($this->generator->getReturn());
         } catch (\Throwable $exception) {
             $this->dispose($exception);
         }
     };
     try {
         $yielded = $this->generator->current();
         if ($yielded instanceof Promise) {
             ++$this->depth;
             $yielded->when($this->when);
             --$this->depth;
             return;
         }
         if ($this->generator->valid()) {
             $got = \is_object($yielded) ? \get_class($yielded) : \gettype($yielded);
             throw new InvalidYieldError($this->generator, \sprintf("Unexpected yield (%s expected, got %s)", Promise::class, $got));
         }
         $this->resolve($this->generator->getReturn());
     } catch (\Throwable $exception) {
         $this->dispose($exception);
     }
 }
Example #12
0
 /**
  * Initialize a new stream listener
  */
 public function __construct($stream)
 {
     $this->stream = $stream;
     stream_set_blocking($this->stream, 0);
     $this->event = event_buffer_new($this->stream, array($this, '_read'), array($this, '_write'), array($this, '_error'));
     Loop::attachBuffer($this);
     event_buffer_timeout_set($this->event, 2, 5);
     event_buffer_watermark_set($this->event, EV_READ, 0, 0xffffff);
     event_buffer_priority_set($this->event, 10);
     event_buffer_enable($this->event, EV_READ | EV_PERSIST);
 }
Example #13
0
 /**
  * Emits a value from the observable. The returned promise is resolved with the emitted value once all subscribers
  * have been invoked.
  *
  * @param mixed $value
  *
  * @return \Interop\Async\Promise
  *
  * @throws \Error If the observable has resolved.
  */
 private function emit($value) : Promise
 {
     if ($this->resolved) {
         throw new \Error("The observable has been resolved; cannot emit more values");
     }
     if ($value instanceof Promise) {
         $deferred = new Deferred();
         $value->when(function ($e, $v) use($deferred) {
             if ($this->resolved) {
                 $deferred->fail(new \Error("The observable was resolved before the promise result could be emitted"));
                 return;
             }
             if ($e) {
                 $this->fail($e);
                 $deferred->fail($e);
                 return;
             }
             $deferred->resolve($this->emit($v));
         });
         return $deferred->promise();
     }
     $promises = [];
     foreach ($this->subscribers as $onNext) {
         try {
             $result = $onNext($value);
             if ($result instanceof Promise) {
                 $promises[] = $result;
             }
         } catch (\Throwable $e) {
             Loop::defer(static function () use($e) {
                 throw $e;
             });
         }
     }
     if (!$promises) {
         return new Success($value);
     }
     $deferred = new Deferred();
     $count = \count($promises);
     $f = static function ($e) use($deferred, $value, &$count) {
         if ($e) {
             Loop::defer(static function () use($e) {
                 throw $e;
             });
         }
         if (!--$count) {
             $deferred->resolve($value);
         }
     };
     foreach ($promises as $promise) {
         $promise->when($f);
     }
     return $deferred->promise();
 }
function render($app, $page, $data = array())
{
    global $loop, $WPGLOBAL;
    $loop = new Loop();
    foreach ($data as $key => $value) {
        if ($key == 'posts') {
            $loop->setResponse($value);
        }
        if ($key == 'single_post') {
            $loop->setPosts(array($value));
        }
        if ($key != 'posts') {
            $WPGLOBAL[$key] = $value;
        }
    }
    $file_path = views_dir() . '/' . $page . '.php';
    if (file_exists($file_path)) {
        require $file_path;
    } else {
        not_found($app);
    }
}
Example #15
0
 public static function start($plane)
 {
     self::$plane = $plane;
     $time = 0;
     system("stty -icanon");
     while (true) {
         self::listen();
         $plane->step(0.2);
         $time += 0.2;
         self::draw($time);
         usleep(200000);
     }
 }
Example #16
0
 /**
  * @param mixed $value
  *
  * @throws \Error Thrown if the promise has already been resolved.
  */
 private function resolve($value = null)
 {
     if ($this->resolved) {
         throw new \Error("Promise has already been resolved");
     }
     $this->resolved = true;
     $this->result = $value;
     if ($this->onResolved === null) {
         return;
     }
     $onResolved = $this->onResolved;
     $this->onResolved = null;
     if ($this->result instanceof Promise) {
         $this->result->when($onResolved);
         return;
     }
     try {
         $onResolved(null, $this->result);
     } catch (\Throwable $exception) {
         Loop::defer(static function () use($exception) {
             throw $exception;
         });
     }
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function stop()
 {
     $this->loop->stop();
     parent::stop();
 }
Example #18
0
/**
 * Returns an observable that emits a value every $interval milliseconds after (up to $count times). The value emitted
 * is an integer of the number of times the observable emitted a value.
 *
 * @param int $interval Time interval between emitted values in milliseconds.
 * @param int $count Number of values to emit. PHP_INT_MAX by default.
 *
 * @return \Amp\Observable
 *
 * @throws \Error If the number of times to emit is not a positive value.
 */
function interval(int $interval, int $count = PHP_INT_MAX) : Observable
{
    if (0 >= $count) {
        throw new \Error("The number of times to emit must be a positive value");
    }
    $postponed = new Postponed();
    Loop::repeat($interval, function ($watcher) use(&$i, $postponed, $count) {
        $postponed->emit(++$i);
        if ($i === $count) {
            Loop::cancel($watcher);
            $postponed->resolve();
        }
    });
    return $postponed->observe();
}
Example #19
0
 public function addLoop(Loop $loop)
 {
     $this->loops[$loop->getName()] = $loop;
 }
Example #20
0
     self::$items->attach($e);
     return $e;
 }
 /**
  * Clear the specified interval
  */
 public static function clearInterval(Timer $timer)
 {
     self::$items->detach($timer);
     $timer->__destruct();
 }
 /**
  * Attach the specified callback on the specified descriptor
  * @return Event
  */
 public static function stop()
 {
     self::$run = false;
     event_base_loopexit(self::$base, 1000);
 }
 /**
  * Check if the loop is started
  */
 public static function isStarted()
 {
     return self::$run;
 }
 /**
  * Starts the event loop
  */
 public static function start()
Example #21
0
        if (!$this->levels->isEmpty()) {
            list($lastLevel, $lastIndex) = $this->levels->peek();
            $comma = ',';
        }
        $level = strlen($row[0]);
        if ($level == $lastLevel) {
            $index = $lastIndex + 1;
            $numClose++;
        } elseif ($level > $lastLevel) {
            $index = 1;
        } elseif ($level < $lastLevel) {
            $numClose = 1 + ($lastLevel - $level);
            // find where we last branched off
            do {
                list($lastLevel, $index) = $this->levels->pop();
            } while ($level != $lastLevel);
            $index++;
        }
        $pad = str_repeat("\t", $level);
        echo str_repeat(')', $numClose) . $comma . "\n{$pad}{$index}, new Hash(\n{$pad}\t'contents', '" . (isset($row[1]) ? str_replace('\'', '\\\'', $row[1]) : '') . "',\n{$pad}\t'uri', '" . (isset($row[2]) ? str_replace('\'', '\\\'', $row[2]) : '') . "',\n{$pad}\t'target', '" . (isset($row[4]) ? $row[4] : '_self') . "',\n{$pad}\t'statusText', '" . (isset($row[3]) ? str_replace('\'', '\\\'', $row[3]) : '') . "'";
        $this->levels->push(array($level, $index));
    }
    function finish($total)
    {
        list($lastLevel, ) = $this->levels->peek();
        $numClose = $lastLevel;
        echo str_repeat(')', $numClose) . "\n));";
    }
}
Loop::run(new DataFileIterator(new DataFile('layersmenu.txt', new DataFileReader(false))), new MenuConverter('domMenu_main'));
Example #22
0
<?php

function autoLoad($method)
{
    $method = str_replace('\\', '/', strtolower($method));
    chdir(__DIR__);
    require $method . '.php';
}
spl_autoload_register('autoLoad');
$plane = new Plane(0, 0, 1000, 100);
Loop::start($plane);
Example #23
0
@include('header')

	@loop
		<!-- BOOK PROMO -->
		<div id="bks-promo" style="background-color: {{ Meta::get(Loop::id(), 'color') }};">
			<div class="wrapper">
				<div class="promo-wrapper">
					<div class="promo-container">
						<h1>{{ Loop::title() }}</h1>
						<h5>By {{ Meta::get(Loop::id(), 'author') }}</h5>
						<a href="#" class="big-button">Buy book</a>
					</div>
					<div class="promo-media">
						<?php 
$image = wp_get_attachment_image_src(Meta::get(Loop::id(), 'promo-image'), 'book-promo');
?>
						<img src="{{ $image[0] }}" alt="Demo Book" width="399" height="435">
					</div>
				</div>
			</div>
		</div>
		<!-- END BOOK PROMO -->
		<!-- BOOK CONTENT -->
		<div class="wrapper">
			<div id="book--container">
				<div class="bks-title-box">
					<h1>Content</h1>
				</div>
				<div id="book--content">
					{{ Loop::content() }}
				</div>
Example #24
0
 /**
  * Here we add a new nextTick function as we're in the middle of a current
  * nextTick.
  */
 function testNextTickStacking()
 {
     $loop = new Loop();
     $check = 0;
     $loop->nextTick(function () use(&$check, $loop) {
         $loop->nextTick(function () use(&$check) {
             $check++;
         });
         $check++;
     });
     $loop->run();
     $this->assertEquals(2, $check);
 }
Example #25
0
 public function testRouteLoop()
 {
     $Loop = new Loop(new Object(array(Object::LATITUDES => array(47.7, 47.8, 47.9, 47.8, 47.7), Object::LONGITUDES => array(7.7, 7.8, 7.9, 7.8, 7.7))));
     $Loop->nextStep();
     $this->assertEquals(47.8, $Loop->latitude());
     $this->assertEquals(7.8, $Loop->longitude());
     $this->assertEquals(13.41, $Loop->calculatedStepDistance(), '', 0.2);
     $Loop->setStepSize(2);
     $Loop->nextStep();
     $this->assertEquals(47.8, $Loop->latitude());
     $this->assertEquals(7.8, $Loop->longitude());
     $this->assertEquals(0.0, $Loop->calculatedStepDistance());
     $Loop->nextStep();
     $this->assertTrue($Loop->isAtEnd());
 }
Example #26
0
 /**
  * {@inheritdoc}
  */
 public function stop()
 {
     $this->loop->stop(\Ev::BREAK_ONE);
     parent::stop();
 }
Example #27
0
File: Pause.php Project: amphp/amp
 /**
  * @param int $time Milliseconds before succeeding the promise.
  * @param mixed $value Succeed the promise with this value.
  */
 public function __construct(int $time, $value = null)
 {
     Loop::delay($time, function () use($value) {
         $this->resolve($value);
     });
 }
Example #28
0
 /**
  * Set up the default arguments for WP_Query.
  *
  * Pass in an array to override or extend the default arguments.
  * For example, pass an array to $override:
  * ```
  * [
  *   'post_status'            => array( 'publish' ),
  *   'posts_per_page'         => '-1',
  *   'order'                  => 'ASC',
  *   'orderby'                => 'menu_order',
  * ]
  * ```
  *
  * @since    1.0.0
  * @param array $override Array of WP_Query arguments
  */
 public function __construct($override = [], $term = null)
 {
     parent::__construct();
     $this->args = array_merge($this->args, array_merge(['post_type' => array('service')], $override));
 }
Example #29
0
 public function testDepth()
 {
     $syntax = new Syntax();
     $this->assertEquals(1, $syntax->depth());
     $rule = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node'));
     $this->assertEquals(1, $rule->depth());
     $syntax->addChild($rule);
     $this->assertEquals(2, $syntax->depth());
     $seq = new Sequence($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node'));
     $this->assertEquals(1, $seq->depth());
     $rule->addChild($seq);
     $this->assertEquals(2, $rule->depth());
     $this->assertEquals(3, $syntax->depth());
     $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node'));
     $this->assertEquals(1, $ident->depth());
     $seq->addChild($ident);
     $this->assertEquals(2, $seq->depth());
     $this->assertEquals(3, $rule->depth());
     $this->assertEquals(4, $syntax->depth());
     $loop = new Loop($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node'));
     $this->assertEquals(1, $loop->depth());
     $seq->addChild($loop);
     $this->assertEquals(2, $seq->depth());
     $this->assertEquals(3, $rule->depth());
     $this->assertEquals(4, $syntax->depth());
     $term = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node'));
     $this->assertEquals(1, $term->depth());
     $loop->addChild($term);
     $this->assertEquals(2, $loop->depth());
     $this->assertEquals(3, $seq->depth());
     $this->assertEquals(4, $rule->depth());
     $this->assertEquals(5, $syntax->depth());
 }
Example #30
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testInvalidMovingTo()
 {
     $Loop = new Loop(new Object());
     $Loop->moveToTime(100);
 }