Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 149 lines (117 sloc) 3.219 kb
d69ebfd Add RollingCurlGroup class.
Fabian Franz authored
1 <?
2 /*
3
4 Authored by Fabian Franz (www.lionsad.de)
5 Released under Apache License 2.0
6
7 $Id$
8 */
9
10 class RollingCurlGroupException extends Exception {}
11
12 abstract class RollingCurlGroupRequest extends RollingCurlRequest
13 {
14 private $group = null;
15
16 /**
17 * Set group for this request
18 *
19 * @param group The group to be set
20 */
21 function setGroup($group)
22 {
23 if (!($group instanceof RollingCurlGroup))
24 throw new RollingCurlGroupException("setGroup: group needs to be of instance RollingCurlGroup");
25
26 $this->group = $group;
27 }
28
29 /**
30 * Process the request
31 *
32 *
33 */
34 function process($output, $info)
35 {
36 if ($this->group)
37 $this->group->process($output, $info, $this);
38 }
39
40 /**
41 * @return void
42 */
43 public function __destruct() {
44 unset($this->group);
45 parent::__destruct();
46 }
47
48 }
49
50 class RollingCurlGroup
51 {
52 protected $name;
53 protected $num_requests = 0;
54 protected $finished_requests = 0;
55 private $requests = array();
56
57 function __construct($name)
58 {
59 $this->name = $name;
60 }
61
62 /**
63 * @return void
64 */
65 public function __destruct() {
66 unset($this->name, $this->num_requests, $this->finished_requests, $this->requests);
67 }
68
69
70 function add($request)
71 {
72 if ($request instanceof RollingCurlGroupRequest)
73 {
74 $request->setGroup($this);
75 $this->num_requests++;
76 $this->requests[] = $request;
77 }
a6e664c Fabian Franz Allow arrays to be used for group->add as well.
authored
78 else if (is_array($request))
79 {
80 foreach ($request as $req)
81 $this->add($req);
82 }
d69ebfd Add RollingCurlGroup class.
Fabian Franz authored
83 else
84 throw new RollingCurlGroupException("add: Request needs to be of instance RollingCurlGroupRequest");
85
86 return true;
87 }
88
89 function addToRC($rc)
90 {
91 $ret = true;
92
93 if (!($rc instanceof RollingCurl))
94 throw new RollingCurlGroupException("addToRC: RC needs to be of instance RollingCurl");
95
96 while (count($this->requests) > 0)
97 {
98 $ret1 = $rc->add(array_shift($this->requests));
99 if (!$ret1)
100 $ret = false;
101 }
102
103 return $ret;
104 }
105
106 function process($output, $info, $request)
107 {
108 $this->finished_requests++;
109
110 if ($this->finished_requests >= $this->num_requests)
111 $this->finished();
112 }
113
114 function finished()
115 {
116 }
117
118 }
119
120 class GroupRollingCurl extends RollingCurl {
121
122 private $group_callback = null;
123
124 protected function process($output, $info, $request)
125 {
126 if( $request instanceof RollingCurlGroupRequest)
127 $request->process($output, $info);
128
129 if (is_callable($this->group_callback))
130 call_user_func($this->group_callback, $output, $info, $request);
131 }
132
133 function __construct($callback = null)
134 {
135 $this->group_callback = $callback;
136
137 parent::__construct(array(&$this, "process"));
138 }
139
140 public function add($request)
141 {
142 if ($request instanceof RollingCurlGroup)
143 return $request->addToRC($this);
144 else
145 return parent::add($request);
146 }
147 }
148
Something went wrong with that request. Please try again.