<?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($m8yiR, $uB3Mn = null) { goto LLKib; LLKib: if (null === $uB3Mn) { $uB3Mn = $m8yiR; } goto myUEA; LYe1_: $this->delay['max'] = $uB3Mn; goto JgZ8P; myUEA: $this->delay['min'] = $m8yiR; goto LYe1_; JgZ8P: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($SmGn2, $FMTd4, $WUTrh = null) { $this->handlers[$SmGn2] = array($FMTd4, $WUTrh); } public function dispatch($SmGn2, $EPwwc = array(), $M3rBS = null) { $this->queue->append($SmGn2, $EPwwc, $M3rBS); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($JGWZb, $Pg57y = null) { goto izxnS; izxnS: $ZqOUg = array(); goto LMZFT; Eo1oE: if (null != $Pg57y) { if (is_string($Pg57y)) { $ZqOUg[] = $Pg57y; } else { $ZqOUg[] = json_encode($Pg57y, JSON_UNESCAPED_UNICODE); } } goto BSNNg; IIDme: $ZqOUg[] = $JGWZb; goto Eo1oE; LMZFT: $ZqOUg[] = date('Y-m-d H:i:s'); goto tfvLh; tfvLh: $ZqOUg[] = '[' . $this->name() . ']'; goto IIDme; BSNNg: echo join(' - ', $ZqOUg) . '
'; goto t6rbI; t6rbI: } public function logError($JGWZb, $Pg57y = null) { goto QRjAZ; Xj1P0: $ZqOUg[] = $JGWZb; goto NStrG; GPHBA: echo join(' - ', $ZqOUg) . '
'; goto vWgb1; NStrG: if (null != $Pg57y) { $ZqOUg[] = json_encode($Pg57y, JSON_UNESCAPED_UNICODE); } goto GPHBA; HV92o: $ZqOUg[] = '[' . $this->name() . ']'; goto Xj1P0; QRjAZ: $ZqOUg = array(); goto Htmi5; Htmi5: $ZqOUg[] = date('Y-m-d H:i:s'); goto HV92o; vWgb1: } public function run() { while (true) { $n_tN1 = $this->queue->poll(); if (empty($n_tN1)) { goto iOWx3; iOWx3: $this->onFinish(); goto RHM9Q; x7azp: break; goto jxQHT; RHM9Q: $this->logInfo('End'); goto x7azp; jxQHT: } if (!isset($this->handlers[$n_tN1['handler']])) { $this->logError("Handler {$n_tN1['handler']} not registered", $n_tN1['param']); continue; } $FMTd4 = $this->handlers[$n_tN1['handler']][0]; $WUTrh = $this->handlers[$n_tN1['handler']][1]; $M3rBS = $n_tN1['id']; if ($M3rBS === null) { if (is_string($n_tN1['param']) || is_numeric($n_tN1['param'])) { $M3rBS = $n_tN1['param']; } else { if (is_array($n_tN1['param'])) { goto b_dig; b_dig: $JidTX = array(); goto I3t27; ayM1Q: $M3rBS = join(',', $JidTX); goto SNOrI; I3t27: foreach ($n_tN1['param'] as $MFVkb => $YxV2x) { if (is_string($YxV2x) || is_numeric($YxV2x)) { } else { $YxV2x = json_encode($YxV2x, JSON_UNESCAPED_UNICODE); } $JidTX[] = "{$MFVkb}:{$YxV2x}"; } goto ayM1Q; SNOrI: } else { $M3rBS = json_encode($n_tN1['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($FMTd4, array($this, $n_tN1['param'], $n_tN1['id'])); $this->logInfo("Execute {$n_tN1['handler']} {$M3rBS}"); } catch (\Exception $fWY9h) { goto aDYjQ; aDYjQ: var_dump($n_tN1); goto kMU51; MJw7W: if (null !== $WUTrh) { try { $WUTrh($this, $n_tN1['param'], $n_tN1['id']); } catch (\Exception $fWY9h) { $this->logInfo("Execute Fail for Error Handler {$n_tN1['handler']} {$M3rBS}", $fWY9h->getMessage()); } } goto DhhpO; kMU51: $this->logInfo("Execute {$n_tN1['handler']} {$M3rBS}", $fWY9h->getMessage()); goto MJw7W; DhhpO: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }