示例#1
0
 /**
  * Static method to create your threads from functions ...
  **/
 public static function call($method, $params)
 {
     $thread = new Async($method, $params);
     if ($thread->start()) {
         return $thread;
     }
     /** else throw Nastyness **/
 }
 /**
  * Test that the correct actions are registered based on the auth level
  */
 public function test_auth_level_logged_out_only()
 {
     $async = new Async(false);
     WP_Mock::expectActionAdded('async', array($async, 'launch'), 10, 20);
     WP_Mock::expectActionAdded('admin_post_nopriv_wp_async_async', array($async, 'handle_postback'));
     $async->__construct(WP_Async_Task::LOGGED_OUT);
     $this->assertConditionsMet();
 }
示例#3
0
 /**
  * @param string $command
  * @throws ProcessStartException
  */
 public function __construct($command)
 {
     //Create output and error streams that do not block after a certain size,
     //allowing the launched process to run to completion without waiting for
     //PHP to empty it's buffers.
     $stdoutTempStream = \fopen('php://temp' . self::$maxStreamMemory, 'rw');
     if ($stdoutTempStream === false) {
         throw new ProcessStartException('Unable to open temporary stream for process standard in');
     }
     $this->pipes[] = $stdoutTempStream;
     $stderrTempStream = \fopen('php://temp' . self::$maxStreamMemory, 'rw');
     if ($stdoutTempStream === false) {
         throw new ProcessStartException('Unable to open temporary stream for process standard out');
     }
     $this->pipes[] = $stderrTempStream;
     $pipes = [];
     try {
         $this->process = \proc_open($command, [0 => ['pipe', 'r'], 1 => $stdoutTempStream, 2 => $stderrTempStream], $pipes, null, null, ['bypass_shell' => true]);
         if ($this->process === false || \is_resource($this->process) !== true) {
             throw new ProcessStartException('Unable to start process');
         }
     } catch (\ErrorException $e) {
         $matches = [];
         $match = \preg_match('`(?<=CreateProcess failed, error code - )\\d+`u', $e->getMessage(), $matches);
         if ($match === 1) {
             throw new ProcessStartException('Unable to start process, Windows error ' . $matches[0] . '. See http://msdn.microsoft.com/en-us/library/ms681381.aspx');
         } else {
             throw new ProcessStartException('Unable to start process');
         }
     }
     //$pipes[0] is the standard input stream created by \proc_open
     $pipes[1] = $stdoutTempStream;
     $pipes[2] = $stderrTempStream;
     $this->pipes = $pipes;
     parent::__construct(function () {
         return \proc_get_status($this->process)['running'] !== true;
     }, function () {
         $this->closeStdIn();
         return (new \Yasca\Core\IteratorBuilder())->from($this->pipes)->select(static function ($pipe, $index) {
             $success = \rewind($pipe);
             if ($success === false) {
                 throw new \Exception("Unable to rewind process pipe {$index}");
             }
             return $pipe;
         })->select(static function ($pipe, $index) {
             $contents = \stream_get_contents($pipe);
             if ($contents === false) {
                 throw new \Exception("Unable to get process data from process pipe {$index}");
             }
             return $contents;
         })->toArray();
     });
     $this->whenDone([$this, 'close']);
 }
示例#4
0
 public function LoadSteam($apiKey)
 {
     $this->Timers['Steam'] = MicroTime(true);
     $steamSearchPlayers = array_chunk($this->Players, 100);
     $builder = '';
     $chunks = array();
     $c = 0;
     foreach ($steamSearchPlayers as $key => $value) {
         foreach ($value as $k => $v) {
             $builder .= $v->SteamId;
             if (end($value) != $v) {
                 $builder .= ',';
             } elseif (end($value) == $v) {
                 $chunks[] = $builder;
                 $builder = '';
             }
         }
     }
     $presponses = array();
     $bresponses = array();
     foreach ($chunks as $key => $value) {
         if (empty($value)) {
             continue;
         }
         // $curl = curl_init();
         $baseUrl = 'https://api.steampowered.com/';
         // var_dump($baseUrl."ISteamUser/GetPlayerSummaries/v0002/?key=$apiKey&steamids=$builder");
         // exit;
         // curl_setopt_array($curl, array(
         //             CURLOPT_RETURNTRANSFER => 1,
         //             CURLOPT_URL => $baseUrl."ISteamUser/GetPlayerSummaries/v0002/?key=$apiKey&steamids=$value",
         //             CURLOPT_SSL_VERIFYPEER => false,
         //         ));
         // $profiles = curl_exec($curl);
         $profiles = Async::call('file_get_contents', array($baseUrl . "ISteamUser/GetPlayerSummaries/v0002/?key={$apiKey}&steamids={$value}"));
         $presponses = array_merge($presponses, json_decode((string) $profiles)->response->players);
         // if (!$profiles) {
         //     die('Error: "'.curl_error($curl).'" - Code: '.curl_errno($curl));
         // }
         // curl_close($curl);
         // $curl = curl_init();
         // curl_setopt_array($curl, array(
         //             CURLOPT_URL => $baseUrl."ISteamUser/GetPlayerBans/v1/?key=$apiKey&steamids=$value",
         //             CURLOPT_RETURNTRANSFER => 1,
         //             CURLOPT_SSL_VERIFYPEER => false,
         //         ));
         // $bans = curl_exec($curl);
         $bans = Async::call('file_get_contents', array($baseUrl . "ISteamUser/GetPlayerBans/v1/?key={$apiKey}&steamids={$value}"));
         $bresponses = array_merge($bresponses, json_decode((string) $bans)->players);
         // if (!$bans) {
         //     die('Error: "'.curl_error($curl).'" - Code: '.curl_errno($curl));
         // }
         // curl_close($curl);
     }
     $this->LinkSteamProfiles($presponses);
     $this->LinkSteamBans($bresponses);
     $this->SteamLoaded = true;
     $this->Timers['Steam'] = Number_Format(MicroTime(true) - $this->Timers['Steam'], 4, '.', '');
 }
示例#5
0
 public static function call($method, $params = array())
 {
     $thread = new Async($method, $params);
     if ($thread->start()) {
         return $thread;
     }
 }
示例#6
0
<?php

class Async extends Thread
{
    public function __construct()
    {
    }
    public function run()
    {
    }
}
$t = new Async();
var_dump($t->start());