Queue.php 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Spider\Lib;
  3. use Dever;
  4. class Queue
  5. {
  6. private $data = array();
  7. public function push($value, $key = 0, $num = 0)
  8. {
  9. if (strpos($value, '{') !== false && strpos($value, '}') !== false) {
  10. $this->preg($value, $num);
  11. } else {
  12. array_push($this->data, $value);
  13. }
  14. return true;
  15. }
  16. public function pop()
  17. {
  18. return array_shift($this->data);
  19. }
  20. private function preg($value, $num)
  21. {
  22. $pat = '/{(.*?)}/i';
  23. preg_match_all($pat, $value, $match);
  24. if (isset($match[1][0]) && $match[1][0]) {
  25. if ($num <= 0) $num = 1000;
  26. parse_str($match[1][0], $param);
  27. $this->page($param, $match[0][0], $value, $num);
  28. }
  29. }
  30. private function page($param, $replace, $value, $num)
  31. {
  32. if (isset($param['page'])) {
  33. for ($i = $param['page']; $i <= $num; $i++) {
  34. $url = str_replace($replace, $i, $value);
  35. $this->push($url);
  36. }
  37. } else {
  38. $this->push(str_replace($replace, '', $value));
  39. }
  40. }
  41. }