Queue.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Queue
  5. {
  6. public function __construct($method)
  7. {
  8. if (!Dever::config('base')->queue) {
  9. Dever::config('base')->queue = array('method' => $method);
  10. } else {
  11. Dever::config('base')->queue['method'] = $method;
  12. }
  13. Dever::import('queue');
  14. }
  15. public function push($url, $config, $num = 0)
  16. {
  17. if (strpos($url, '{') !== false && strpos($url, '}') !== false) {
  18. $this->preg($url, $num, $config);
  19. } else {
  20. $this->push_db($url, $config);
  21. }
  22. return true;
  23. }
  24. public function pop()
  25. {
  26. return $this->pop_db();
  27. }
  28. private function preg($value, $num, $config)
  29. {
  30. $pat = '/{(.*?)}/i';
  31. preg_match_all($pat, $value, $match);
  32. if (isset($match[1][0]) && $match[1][0]) {
  33. if ($num <= 0) $num = 1000;
  34. parse_str($match[1][0], $param);
  35. $this->page($param, $match[0][0], $value, $num, $config);
  36. }
  37. }
  38. private function page($param, $replace, $value, $num, $config)
  39. {
  40. if (isset($param['page'])) {
  41. for ($i = $param['page']; $i <= $num; $i++) {
  42. $url = str_replace($replace, $i, $value);
  43. $this->push($url, $config);
  44. }
  45. } else {
  46. $this->push(str_replace($replace, '', $value), $config);
  47. }
  48. }
  49. private function push_db($value, $config)
  50. {
  51. $config['url'] = $value;
  52. Dever::push($config);
  53. }
  54. private function pop_db()
  55. {
  56. return Dever::pop();
  57. }
  58. }