1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common\Resource; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Transport\Utils; |
6
|
|
|
|
7
|
|
|
class Iterator |
8
|
|
|
{ |
9
|
|
|
private $requestFn; |
10
|
|
|
private $resourceFn; |
11
|
|
|
private $limit; |
12
|
|
|
private $count; |
13
|
|
|
private $resourcesKey; |
14
|
|
|
private $markerKey; |
15
|
|
|
private $mapFn; |
16
|
|
|
private $currentMarker; |
17
|
|
|
|
18
|
28 |
|
public function __construct(array $options, callable $requestFn, callable $resourceFn) |
19
|
|
|
{ |
20
|
28 |
|
$this->limit = isset($options['limit']) ? $options['limit'] : false; |
21
|
28 |
|
$this->count = 0; |
22
|
|
|
|
23
|
28 |
|
if (isset($options['resourcesKey'])) { |
24
|
23 |
|
$this->resourcesKey = $options['resourcesKey']; |
25
|
23 |
|
} |
26
|
|
|
|
27
|
28 |
|
if (isset($options['markerKey'])) { |
28
|
8 |
|
$this->markerKey = $options['markerKey']; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
28 |
|
if (isset($options['mapFn']) && is_callable($options['mapFn'])) { |
32
|
1 |
|
$this->mapFn = $options['mapFn']; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
28 |
|
$this->requestFn = $requestFn; |
36
|
28 |
|
$this->resourceFn = $resourceFn; |
37
|
28 |
|
} |
38
|
|
|
|
39
|
28 |
|
private function fetchResources() |
40
|
|
|
{ |
41
|
28 |
|
$response = call_user_func($this->requestFn, $this->currentMarker); |
42
|
|
|
|
43
|
28 |
|
$json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey); |
44
|
|
|
|
45
|
28 |
|
if ($response->getStatusCode() === 204 || empty($json)) { |
46
|
4 |
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
28 |
|
return $json; |
50
|
|
|
} |
51
|
|
|
|
52
|
28 |
|
private function assembleResource(array $data) |
53
|
|
|
{ |
54
|
28 |
|
$resource = call_user_func($this->resourceFn, $data); |
55
|
|
|
|
56
|
|
|
// Invoke user-provided fn if provided |
57
|
28 |
|
if ($this->mapFn) { |
58
|
1 |
|
call_user_func_array($this->mapFn, [&$resource]); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
// Update marker if operation supports it |
62
|
28 |
|
if ($this->markerKey) { |
63
|
8 |
|
$this->currentMarker = $resource->{$this->markerKey}; |
64
|
8 |
|
} |
65
|
|
|
|
66
|
28 |
|
return $resource; |
67
|
|
|
} |
68
|
|
|
|
69
|
28 |
|
private function totalReached() |
70
|
|
|
{ |
71
|
28 |
|
return $this->limit && $this->count >= $this->limit; |
72
|
|
|
} |
73
|
|
|
|
74
|
28 |
|
private function shouldHalt() |
75
|
|
|
{ |
76
|
28 |
|
return $this->totalReached() || !$this->markerKey; |
77
|
|
|
} |
78
|
|
|
|
79
|
28 |
|
public function __invoke() |
80
|
|
|
{ |
81
|
28 |
|
while (true) { |
82
|
|
|
// Fetch new collection from API. Break loop if empty set returned |
83
|
28 |
|
if (false === ($resources = $this->fetchResources())) { |
84
|
4 |
|
break; |
85
|
|
|
} |
86
|
|
|
|
87
|
28 |
|
foreach ($resources as $resourceData) { |
88
|
|
|
// Halt if user-provided limit is reached |
89
|
28 |
|
if ($this->totalReached()) { |
90
|
2 |
|
break; |
91
|
|
|
} |
92
|
|
|
|
93
|
28 |
|
$this->count++; |
94
|
|
|
|
95
|
28 |
|
yield $this->assembleResource($resourceData); |
96
|
28 |
|
} |
97
|
|
|
|
98
|
|
|
// If user-provided limit has been reached, or if the operation does not support pagination, halt the |
99
|
|
|
// loop without sending another request. |
100
|
28 |
|
if ($this->shouldHalt()) { |
101
|
24 |
|
break; |
102
|
|
|
} |
103
|
4 |
|
} |
104
|
|
|
} |
105
|
|
|
} |