<?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($pxo8b, $GA5gO = null) { goto qVKMb; LqUW0: $this->delay['max'] = $GA5gO; goto QOQPq; qVKMb: if (null === $GA5gO) { $GA5gO = $pxo8b; } goto HqjrV; HqjrV: $this->delay['min'] = $pxo8b; goto LqUW0; QOQPq: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($AMe9x, $XPQDG, $OrV_p = null) { $this->handlers[$AMe9x] = array($XPQDG, $OrV_p); } public function dispatch($AMe9x, $FRVtF = array(), $FhIQB = null) { $this->queue->append($AMe9x, $FRVtF, $FhIQB); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($Lo_ux, $GpXwG = null) { goto YlaaR; DXzjL: if (null != $GpXwG) { if (is_string($GpXwG)) { $xHWsa[] = $GpXwG; } else { $xHWsa[] = json_encode($GpXwG, JSON_UNESCAPED_UNICODE); } } goto aktmB; mzJ0M: $xHWsa[] = date('Y-m-d H:i:s'); goto ZtLMV; UaqsS: $xHWsa[] = $Lo_ux; goto DXzjL; aktmB: echo join(' - ', $xHWsa) . '
'; goto lFRjJ; ZtLMV: $xHWsa[] = '[' . $this->name() . ']'; goto UaqsS; YlaaR: $xHWsa = array(); goto mzJ0M; lFRjJ: } public function logError($Lo_ux, $GpXwG = null) { goto gkOsJ; gkOsJ: $xHWsa = array(); goto VPcrm; SfXdW: echo join(' - ', $xHWsa) . '
'; goto SgzTO; riXWI: if (null != $GpXwG) { $xHWsa[] = json_encode($GpXwG, JSON_UNESCAPED_UNICODE); } goto SfXdW; VPcrm: $xHWsa[] = date('Y-m-d H:i:s'); goto biB_F; biB_F: $xHWsa[] = '[' . $this->name() . ']'; goto QSgHj; QSgHj: $xHWsa[] = $Lo_ux; goto riXWI; SgzTO: } public function run() { while (true) { $jm4we = $this->queue->poll(); if (empty($jm4we)) { goto OhoRd; qxfHl: $this->logInfo('End'); goto KSMXy; KSMXy: break; goto iB0QF; OhoRd: $this->onFinish(); goto qxfHl; iB0QF: } if (!isset($this->handlers[$jm4we['handler']])) { $this->logError("Handler {$jm4we['handler']} not registered", $jm4we['param']); continue; } $XPQDG = $this->handlers[$jm4we['handler']][0]; $OrV_p = $this->handlers[$jm4we['handler']][1]; $FhIQB = $jm4we['id']; if ($FhIQB === null) { if (is_string($jm4we['param']) || is_numeric($jm4we['param'])) { $FhIQB = $jm4we['param']; } else { if (is_array($jm4we['param'])) { goto vw8Es; aFvq0: $FhIQB = join(',', $FWGIk); goto fM5Ox; PcIwm: foreach ($jm4we['param'] as $F4wee => $SxG_W) { if (is_string($SxG_W) || is_numeric($SxG_W)) { } else { $SxG_W = json_encode($SxG_W, JSON_UNESCAPED_UNICODE); } $FWGIk[] = "{$F4wee}:{$SxG_W}"; } goto aFvq0; vw8Es: $FWGIk = array(); goto PcIwm; fM5Ox: } else { $FhIQB = json_encode($jm4we['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($XPQDG, array($this, $jm4we['param'], $jm4we['id'])); $this->logInfo("Execute {$jm4we['handler']} {$FhIQB}"); } catch (\Exception $re7gC) { goto REZw1; REZw1: var_dump($jm4we); goto InRoZ; sQ87E: if (null !== $OrV_p) { try { $OrV_p($this, $jm4we['param'], $jm4we['id']); } catch (\Exception $re7gC) { $this->logInfo("Execute Fail for Error Handler {$jm4we['handler']} {$FhIQB}", $re7gC->getMessage()); } } goto p2L7K; InRoZ: $this->logInfo("Execute {$jm4we['handler']} {$FhIQB}", $re7gC->getMessage()); goto sQ87E; p2L7K: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }