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 totalReached() |
40
|
|
|
{ |
41
|
28 |
|
return $this->limit && $this->count >= $this->limit; |
42
|
|
|
} |
43
|
|
|
|
44
|
28 |
|
private function fetchResources() |
45
|
|
|
{ |
46
|
28 |
|
$response = call_user_func($this->requestFn, $this->currentMarker); |
47
|
|
|
|
48
|
28 |
|
$json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey); |
49
|
|
|
|
50
|
28 |
|
if ($response->getStatusCode() === 204 || empty($json)) { |
51
|
4 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
28 |
|
return $json; |
55
|
|
|
} |
56
|
|
|
|
57
|
28 |
|
private function assembleResource(array $data) |
58
|
|
|
{ |
59
|
28 |
|
$resource = call_user_func($this->resourceFn, $data); |
60
|
|
|
|
61
|
|
|
// Invoke user-provided fn if provided |
62
|
28 |
|
if ($this->mapFn) { |
63
|
1 |
|
call_user_func_array($this->mapFn, [&$resource]); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
// Update marker if operation supports it |
67
|
28 |
|
if ($this->markerKey) { |
68
|
8 |
|
$this->currentMarker = $resource->{$this->markerKey}; |
69
|
8 |
|
} |
70
|
|
|
|
71
|
28 |
|
return $resource; |
72
|
|
|
} |
73
|
|
|
|
74
|
28 |
|
public function __invoke() |
75
|
|
|
{ |
76
|
28 |
|
while (true) { |
77
|
|
|
// Fetch new collection from API. Break loop if empty set returned |
78
|
28 |
|
if (false === ($resources = $this->fetchResources())) { |
79
|
4 |
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
28 |
|
foreach ($resources as $resourceData) { |
83
|
|
|
// Halt if user-provided limit is reached |
84
|
28 |
|
if ($this->totalReached()) { |
85
|
2 |
|
break; |
86
|
|
|
} |
87
|
|
|
|
88
|
28 |
|
$this->count++; |
89
|
|
|
|
90
|
28 |
|
yield $this->assembleResource($resourceData); |
91
|
28 |
|
} |
92
|
|
|
|
93
|
|
|
// If user-provided limit has been reached, or if the operation does not support pagination, halt the |
94
|
|
|
// loop without sending another request. |
95
|
28 |
|
if ($this->totalReached() || !$this->markerKey) { |
96
|
24 |
|
break; |
97
|
|
|
} |
98
|
4 |
|
} |
99
|
|
|
} |
100
|
|
|
} |