1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Directus – <http://getdirectus.com> |
5
|
|
|
* |
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Directus\SDK; |
12
|
|
|
|
13
|
|
|
use Directus\Database\Connection; |
14
|
|
|
use Directus\Database\TableGateway\BaseTableGateway; |
15
|
|
|
use Directus\Database\TableGateway\RelationalTableGateway; |
16
|
|
|
use Directus\Database\TableSchema; |
17
|
|
|
use Directus\SDK\Response\EntryCollection; |
18
|
|
|
use Directus\SDK\Response\Entry; |
19
|
|
|
use Zend\Db\Sql\Select; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Client Local |
23
|
|
|
* |
24
|
|
|
* Client to Interact with the database directly using Directus Database Component |
25
|
|
|
* |
26
|
|
|
* @author Welling Guzmán <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ClientLocal implements RequestsInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var BaseTableGateway[] |
32
|
|
|
*/ |
33
|
|
|
protected $tableGateways = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Connection |
37
|
|
|
*/ |
38
|
|
|
protected $connection = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ClientLocal constructor. |
42
|
|
|
* |
43
|
|
|
* @param $connection |
44
|
|
|
*/ |
45
|
|
|
public function __construct($connection) |
46
|
|
|
{ |
47
|
|
|
$this->connection = $connection; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Gets the list of tables name in the database |
52
|
|
|
* |
53
|
|
|
* @param array $params |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getTables(array $params = []) |
58
|
|
|
{ |
59
|
|
|
return TableSchema::getTablesSchema($params); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets all the columns in the database |
64
|
|
|
* |
65
|
|
|
* @param array $params |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getColumns(array $params = []) |
70
|
|
|
{ |
71
|
|
|
return TableSchema::getColumnsSchema($params); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets table columns |
76
|
|
|
* |
77
|
|
|
* @param $tableName |
78
|
|
|
* @param array $params |
79
|
|
|
* |
80
|
|
|
* @return \Directus\Database\Object\Column[] |
81
|
|
|
*/ |
82
|
|
|
public function getTableColumns($tableName, array $params = []) |
83
|
|
|
{ |
84
|
|
|
$tables = TableSchema::getTableColumnsSchema($tableName, $params); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return $tables; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets all the entries in the given table name |
91
|
|
|
* |
92
|
|
|
* @param string $tableName |
93
|
|
|
* @param array $params |
94
|
|
|
* |
95
|
|
|
* @return Entry|EntryCollection |
96
|
|
|
*/ |
97
|
|
|
public function getEntries($tableName, array $params = []) |
98
|
|
|
{ |
99
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
100
|
|
|
|
101
|
|
|
return $this->createResponseFromData($tableGateway->getEntries($params)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Gets an entry in the given table name with the given id |
106
|
|
|
* |
107
|
|
|
* @param string $tableName |
108
|
|
|
* @param mixed $id |
109
|
|
|
* @param array $params |
110
|
|
|
* |
111
|
|
|
* @return array|mixed |
112
|
|
|
*/ |
113
|
|
|
public function getEntry($tableName, $id, array $params = []) |
114
|
|
|
{ |
115
|
|
|
// @TODO: Dynamic ID |
116
|
|
|
return $this->getEntries($tableName, array_merge($params, [ |
|
|
|
|
117
|
|
|
'id' => $id |
118
|
|
|
])); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets the list of users |
123
|
|
|
* |
124
|
|
|
* @param array $params |
125
|
|
|
* |
126
|
|
|
* @return array|mixed |
127
|
|
|
*/ |
128
|
|
|
public function getUsers(array $params = []) |
129
|
|
|
{ |
130
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
131
|
|
|
return $this->getEntries('directus_users', $params); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Gets an user by the given id |
136
|
|
|
* |
137
|
|
|
* @param $id |
138
|
|
|
* @param array $params |
139
|
|
|
* |
140
|
|
|
* @return array|mixed |
141
|
|
|
*/ |
142
|
|
|
public function getUser($id, array $params = []) |
143
|
|
|
{ |
144
|
|
|
return $this->getEntry('directus_users', $id, $params); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritDoc |
149
|
|
|
*/ |
150
|
|
|
public function fetchTables() |
151
|
|
|
{ |
152
|
|
|
// TODO: Implement fetchTables() method. |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritDoc |
157
|
|
|
*/ |
158
|
|
|
public function fetchTableInfo($tableName) |
159
|
|
|
{ |
160
|
|
|
// TODO: Implement fetchTableInfo() method. |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritDoc |
165
|
|
|
*/ |
166
|
|
|
public function fetchColumns($tableName) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
// TODO: Implement fetchColumns() method. |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritDoc |
173
|
|
|
*/ |
174
|
|
|
public function fetchColumnInfo($tableName, $columnName) |
175
|
|
|
{ |
176
|
|
|
// TODO: Implement fetchColumnInfo() method. |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @inheritDoc |
181
|
|
|
*/ |
182
|
|
|
public function fetchItems($tableName = null, $conditions = []) |
183
|
|
|
{ |
184
|
|
|
if ($tableName == null) { |
185
|
|
|
$tableName = $this->getTable(); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$select = new Select($tableName); |
189
|
|
|
|
190
|
|
|
// Conditional to honor the active column, (does not check if column exists) |
191
|
|
|
if (isset($conditions['active'])) { |
192
|
|
|
$select->where->equalTo('active', $conditions['active']); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Order by "id desc" by default or by a parameter value |
196
|
|
|
if (isset($conditions['sort'])) { |
197
|
|
|
$select->order($conditions['sort']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->selectWith($select); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @inheritDoc |
205
|
|
|
*/ |
206
|
|
|
public function fetchItem($tableName, $itemID) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
// TODO: Implement fetchItem() method. |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @inheritDoc |
213
|
|
|
*/ |
214
|
|
|
public function fetchGroups() |
215
|
|
|
{ |
216
|
|
|
// TODO: Implement fetchGroups() method. |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @inheritDoc |
221
|
|
|
*/ |
222
|
|
|
public function fetchGroupInfo($groupID) |
223
|
|
|
{ |
224
|
|
|
// TODO: Implement fetchGroupInfo() method. |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @inheritDoc |
229
|
|
|
*/ |
230
|
|
|
public function fetchGroupPrivileges($groupID) |
231
|
|
|
{ |
232
|
|
|
// TODO: Implement fetchGroupPrivileges() method. |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @inheritDoc |
237
|
|
|
*/ |
238
|
|
|
public function getFiles(array $params = []) |
239
|
|
|
{ |
240
|
|
|
return $this->getEntries('directus_files', $params); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @inheritDoc |
245
|
|
|
*/ |
246
|
|
|
public function getFile($id, array $params = []) |
247
|
|
|
{ |
248
|
|
|
return $this->getEntry('directus_files', $id, $params); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @inheritDoc |
253
|
|
|
*/ |
254
|
|
|
public function fetchSettings() |
255
|
|
|
{ |
256
|
|
|
// TODO: Implement fetchSettings() method. |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @inheritDoc |
261
|
|
|
*/ |
262
|
|
|
public function fetchSettingCollection($collectionName) |
263
|
|
|
{ |
264
|
|
|
// TODO: Implement fetchSettingCollection() method. |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get a table gateway for the given table name |
269
|
|
|
* |
270
|
|
|
* @param $tableName |
271
|
|
|
* |
272
|
|
|
* @return RelationalTableGateway |
273
|
|
|
*/ |
274
|
|
|
protected function getTableGateway($tableName) |
275
|
|
|
{ |
276
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
277
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this->tableGateways[$tableName]; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// @TODO: move to a builder class |
284
|
|
|
protected function createResponseFromData($data) |
285
|
|
|
{ |
286
|
|
|
if (isset($data['rows'])) { |
287
|
|
|
$response = new EntryCollection($data); |
288
|
|
|
} else { |
289
|
|
|
$response = new Entry($data); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $response; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.