-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trieur.php
396 lines (349 loc) · 8.53 KB
/
Trieur.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
namespace Polinome\Trieur;
use Solire\Conf\Conf;
/**
* Trieur.
*
* @author polinome <contact@polinome.com>
* @license MIT http://mit-license.org/
*/
class Trieur extends \Pimple\Container
{
/**
* Columns list.
*
* @var Columns
*/
protected $columns = null;
/**
* Configuration.
*
* @var Conf
*/
protected $conf = null;
/**
* Driver.
*
* @var Driver
*/
protected $driver = null;
/**
* Source to the database.
*
* @var Source
*/
protected $source = null;
/**
* List of supported drivers and their mappings to the driver classes.
*
* @var array
*/
private static $driverMap = [
'dataTables' => '\Polinome\Trieur\Driver\DataTables',
'csv' => '\Polinome\Trieur\Driver\Csv',
];
/**
* List of supported source class and their mappings to the source
* wrapper classes.
*
* @var type
*/
private static $sourceMap = [
'doctrine' => '\Polinome\Trieur\Source\Doctrine',
'csv' => '\Polinome\Trieur\Source\Csv',
];
/**
* Constructor.
*
* @param Conf $conf The configuration
* @param mixed $sourceModel The database source object
*/
public function __construct(Conf $conf, $sourceModel = null)
{
$this->init($conf, $sourceModel);
$this->run();
}
/**
* Initialise the container, and prepare the instanciation of the driver
* and source class.
*
* @param Conf $conf The configuration
* @param mixed $sourceModel The data source object
*
* @return void
*/
private function init(Conf $conf, $sourceModel = null)
{
$this->conf = $conf;
$this->initColumns();
$this->initDriver();
if ($sourceModel !== null) {
$this['sourceModel'] = $sourceModel;
$this->initSource();
}
$this->initFormat();
}
/**
* Instanciate the driver and source class.
*
* @return void
*/
private function run()
{
$this->setColumns($this['columns']);
$this->setDriver($this['driver']);
if (isset($this['source'])) {
$this->setSource($this['source']);
}
}
/**
* Build Colmumns configuration object.
*
* @return void
*/
protected function initColumns()
{
if (!isset($this->conf->columns)) {
$this->conf->columns = new Conf();
}
$this['columns'] = function ($c) {
return new Columns(
$c->conf->columns
);
};
}
/**
* Find the driver class.
*
* @return void
*/
protected function findDriverClass()
{
if (isset($this->conf->driver->class)) {
if (!class_exists($this->conf->driver->class)) {
throw new Exception(
sprintf(
'class "%s" does not exist',
$this->conf->driver->class
)
);
}
if (!is_subclass_of($this->conf->driver->class, '\Polinome\Trieur\Driver')) {
throw new Exception(
sprintf(
'class "%s" does not extend abstract class '
. '"\Polinome\Trieur\Driver"',
$this->conf->driver->class
)
);
}
} elseif (isset($this->conf->driver->name)
&& isset(self::$driverMap[$this->conf->driver->name])
) {
$this->conf->driver->class = self::$driverMap[
$this->conf->driver->name
];
} else {
throw new Exception(
'No class for driver class founed or given'
);
}
}
/**
* Build Driver object.
*
* @return void
*/
protected function initDriver()
{
$this->findDriverClass();
if (!isset($this->conf->driver->conf)) {
$this->conf->driver->conf = new Conf();
}
$this['driver'] = function ($c) {
$className = $c->conf->driver->class;
return new $className(
$c->conf->driver->conf,
$this['columns']
);
};
}
/**
* Find the source class.
*
* @return void
*
* @throws \Exception If no wrapper class found
*/
protected function findSourceClass()
{
if (isset($this->conf->source->class)) {
if (!class_exists($this->conf->source->class)) {
throw new Exception(
sprintf(
'class "%s" does not exist',
$this->conf->source->class
)
);
}
if (!is_subclass_of($this->conf->source->class, '\Polinome\Trieur\Source')) {
throw new Exception(
sprintf(
'class "%s" does not extend '
. 'abstract class "\Polinome\Trieur\Source"',
$this->conf->source->class
)
);
}
return;
}
if (isset($this->conf->source->name)
&& isset(self::$sourceMap[$this->conf->source->name])
) {
$this->conf->source->class = self::$sourceMap[
$this->conf->source->name
];
return;
}
throw new Exception(
'No wrapper class for source class founed'
);
}
/**
* Build the source wrapper class.
*
* @return void
*/
protected function initSource()
{
$this->findSourceClass();
if (!isset($this->conf->source->conf)) {
$this->conf->source->conf = new Conf();
}
$this['source'] = function ($c) {
$className = $c->conf->source->class;
return new $className(
$c->conf->source->conf,
$this['columns'],
$c['sourceModel']
);
};
}
/**
* Build the format class.
*
* @return void
*/
protected function initFormat()
{
$this['format'] = function ($c) {
return new Format(
$this['columns']
);
};
}
/**
* Sets the columns configuration.
*
* @param Columns $columns The columns configuration
*
* @return self
*/
public function setColumns(Columns $columns)
{
$this->columns = $columns;
return $this;
}
/**
* Sets the driver.
*
* @param Driver $driver The driver
*
* @return self
*/
public function setDriver(Driver $driver)
{
$this->driver = $driver;
return $this;
}
/**
* Sets the source wrapper.
*
* @param Source $source The data source
*
* @return self
*/
public function setSource(Source $source)
{
$this->source = $source;
return $this;
}
/**
* Get the Driver.
*
* @return Driver
*/
public function getDriver()
{
return $this->driver;
}
/**
* Get the source wrapper object.
*
* @return Source
*/
public function getSource()
{
return $this->source;
}
/**
* Set the request.
*
* @param mixed $request The request
*
* @return self
*/
public function setRequest($request)
{
$this->driver->setRequest($request);
return $this;
}
/**
* Prepare the fetch.
*
* @return self
*/
public function prepare()
{
$filters = $this->driver->getFilters();
if (!empty($filters)) {
$this->source->addFilters($filters);
}
$this->source->setLength($this->driver->getLength());
$this->source->setOffset($this->driver->getOffset());
$this->source->setOrders($this->driver->getOrder());
return $this;
}
/**
* Prepare the fetch.
*
* @return self
*/
public function fetch()
{
return $this->driver->getResponse(
$this['format']->format($this->source->getData()),
$this->source->getCount(),
$this->source->getFilteredCount()
);
}
/**
* Returns the response.
*
* @return array
*/
public function getResponse()
{
return $this->prepare()->fetch();
}
}