Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/drydock/query/DrydockResourceQuery.php
12256 views
1
<?php
2
3
final class DrydockResourceQuery extends DrydockQuery {
4
5
private $ids;
6
private $phids;
7
private $statuses;
8
private $types;
9
private $blueprintPHIDs;
10
private $datasourceQuery;
11
private $needUnconsumedCommands;
12
13
public function withIDs(array $ids) {
14
$this->ids = $ids;
15
return $this;
16
}
17
18
public function withPHIDs(array $phids) {
19
$this->phids = $phids;
20
return $this;
21
}
22
23
public function withTypes(array $types) {
24
$this->types = $types;
25
return $this;
26
}
27
28
public function withStatuses(array $statuses) {
29
$this->statuses = $statuses;
30
return $this;
31
}
32
33
public function withBlueprintPHIDs(array $blueprint_phids) {
34
$this->blueprintPHIDs = $blueprint_phids;
35
return $this;
36
}
37
38
public function withDatasourceQuery($query) {
39
$this->datasourceQuery = $query;
40
return $this;
41
}
42
43
public function needUnconsumedCommands($need) {
44
$this->needUnconsumedCommands = $need;
45
return $this;
46
}
47
48
public function newResultObject() {
49
return new DrydockResource();
50
}
51
52
protected function willFilterPage(array $resources) {
53
$blueprint_phids = mpull($resources, 'getBlueprintPHID');
54
55
$blueprints = id(new DrydockBlueprintQuery())
56
->setViewer($this->getViewer())
57
->withPHIDs($blueprint_phids)
58
->execute();
59
$blueprints = mpull($blueprints, null, 'getPHID');
60
61
foreach ($resources as $key => $resource) {
62
$blueprint = idx($blueprints, $resource->getBlueprintPHID());
63
if (!$blueprint) {
64
$this->didRejectResult($resource);
65
unset($resources[$key]);
66
continue;
67
}
68
$resource->attachBlueprint($blueprint);
69
}
70
71
return $resources;
72
}
73
74
protected function didFilterPage(array $resources) {
75
if ($this->needUnconsumedCommands) {
76
$commands = id(new DrydockCommandQuery())
77
->setViewer($this->getViewer())
78
->setParentQuery($this)
79
->withTargetPHIDs(mpull($resources, 'getPHID'))
80
->withConsumed(false)
81
->execute();
82
$commands = mgroup($commands, 'getTargetPHID');
83
84
foreach ($resources as $resource) {
85
$list = idx($commands, $resource->getPHID(), array());
86
$resource->attachUnconsumedCommands($list);
87
}
88
}
89
90
return $resources;
91
}
92
93
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
94
$where = parent::buildWhereClauseParts($conn);
95
96
if ($this->ids !== null) {
97
$where[] = qsprintf(
98
$conn,
99
'resource.id IN (%Ld)',
100
$this->ids);
101
}
102
103
if ($this->phids !== null) {
104
$where[] = qsprintf(
105
$conn,
106
'resource.phid IN (%Ls)',
107
$this->phids);
108
}
109
110
if ($this->types !== null) {
111
$where[] = qsprintf(
112
$conn,
113
'resource.type IN (%Ls)',
114
$this->types);
115
}
116
117
if ($this->statuses !== null) {
118
$where[] = qsprintf(
119
$conn,
120
'resource.status IN (%Ls)',
121
$this->statuses);
122
}
123
124
if ($this->blueprintPHIDs !== null) {
125
$where[] = qsprintf(
126
$conn,
127
'resource.blueprintPHID IN (%Ls)',
128
$this->blueprintPHIDs);
129
}
130
131
if ($this->datasourceQuery !== null) {
132
$where[] = qsprintf(
133
$conn,
134
'resource.name LIKE %>',
135
$this->datasourceQuery);
136
}
137
138
return $where;
139
}
140
141
protected function getPrimaryTableAlias() {
142
return 'resource';
143
}
144
145
}
146
147