|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RedisQ; |
|
4
|
|
|
|
|
5
|
|
|
class RedisQ |
|
6
|
|
|
{ |
|
7
|
|
|
public function queueObject($object) |
|
8
|
|
|
{ |
|
9
|
|
|
global $redis; |
|
10
|
|
|
|
|
11
|
|
|
$wrapped = serialize($object); |
|
12
|
|
|
|
|
13
|
|
|
$allQueues = new RedisTtlSortedSet('redisQ:allQueues'); |
|
14
|
|
|
$objectQueues = new RedisTtlSortedSet('objectQueues'); |
|
15
|
|
|
$queues = $allQueues->getMembers(); |
|
16
|
|
|
|
|
17
|
|
|
$multi = $redis->multi(); |
|
18
|
|
|
|
|
19
|
|
|
// Store an instance of the object |
|
20
|
|
|
$objectID = 'redisQ:objectID:'.uniqID().md5($wrapped); |
|
21
|
|
|
$objectQueues->add(time(), $objectID); |
|
22
|
|
|
$multi->setex($objectID, 9600, $wrapped); |
|
23
|
|
|
|
|
24
|
|
|
// Add objectID to all queues |
|
25
|
|
|
foreach ($queues as $queueID) { |
|
26
|
|
|
$multi->lPush($queueID, $objectID); |
|
27
|
|
|
$multi->expire($queueID, 9600); |
|
28
|
|
|
} |
|
29
|
|
|
$multi->exec(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function registerListener($queueID) |
|
33
|
|
|
{ |
|
34
|
|
|
$allQueues = new RedisTtlSortedSet('redisQ:allQueues'); |
|
35
|
|
|
$allQueues->add(time(), $queueID); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function listen($queueID, $timeToWait = 10, $filterValue = null) |
|
39
|
|
|
{ |
|
40
|
|
|
global $redis; |
|
41
|
|
|
|
|
42
|
|
|
$timeToWait = max(1, min(10, $timeToWait)); |
|
43
|
|
|
|
|
44
|
|
|
$rQueueID = "redisQ:queueID:$queueID"; |
|
45
|
|
|
$wQueueID = "$rQueueID:w"; |
|
46
|
|
|
if ($redis->set($wQueueID, true, Array('nx', 'ex'=>15)) === false) { |
|
47
|
|
|
header('HTTP/1.1 429 Too many requests.'); |
|
48
|
|
|
exit(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
self::registerListener($rQueueID); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$time = time(); |
|
55
|
|
|
do { |
|
56
|
|
|
$object = false; |
|
57
|
|
|
$pop = $redis->blPop($rQueueID, 1); |
|
58
|
|
|
if (!isset($pop[1])) { |
|
59
|
|
|
if (time() >= ($time + $timeToWait)) return; |
|
60
|
|
|
} else { |
|
61
|
|
|
$objectID = $pop[1]; |
|
62
|
|
|
$object = $redis->get($objectID); |
|
63
|
|
|
$object = unserialize($object); |
|
64
|
|
|
$object = self::matchesFilter($filterValue, $object); |
|
65
|
|
|
} |
|
66
|
|
|
} while ($object === false); |
|
67
|
|
|
} finally { |
|
68
|
|
|
$redis->del($wQueueID); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $object; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function matchesFilter($filterValue, $object) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($filterValue == null) return $object; |
|
77
|
|
|
return self::recursive_array_search($filterValue, unserialize($object)) ? $object : false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function recursive_array_search($needle, $haystack) { |
|
81
|
|
|
foreach($haystack as $key=>$value) { |
|
82
|
|
|
$current_key = $key; |
|
|
|
|
|
|
83
|
|
|
if ($needle === $value || (is_array($value) && self::recursive_array_search($needle, $value) !== false)) { |
|
84
|
|
|
return true; |
|
85
|
|
|
} else if ($needle === $value) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: