1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\GridBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Dtc\GridBundle\Grid\Renderer\AbstractRenderer; |
6
|
|
|
use Dtc\GridBundle\Util\CamelCase; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
|
|
|
11
|
|
|
|
12
|
|
|
class GridController |
13
|
|
|
{ |
14
|
|
|
private $container; |
15
|
|
|
|
16
|
|
|
public function __construct(ContainerInterface $container) |
17
|
|
|
{ |
18
|
|
|
$this->container = $container; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function grid(Request $request) |
22
|
|
|
{ |
23
|
|
|
$class = $request->get('class'); |
24
|
|
|
if (!$class) { |
25
|
|
|
throw $this->createNotFoundException('No class passed in'); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($rendererId = $request->get('renderer')) { |
29
|
|
|
if (!$this->container->has($rendererId)) { |
30
|
|
|
throw new \Exception("No renderer found with id $rendererId"); |
31
|
|
|
} |
32
|
|
|
if (!($renderer = $this->container->get($rendererId)) instanceof AbstractRenderer) { |
33
|
|
|
throw new \Exception("Rennderer $rendererId must be instanace of Dtc\GridBundle\Grid\Renderer\AbstractRenderer"); |
34
|
|
|
} |
35
|
|
|
if (!($view = $request->get('view'))) { |
36
|
|
|
throw new \Exception("No view parameter specified for renderer $rendererId"); |
37
|
|
|
} |
38
|
|
|
} else { |
39
|
|
|
$rendererType = $request->get('type', 'table'); |
40
|
|
|
$renderer = $this->container->get('dtc_grid.renderer.factory')->create($rendererType); |
41
|
|
|
$view = '@DtcGrid/Page/'.$rendererType.'.html.twig'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$gridSource = $this->container->get('dtc_grid.manager.source')->get($class); |
45
|
|
|
$renderer->bind($gridSource); |
46
|
|
|
|
47
|
|
|
if ($this->container->has('templating')) { |
48
|
|
|
return new Response($this->container->get('templating')->render($view, $renderer->getParams())); |
49
|
|
|
} elseif ($this->container->has('twig')) { |
50
|
|
|
return new Response($this->container->get('twig')->render($view, $renderer->getParams())); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new \Exception('Need Twig Bundle or Templating component installed'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function data(Request $request) |
57
|
|
|
{ |
58
|
|
|
$rendererService = $request->get('renderer', 'datatables'); |
59
|
|
|
if ($this->container->has($rendererService)) { |
60
|
|
|
if (!($rendererService = $this->container->get($rendererService)) instanceof AbstractRenderer) { |
61
|
|
|
throw new \Exception("$rendererService not instance of Dtc\GridBundle\Grid\Renderer\AbstractRenderer"); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$renderer = $this->container->get('dtc_grid.renderer.factory')->create($rendererService); |
65
|
|
|
} |
66
|
|
|
$gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id')); |
67
|
|
|
|
68
|
|
|
$response = new Response(); |
69
|
|
|
$gridSource->bind($request); // Sets limit, offset, sort, filter, etc |
70
|
|
|
$renderer->bind($gridSource); // Sets grid to renderer |
|
|
|
|
71
|
|
|
|
72
|
|
|
$fields = $request->get('fields', null); |
73
|
|
|
if ($fields && is_array($fields)) { |
74
|
|
|
$gridSource->selectColumns($fields); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$content = null; |
78
|
|
|
// If changes to data is kept track using update_time, then we |
79
|
|
|
// can skip querying for all data. |
80
|
|
|
if ($lastModified = $gridSource->getLastModified()) { |
81
|
|
|
$response->setLastModified($lastModified); |
82
|
|
|
} else { |
83
|
|
|
// generate etag from data |
84
|
|
|
$data = $renderer->getData(); |
85
|
|
|
$content = json_encode($data); |
86
|
|
|
$eTag = hash('sha256', $content); |
87
|
|
|
$response->setEtag($eTag); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$response->setPublic(); |
91
|
|
|
if ($response->isNotModified($request)) { |
92
|
|
|
return $response; |
93
|
|
|
} else { |
94
|
|
|
if (!$content) { |
95
|
|
|
$data = $renderer->getData(); |
96
|
|
|
$content = json_encode($data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$response->headers->set('Content-type', 'application/json'); |
100
|
|
|
$response->setContent($content); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return JsonResponse|Response |
108
|
|
|
*/ |
109
|
|
|
public function show(Request $request) |
110
|
|
|
{ |
111
|
|
|
$gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id')); |
112
|
|
|
$id = $request->get('identifier'); |
113
|
|
|
$result = $gridSource->find($id); |
114
|
|
|
|
115
|
|
|
$responseResult = []; |
116
|
|
|
if (!$result) { |
117
|
|
|
return new Response('Not Found', 404); |
118
|
|
|
} |
119
|
|
|
if (is_array($result)) { |
120
|
|
|
foreach ($result as $key => $value) { |
121
|
|
|
$responseResult[CamelCase::fromCamelCase($key)] = $value; |
122
|
|
|
} |
123
|
|
|
} elseif (method_exists($gridSource, 'getClassMetadata')) { |
124
|
|
|
$classMetadata = $gridSource->getClassMetadata(); |
125
|
|
|
$fieldNames = $classMetadata->getFieldNames(); |
126
|
|
|
foreach ($fieldNames as $fieldName) { |
127
|
|
|
$method = 'get'.ucfirst($fieldName); |
128
|
|
|
if (method_exists($result, $method)) { |
129
|
|
|
$responseResult[CamelCase::fromCamelCase($fieldName)] = $result->$method(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return new JsonResponse($responseResult); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return Response |
139
|
|
|
*/ |
140
|
|
|
public function delete(Request $request) |
141
|
|
|
{ |
142
|
|
|
$gridSource = $this->container->get('dtc_grid.manager.source')->get($request->get('id')); |
143
|
|
|
$id = $request->get('identifier'); |
144
|
|
|
$gridSource->remove($id); |
145
|
|
|
$response = new Response(); |
146
|
|
|
$response->setStatusCode(204); |
147
|
|
|
|
148
|
|
|
return $response; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths