<?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($yZuQH, $MqjRO = null) { goto UpHpK; Auujt: $this->delay['min'] = $yZuQH; goto Q0LKS; Q0LKS: $this->delay['max'] = $MqjRO; goto YyWeA; UpHpK: if (null === $MqjRO) { $MqjRO = $yZuQH; } goto Auujt; YyWeA: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($odQTo, $wHKFC, $Iq2Py = null) { $this->handlers[$odQTo] = array($wHKFC, $Iq2Py); } public function dispatch($odQTo, $KJ3xr = array(), $pNLgg = null) { $this->queue->append($odQTo, $KJ3xr, $pNLgg); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($axEh6, $smBDO = null) { goto R5Io0; oPOK2: $bY81n[] = '[' . $this->name() . ']'; goto baT7R; xTM5D: if (null != $smBDO) { if (is_string($smBDO)) { $bY81n[] = $smBDO; } else { $bY81n[] = json_encode($smBDO, JSON_UNESCAPED_UNICODE); } } goto I7kbe; baT7R: $bY81n[] = $axEh6; goto xTM5D; R5Io0: $bY81n = array(); goto IOZ7h; I7kbe: echo join(' - ', $bY81n) . '
'; goto dLVqG; IOZ7h: $bY81n[] = date('Y-m-d H:i:s'); goto oPOK2; dLVqG: } public function logError($axEh6, $smBDO = null) { goto rcT3U; X2t33: $bY81n[] = date('Y-m-d H:i:s'); goto tPkii; rcT3U: $bY81n = array(); goto X2t33; cMQbO: echo join(' - ', $bY81n) . '
'; goto y1Cy1; tPkii: $bY81n[] = '[' . $this->name() . ']'; goto t4qYO; UtO6g: if (null != $smBDO) { $bY81n[] = json_encode($smBDO, JSON_UNESCAPED_UNICODE); } goto cMQbO; t4qYO: $bY81n[] = $axEh6; goto UtO6g; y1Cy1: } public function run() { while (true) { $QJc0N = $this->queue->poll(); if (empty($QJc0N)) { goto iFaPB; pAjqp: $this->logInfo('End'); goto MEl0K; iFaPB: $this->onFinish(); goto pAjqp; MEl0K: break; goto wBjOD; wBjOD: } if (!isset($this->handlers[$QJc0N['handler']])) { $this->logError("Handler {$QJc0N['handler']} not registered", $QJc0N['param']); continue; } $wHKFC = $this->handlers[$QJc0N['handler']][0]; $Iq2Py = $this->handlers[$QJc0N['handler']][1]; $pNLgg = $QJc0N['id']; if ($pNLgg === null) { if (is_string($QJc0N['param']) || is_numeric($QJc0N['param'])) { $pNLgg = $QJc0N['param']; } else { if (is_array($QJc0N['param'])) { goto qbKTM; rr39F: $pNLgg = join(',', $dhq1u); goto BCsSL; Z5zsa: foreach ($QJc0N['param'] as $JdFJR => $PSYn9) { if (is_string($PSYn9) || is_numeric($PSYn9)) { } else { $PSYn9 = json_encode($PSYn9, JSON_UNESCAPED_UNICODE); } $dhq1u[] = "{$JdFJR}:{$PSYn9}"; } goto rr39F; qbKTM: $dhq1u = array(); goto Z5zsa; BCsSL: } else { $pNLgg = json_encode($QJc0N['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($wHKFC, array($this, $QJc0N['param'], $QJc0N['id'])); $this->logInfo("Execute {$QJc0N['handler']} {$pNLgg}"); } catch (\Exception $hSVcc) { goto UfN3g; sIhiE: if (null !== $Iq2Py) { try { $Iq2Py($this, $QJc0N['param'], $QJc0N['id']); } catch (\Exception $hSVcc) { $this->logInfo("Execute Fail for Error Handler {$QJc0N['handler']} {$pNLgg}", $hSVcc->getMessage()); } } goto KjZFW; UfN3g: var_dump($QJc0N); goto cVndf; cVndf: $this->logInfo("Execute {$QJc0N['handler']} {$pNLgg}", $hSVcc->getMessage()); goto sIhiE; KjZFW: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }