1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2015-2019 Artem Rodygin |
6
|
|
|
// |
7
|
|
|
// This file is part of DataTables Symfony bundle. |
8
|
|
|
// |
9
|
|
|
// You should have received a copy of the MIT License along with |
10
|
|
|
// the bundle. If not, see <http://opensource.org/licenses/MIT>. |
11
|
|
|
// |
12
|
|
|
//---------------------------------------------------------------------- |
13
|
|
|
|
14
|
|
|
namespace DataTables; |
15
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DataTables lookup service. |
22
|
|
|
*/ |
23
|
|
|
class DataTables implements DataTablesInterface |
24
|
|
|
{ |
25
|
|
|
protected $logger; |
26
|
|
|
protected $validator; |
27
|
|
|
|
28
|
|
|
/** @var DataTableHandlerInterface[] List of registered DataTable services. */ |
29
|
|
|
protected $services = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @codeCoverageIgnore Dependency Injection constructor. |
33
|
|
|
* |
34
|
|
|
* @param LoggerInterface $logger |
35
|
|
|
* @param ValidatorInterface $validator |
36
|
|
|
*/ |
37
|
|
|
public function __construct(LoggerInterface $logger, ValidatorInterface $validator) |
38
|
|
|
{ |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
$this->validator = $validator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Registers specified DataTable handler. |
45
|
|
|
* |
46
|
|
|
* @param DataTableHandlerInterface $service Service of the DataTable handler. |
47
|
|
|
* @param string $id DataTable ID. |
48
|
|
|
*/ |
49
|
|
|
public function addService(DataTableHandlerInterface $service, string $id = null) |
50
|
|
|
{ |
51
|
|
|
$service_id = $id ?? $service::ID; |
52
|
|
|
|
53
|
|
|
if ($service_id !== null) { |
54
|
|
|
$this->services[$service_id] = $service; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
9 |
|
public function handle(Request $request, string $id): DataTableResults |
62
|
|
|
{ |
63
|
9 |
|
$this->logger->debug('Handle DataTable request', [$id]); |
64
|
|
|
|
65
|
|
|
// Retrieve sent parameters. |
66
|
9 |
|
$params = new Parameters(); |
67
|
|
|
|
68
|
|
|
$keyParams = [ |
69
|
9 |
|
'draw', |
70
|
|
|
'start', |
71
|
|
|
'length', |
72
|
|
|
'search', |
73
|
|
|
'order', |
74
|
|
|
'columns', |
75
|
|
|
]; |
76
|
|
|
|
77
|
9 |
|
$params->draw = $request->get('draw'); |
78
|
9 |
|
$params->start = $request->get('start'); |
79
|
9 |
|
$params->length = $request->get('length'); |
80
|
9 |
|
$params->search = $request->get('search'); |
81
|
9 |
|
$params->order = $request->get('order') ?? []; |
82
|
9 |
|
$params->columns = $request->get('columns') ?? []; |
83
|
|
|
|
84
|
9 |
|
$allParams = $request->isMethod(Request::METHOD_POST) |
85
|
1 |
|
? $request->request->all() |
86
|
9 |
|
: $request->query->all(); |
87
|
|
|
|
88
|
9 |
|
$params->customData = array_diff_key($allParams, array_flip($keyParams)); |
89
|
|
|
|
90
|
|
|
// Validate sent parameters. |
91
|
9 |
|
$violations = $this->validator->validate($params); |
92
|
|
|
|
93
|
9 |
|
if (count($violations)) { |
94
|
1 |
|
$message = $violations->get(0)->getMessage(); |
95
|
1 |
|
$this->logger->error($message, ['request']); |
96
|
1 |
|
throw new DataTableException($message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Check for valid handler is registered. |
100
|
8 |
|
if (!array_key_exists($id, $this->services)) { |
101
|
1 |
|
$message = 'Unknown DataTable ID.'; |
102
|
1 |
|
$this->logger->error($message, [$id]); |
103
|
1 |
|
throw new DataTableException($message); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Convert sent parameters into data model. |
107
|
7 |
|
$query = new DataTableQuery($params); |
108
|
|
|
|
109
|
|
|
// Pass the data model to the handler. |
110
|
7 |
|
$result = null; |
|
|
|
|
111
|
|
|
|
112
|
7 |
|
$timer_started = microtime(true); |
113
|
|
|
|
114
|
|
|
try { |
115
|
7 |
|
$result = $this->services[$id]->handle($query); |
116
|
|
|
} |
117
|
1 |
|
catch (\Exception $e) { |
118
|
1 |
|
$this->logger->error($e->getMessage(), [$this->services[$id]]); |
119
|
1 |
|
throw new DataTableException($e->getMessage()); |
120
|
|
|
} |
121
|
6 |
|
finally { |
122
|
7 |
|
$timer_stopped = microtime(true); |
123
|
7 |
|
$this->logger->debug('DataTable processing time', [$timer_stopped - $timer_started, $this->services[$id]]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Validate results returned from handler. |
127
|
6 |
|
$violations = $this->validator->validate($result); |
128
|
|
|
|
129
|
6 |
|
if (count($violations)) { |
130
|
1 |
|
$message = $violations->get(0)->getMessage(); |
131
|
1 |
|
$this->logger->error($message, ['response']); |
132
|
1 |
|
throw new DataTableException($message); |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
$reflection = new \ReflectionProperty(DataTableResults::class, 'draw'); |
136
|
5 |
|
$reflection->setAccessible(true); |
137
|
5 |
|
$reflection->setValue($result, $params->draw); |
138
|
|
|
|
139
|
5 |
|
return $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|