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