<?php
/**
 * ------------------------ 
 *  版权所有  www.tecmz.com
 *  商业版本请购买正版授权使用
 * ------------------------
*/ namespace Module\Vendor\QuickRun\Crawl; use ModStart\Core\Util\SerializeUtil; abstract class AbstractCrawler { protected $queue = null; protected $handlers = array(); protected $delay = array('min' => 0, 'max' => 0); public function setDelay($h53P0, $oLXSv = null) { goto WOnCo; mPO3M: $this->delay['min'] = $h53P0; goto SmMsD; SmMsD: $this->delay['max'] = $oLXSv; goto FjliM; WOnCo: if (null === $oLXSv) { $oLXSv = $h53P0; } goto mPO3M; FjliM: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($Np3Ob, $YDW_n, $xdPzG = null) { $this->handlers[$Np3Ob] = array($YDW_n, $xdPzG); } public function dispatch($Np3Ob, $w6bW5 = array(), $gZ39D = null) { $this->queue->append($Np3Ob, $w6bW5, $gZ39D); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($Qs3bw, $avcnU = null) { goto HxP8p; HxP8p: $QCRGR = array(); goto VR5hs; d3QxF: $QCRGR[] = $Qs3bw; goto E9nkX; df5Yn: $QCRGR[] = '[' . $this->name() . ']'; goto d3QxF; VR5hs: $QCRGR[] = date('Y-m-d H:i:s'); goto df5Yn; E9nkX: if (null != $avcnU) { if (is_string($avcnU)) { $QCRGR[] = $avcnU; } else { $QCRGR[] = SerializeUtil::jsonEncode($avcnU, JSON_UNESCAPED_UNICODE); } } goto n7s47; n7s47: echo join(' - ', $QCRGR) . '
'; goto SvMin; SvMin: } public function logError($Qs3bw, $avcnU = null) { goto Lq6bq; VbTuX: $QCRGR[] = $Qs3bw; goto ziZyc; nHWJ9: $QCRGR[] = date('Y-m-d H:i:s'); goto Cj1j5; Cj1j5: $QCRGR[] = '[' . $this->name() . ']'; goto VbTuX; ziZyc: if (null != $avcnU) { $QCRGR[] = SerializeUtil::jsonEncode($avcnU, JSON_UNESCAPED_UNICODE); } goto AQxVz; Lq6bq: $QCRGR = array(); goto nHWJ9; AQxVz: echo join(' - ', $QCRGR) . '
'; goto CgnvR; CgnvR: } public function run() { while (true) { $owZYb = $this->queue->poll(); if (empty($owZYb)) { goto KXtCy; DfZP5: break; goto e_gcs; KXtCy: $this->onFinish(); goto Z6Pzh; Z6Pzh: $this->logInfo('End'); goto DfZP5; e_gcs: } if (!isset($this->handlers[$owZYb['handler']])) { $this->logError("Handler {$owZYb['handler']} not registered", $owZYb['param']); continue; } $YDW_n = $this->handlers[$owZYb['handler']][0]; $xdPzG = $this->handlers[$owZYb['handler']][1]; $gZ39D = $owZYb['id']; if ($gZ39D === null) { if (is_string($owZYb['param']) || is_numeric($owZYb['param'])) { $gZ39D = $owZYb['param']; } else { if (is_array($owZYb['param'])) { goto Tfe8L; Tfe8L: $FN9bz = array(); goto iWp2u; iWp2u: foreach ($owZYb['param'] as $KAKiA => $Iim2u) { if (is_string($Iim2u) || is_numeric($Iim2u)) { } else { $Iim2u = SerializeUtil::jsonEncode($Iim2u, JSON_UNESCAPED_UNICODE); } $FN9bz[] = "{$KAKiA}:{$Iim2u}"; } goto POdHm; POdHm: $gZ39D = join(',', $FN9bz); goto doS4T; doS4T: } else { $gZ39D = SerializeUtil::jsonEncode($owZYb['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($YDW_n, array($this, $owZYb['param'], $owZYb['id'])); $this->logInfo("Execute {$owZYb['handler']} {$gZ39D}"); } catch (\Exception $qoTNG) { goto C0NHq; p4Xf2: $this->logInfo("Execute {$owZYb['handler']} {$gZ39D}", $qoTNG->getMessage()); goto Ki6P8; Ki6P8: if (null !== $xdPzG) { try { $xdPzG($this, $owZYb['param'], $owZYb['id']); } catch (\Exception $qoTNG) { $this->logInfo("Execute Fail for Error Handler {$owZYb['handler']} {$gZ39D}", $qoTNG->getMessage()); } } goto Wto_I; C0NHq: var_dump($owZYb); goto p4Xf2; Wto_I: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }