<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace Module\Vendor\QuickRun\Crawl; abstract class AbstractCrawler { protected $queue = null; protected $handlers = array(); protected $delay = array('min' => 0, 'max' => 0); public function setDelay($DeIXu, $Lnyvt = null) { goto s02ej; kOo2_: $this->delay['max'] = $Lnyvt; goto YqHQE; s02ej: if (null === $Lnyvt) { $Lnyvt = $DeIXu; } goto xli4t; xli4t: $this->delay['min'] = $DeIXu; goto kOo2_; YqHQE: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($Qxqov, $b2yRC, $nw7PP = null) { $this->handlers[$Qxqov] = array($b2yRC, $nw7PP); } public function dispatch($Qxqov, $mzzZ3 = array(), $DcXJr = null) { $this->queue->append($Qxqov, $mzzZ3, $DcXJr); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($wk1ic, $q62oK = null) { goto cKBOi; xH8nB: if (null != $q62oK) { if (is_string($q62oK)) { $ck6T8[] = $q62oK; } else { $ck6T8[] = json_encode($q62oK, JSON_UNESCAPED_UNICODE); } } goto GIn4C; cKBOi: $ck6T8 = array(); goto xV8Im; GIn4C: echo join(' - ', $ck6T8) . '
'; goto Y0XKk; xV8Im: $ck6T8[] = date('Y-m-d H:i:s'); goto lvesX; lvesX: $ck6T8[] = '[' . $this->name() . ']'; goto Y8HcH; Y8HcH: $ck6T8[] = $wk1ic; goto xH8nB; Y0XKk: } public function logError($wk1ic, $q62oK = null) { goto naCGP; T0b9Z: echo join(' - ', $ck6T8) . '
'; goto uVm1c; gABE2: if (null != $q62oK) { $ck6T8[] = json_encode($q62oK, JSON_UNESCAPED_UNICODE); } goto T0b9Z; naCGP: $ck6T8 = array(); goto GJCoy; iIq64: $ck6T8[] = $wk1ic; goto gABE2; GJCoy: $ck6T8[] = date('Y-m-d H:i:s'); goto sqMEF; sqMEF: $ck6T8[] = '[' . $this->name() . ']'; goto iIq64; uVm1c: } public function run() { while (true) { $sEf_e = $this->queue->poll(); if (empty($sEf_e)) { goto BBel5; PSj5E: $this->logInfo('End'); goto JUAS0; BBel5: $this->onFinish(); goto PSj5E; JUAS0: break; goto ICP0h; ICP0h: } if (!isset($this->handlers[$sEf_e['handler']])) { $this->logError("Handler {$sEf_e['handler']} not registered", $sEf_e['param']); continue; } $b2yRC = $this->handlers[$sEf_e['handler']][0]; $nw7PP = $this->handlers[$sEf_e['handler']][1]; $DcXJr = $sEf_e['id']; if ($DcXJr === null) { if (is_string($sEf_e['param']) || is_numeric($sEf_e['param'])) { $DcXJr = $sEf_e['param']; } else { if (is_array($sEf_e['param'])) { goto BQrpO; P33On: $DcXJr = join(',', $y2SDh); goto x41h8; BQrpO: $y2SDh = array(); goto tgfJY; tgfJY: foreach ($sEf_e['param'] as $d2HEV => $hf0nR) { if (is_string($hf0nR) || is_numeric($hf0nR)) { } else { $hf0nR = json_encode($hf0nR, JSON_UNESCAPED_UNICODE); } $y2SDh[] = "{$d2HEV}:{$hf0nR}"; } goto P33On; x41h8: } else { $DcXJr = json_encode($sEf_e['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($b2yRC, array($this, $sEf_e['param'], $sEf_e['id'])); $this->logInfo("Execute {$sEf_e['handler']} {$DcXJr}"); } catch (\Exception $cknMI) { goto n_yIl; n_yIl: var_dump($sEf_e); goto GvCKz; GvCKz: $this->logInfo("Execute {$sEf_e['handler']} {$DcXJr}", $cknMI->getMessage()); goto nucqW; nucqW: if (null !== $nw7PP) { try { $nw7PP($this, $sEf_e['param'], $sEf_e['id']); } catch (\Exception $cknMI) { $this->logInfo("Execute Fail for Error Handler {$sEf_e['handler']} {$DcXJr}", $cknMI->getMessage()); } } goto zKlNt; zKlNt: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }