Example #1
0
 /**
  * @inheritdoc
  *
  * @return bool
  */
 public function tick()
 {
     $this->server->tick();
     if ($this->manager->tick()) {
         return true;
     }
     $this->server->tick();
     if (empty($this->waiting)) {
         return false;
     }
     $next = array_shift($this->waiting);
     if ($next["type"] === "parallel") {
         $client = $this->getClient();
         $tasks = $next["tasks"];
         $tasks = array_map(function ($task) use($client) {
             if ($task instanceof Task) {
                 return $task;
             }
             if ($task instanceof Closure) {
                 $task = new ProcessCallbackTask($task);
             }
             if ($task instanceof Task) {
                 return new DoormanTask($task, $client);
             }
             return $task;
         }, $tasks);
         $tasks = array_filter($tasks, function ($task) {
             return $task instanceof Task;
         });
         $this->manager->addTaskGroup($tasks);
     }
     if ($next["type"] === "synchronous") {
         foreach ($next["tasks"] as $task) {
             if ($task instanceof Task) {
                 $handler = $task->getHandler();
                 $object = new $handler();
                 if ($object instanceof Handler) {
                     $object = new DoormanHandler($object);
                     $object->handle($task);
                 }
             }
         }
     }
     return true;
 }
Example #2
0
<?php

use AsyncPHP\Assistant\Handler\DoormanHandler;
use AsyncPHP\Doorman\Handler;
use AsyncPHP\Doorman\Task;
$paths = array(__DIR__ . "/../../../autoload.php", __DIR__ . "/../vendor/autoload.php");
foreach ($paths as $path) {
    if (file_exists($path)) {
        require $path;
    }
}
if (count($argv) < 2) {
    throw new InvalidArgumentException("Invalid call");
}
$script = array_shift($argv);
$task = array_shift($argv);
/**
 * We must account for the input data being malformed. That's why we use "@".
 */
$task = @unserialize(base64_decode($task));
if ($task instanceof Task) {
    $handler = $task->getHandler();
    $object = new $handler();
    if ($object instanceof Handler) {
        $object = new DoormanHandler($object);
        $object->handle($task);
    }
}