-
Notifications
You must be signed in to change notification settings - Fork 0
/
minions.php
executable file
·101 lines (91 loc) · 2.31 KB
/
minions.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
<?php
namespace PMVC\PlugIn\minions;
use PMVC\PlugIn\curl\curl;
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\minions';
\PMVC\initPlugin(
[
'curl'=>null
]
);
class minions extends curl
{
const options = 'options';
const callback = 'callback';
const hash = 'hash';
const hosts = 'hosts';
const curl = 'curl';
public function init()
{
parent::init();
$this->useCache(false);
$this['ttl'] = 86400;
}
public function useCache($bool)
{
if ($bool) {
$this['process'] = [
$this,
'processCache'
];
} else {
$this['process'] = [
$this,
'processClient'
];
}
}
public function processClient($more=null)
{
if (isset($this['cookieHandler'])) {
$this->ask()->handleCookie();
unset($this['cookieHandler']);
}
\PMVC\dev(
function () use (&$more) {
$more[]= 'request_header';
}, 'req'
);
$this->_handleQueue([$this->ask(), 'process'], [$more]);
}
public function processCache($more=null, $ttl=null)
{
\PMVC\dev(
function () use (&$more) {
$more[]= 'request_header';
}, 'req'
);
if (is_null($ttl)) {
$ttl = $this['ttl'];
}
$this->_handleQueue([$this->cache(), 'process'], [$more, $ttl]);
$this['cache']->finish();
}
private function _handleQueue($callback, $params)
{
$curls = $this->getCurls();
if (empty($curls) || !count($curls)) {
return;
}
$queue = [];
foreach ($curls as $curl) {
$options = $curl->set();
if (!empty($options)) {
$queue[] = [
self::hash =>$curl->getHash(),
self::options =>$options,
self::callback=>$curl->getCallback()
];
$curl->clean();
}
}
$this->clean();
while(count($queue))
{
$first = array_shift($queue);
call_user_func_array($callback, array_merge([$first], $params));
if (empty($queue)) {
break;
}
}
}
}