<?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($f2znb, $SrkzJ = null) { goto daT3Y; U18Cv: $this->delay['min'] = $f2znb; goto N1WCk; N1WCk: $this->delay['max'] = $SrkzJ; goto pRWw7; daT3Y: if (null === $SrkzJ) { $SrkzJ = $f2znb; } goto U18Cv; pRWw7: } public function init() { if (null == $this->queue) { $this->queue = new ArrayQueue(); } } public function register($B4X5t, $Z2YJF, $D0FHP = null) { $this->handlers[$B4X5t] = array($Z2YJF, $D0FHP); } public function dispatch($B4X5t, $qOrzw = array(), $ULYbu = null) { $this->queue->append($B4X5t, $qOrzw, $ULYbu); } public function onFinish() { } public function name() { return class_basename(static::class); } public function logInfo($nXCfA, $U8wjN = null) { goto IEA22; IEA22: $qNYnn = array(); goto MJbN7; xNSPm: $qNYnn[] = '[' . $this->name() . ']'; goto eTXaN; MJbN7: $qNYnn[] = date('Y-m-d H:i:s'); goto xNSPm; j1uQs: if (null != $U8wjN) { if (is_string($U8wjN)) { $qNYnn[] = $U8wjN; } else { $qNYnn[] = json_encode($U8wjN, JSON_UNESCAPED_UNICODE); } } goto QSZur; eTXaN: $qNYnn[] = $nXCfA; goto j1uQs; QSZur: echo join(' - ', $qNYnn) . '
'; goto eYJrS; eYJrS: } public function logError($nXCfA, $U8wjN = null) { goto LWPUQ; hOhVY: echo join(' - ', $qNYnn) . '
'; goto Ce3sl; xnclq: $qNYnn[] = $nXCfA; goto u71ve; pmfya: $qNYnn[] = '[' . $this->name() . ']'; goto xnclq; LWPUQ: $qNYnn = array(); goto J0C1g; J0C1g: $qNYnn[] = date('Y-m-d H:i:s'); goto pmfya; u71ve: if (null != $U8wjN) { $qNYnn[] = json_encode($U8wjN, JSON_UNESCAPED_UNICODE); } goto hOhVY; Ce3sl: } public function run() { while (true) { $pJeg8 = $this->queue->poll(); if (empty($pJeg8)) { goto hBtby; I8hH_: $this->logInfo('End'); goto kqX_F; hBtby: $this->onFinish(); goto I8hH_; kqX_F: break; goto uXYq1; uXYq1: } if (!isset($this->handlers[$pJeg8['handler']])) { $this->logError("Handler {$pJeg8['handler']} not registered", $pJeg8['param']); continue; } $Z2YJF = $this->handlers[$pJeg8['handler']][0]; $D0FHP = $this->handlers[$pJeg8['handler']][1]; $ULYbu = $pJeg8['id']; if ($ULYbu === null) { if (is_string($pJeg8['param']) || is_numeric($pJeg8['param'])) { $ULYbu = $pJeg8['param']; } else { if (is_array($pJeg8['param'])) { goto aM3hI; K4awl: $ULYbu = join(',', $CQYv7); goto cueK6; aM3hI: $CQYv7 = array(); goto E8Z4e; E8Z4e: foreach ($pJeg8['param'] as $kEhTf => $y2Ltn) { if (is_string($y2Ltn) || is_numeric($y2Ltn)) { } else { $y2Ltn = json_encode($y2Ltn, JSON_UNESCAPED_UNICODE); } $CQYv7[] = "{$kEhTf}:{$y2Ltn}"; } goto K4awl; cueK6: } else { $ULYbu = json_encode($pJeg8['param'], JSON_UNESCAPED_UNICODE); } } } try { call_user_func_array($Z2YJF, array($this, $pJeg8['param'], $pJeg8['id'])); $this->logInfo("Execute {$pJeg8['handler']} {$ULYbu}"); } catch (\Exception $ZemkG) { goto ha21Q; ha21Q: var_dump($pJeg8); goto JzH0L; wWF8t: if (null !== $D0FHP) { try { $D0FHP($this, $pJeg8['param'], $pJeg8['id']); } catch (\Exception $ZemkG) { $this->logInfo("Execute Fail for Error Handler {$pJeg8['handler']} {$ULYbu}", $ZemkG->getMessage()); } } goto hjn61; JzH0L: $this->logInfo("Execute {$pJeg8['handler']} {$ULYbu}", $ZemkG->getMessage()); goto wWF8t; hjn61: } if ($this->delay['max'] > 0) { usleep(rand($this->delay['min'], $this->delay['max']) * 1000); } } } public abstract function setup(); }