1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Cdf\BiCoreBundle\Service\Permessi\PermessiManager; |
6
|
|
|
|
7
|
|
|
use function count; |
8
|
|
|
|
9
|
|
|
use ReflectionClass; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
11
|
|
|
use Twig\Environment; |
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
13
|
|
|
use Symfony\Component\String\Inflector\EnglishInflector; |
14
|
|
|
use Cdf\BiCoreBundle\Utils\Api\ApiUtils; |
15
|
|
|
use Cdf\BiCoreBundle\Utils\String\StringUtils; |
16
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Cdf\BiCoreBundle\Service\Api\ApiManager; |
20
|
|
|
use Cdf\BiCoreBundle\Service\Api\Oauth2TokenService; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @SuppressWarnings(PHPMD.TooManyFields) |
24
|
|
|
* @codeCoverageIgnore |
25
|
|
|
*/ |
26
|
|
|
class FiApiController extends AbstractController |
27
|
|
|
{ |
28
|
|
|
use FiApiCoreControllerTrait; |
29
|
|
|
use FiApiCoreCrudControllerTrait; |
|
|
|
|
30
|
|
|
use FiCoreTabellaControllerTrait; |
31
|
|
|
|
32
|
|
|
protected string $bundle; |
33
|
|
|
protected Environment $template; |
34
|
|
|
protected string $controller; |
35
|
|
|
protected PermessiManager $permessi; |
36
|
|
|
//API rest attributes |
37
|
|
|
protected string $project; |
38
|
|
|
protected string $model; |
39
|
|
|
protected string $collection; |
40
|
|
|
protected string $modelClass; |
41
|
|
|
protected string $formClass; |
42
|
|
|
protected string $controllerItem; |
43
|
|
|
protected string $apiController; |
44
|
|
|
/** @var array<mixed> */ |
45
|
|
|
protected array $options; |
46
|
|
|
/** @var array<mixed> */ |
47
|
|
|
protected array $enumOptions; |
48
|
|
|
/** @var array<mixed> */ |
49
|
|
|
protected array $inflectorExceptions; |
50
|
|
|
protected ParameterBagInterface $params; |
51
|
|
|
protected EntityManagerInterface $em; |
52
|
|
|
protected ApiManager $apiManager; |
53
|
|
|
protected string $apiProjectCollection; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function __construct(PermessiManager $permessi, Environment $template, ParameterBagInterface $params, EntityManagerInterface $em) |
58
|
|
|
{ |
59
|
|
|
$matches = []; |
60
|
|
|
$controllo = new ReflectionClass(get_class($this)); |
61
|
|
|
|
62
|
|
|
preg_match('/(.*)\\\(.*)\\\Controller\\\(.*)Controller/', $controllo->name, $matches); |
63
|
|
|
if (0 == count($matches)) { |
64
|
|
|
preg_match('/(.*)(.*)\\\Controller\\\(.*)Controller/', $controllo->name, $matches); |
65
|
|
|
} |
66
|
|
|
$this->project = $this->getProject(); |
67
|
|
|
$this->params = $params; |
68
|
|
|
|
69
|
|
|
$this->bundle = ($matches[count($matches) - 2] ? $matches[count($matches) - 2] : $matches[count($matches) - 3]); |
70
|
|
|
$this->controller = $matches[count($matches) - 1]; |
71
|
|
|
$this->permessi = $permessi; |
72
|
|
|
$this->template = $template; |
73
|
|
|
$this->em = $em; |
74
|
|
|
|
75
|
|
|
$this->model = $this->controller; //they matches |
76
|
|
|
$this->collection = $this->pluralize($this->model); |
77
|
|
|
$apiUtil = new ApiUtils(); |
78
|
|
|
$this->modelClass = $apiUtil->getModelClass($this->project, $this->model); |
79
|
|
|
$this->formClass = $apiUtil->getFormClass($this->model); |
80
|
|
|
$this->controllerItem = $apiUtil->getModelControllerClass($this->project, $this->model); |
81
|
|
|
$this->apiController = $apiUtil->getApiControllerClass($this->project, $this->collection); |
82
|
|
|
$this->options = array(); |
83
|
|
|
$this->enumOptions = array(); |
84
|
|
|
$this->inflectorExceptions = array(); |
85
|
|
|
|
86
|
|
|
//*** instantiate ApiManager *** |
87
|
|
|
$this->apiManager = new ApiManager( |
88
|
|
|
$this->params->get("bi_core.oauth2_enabled"), |
89
|
|
|
new Oauth2TokenService( |
90
|
|
|
$this->params->get("bi_core.oauth2_endpoint"), |
91
|
|
|
$this->params->get("bi_core.oauth2_clientkey") |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$this->apiManager->setProjectName($this->project); |
96
|
|
|
$this->apiProjectCollection = strtolower($this->collection); |
97
|
|
|
|
98
|
|
|
$projectRoot = $apiUtil->getProjectRoot($this->project); |
99
|
|
|
|
100
|
|
|
$projectConfiguration = $projectRoot."Configuration"; |
101
|
|
|
$projectHeaderSelector = $projectRoot."HeaderSelector"; |
102
|
|
|
|
103
|
|
|
$config = $projectConfiguration::getDefaultConfiguration(); |
104
|
|
|
$headerSelector = new $projectHeaderSelector($config); |
105
|
|
|
|
106
|
|
|
$this->apiManager->setApiClientConfigs($headerSelector, $config); |
107
|
|
|
|
108
|
|
|
$this->apiManager->setApiController($this->apiProjectCollection); |
109
|
|
|
|
110
|
|
|
//it generates options one time for all |
111
|
|
|
$this->loadInflectorExceptions(); |
112
|
|
|
$this->generateEnumAndOptions(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function loadInflectorExceptions() : void |
116
|
|
|
{ |
117
|
|
|
$vars = $this->params->get("bi_core.api_inflector_exceptions"); |
118
|
|
|
if (($vars)) { |
119
|
|
|
$values = json_decode($vars, true); |
120
|
|
|
$this->inflectorExceptions = $values; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getCollectionName() : string |
125
|
|
|
{ |
126
|
|
|
return $this->apiProjectCollection; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Copy this method into your controller in case of exceptions |
131
|
|
|
* |
132
|
|
|
* @param string $singleForm |
133
|
|
|
* @return array<mixed>|string |
134
|
|
|
*/ |
135
|
|
|
protected function pluralizeForm(string $singleForm) |
136
|
|
|
{ |
137
|
|
|
if (isset($this->inflectorExceptions[$singleForm])) { |
138
|
|
|
return $this->inflectorExceptions[$singleForm]; |
139
|
|
|
} |
140
|
|
|
$inflector = new EnglishInflector(); |
141
|
|
|
return $inflector->pluralize($singleForm); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Pluralize a single form giving as response the correct plurale form matching with existent objects |
146
|
|
|
*/ |
147
|
|
|
protected function pluralize(string $singleForm) : string |
148
|
|
|
{ |
149
|
|
|
$outcome = ''; |
150
|
|
|
$results = $this->pluralizeForm($singleForm); |
151
|
|
|
|
152
|
|
|
if (is_array($results)) { |
153
|
|
|
$apiUtil = new ApiUtils(); |
154
|
|
|
foreach ($results as $result) { |
155
|
|
|
//get name of api controller |
156
|
|
|
$apiClassPath = $apiUtil->getApiControllerClass($this->project, $result); |
157
|
|
|
if (class_exists($apiClassPath)) { |
158
|
|
|
$outcome = $result; |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} else { |
163
|
|
|
$outcome = $results; |
164
|
|
|
} |
165
|
|
|
return $outcome; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Generate option choices for edit form |
170
|
|
|
*/ |
171
|
|
|
protected function generateEnumAndOptions() : void |
172
|
|
|
{ |
173
|
|
|
$itemController = new $this->controllerItem(); |
174
|
|
|
$fieldMappings = $itemController::swaggerTypes(); |
175
|
|
|
|
176
|
|
|
//dump($fieldMappings); |
177
|
|
|
|
178
|
|
|
foreach (array_keys($fieldMappings) as $fieldName) { |
179
|
|
|
//is it a foreign key field? |
180
|
|
|
if (\str_contains($fieldName, '_id')) { |
181
|
|
|
$tools = $this->getApiTools($fieldName, '_id'); |
182
|
|
|
$apiController = $tools['controller']; |
183
|
|
|
$apiBook = $tools['book']; |
184
|
|
|
|
185
|
|
|
$method = $apiBook->getAllToString(); |
186
|
|
|
$results = $apiController->$method(); |
187
|
|
|
|
188
|
|
|
$arrayContainer = array(); |
189
|
|
|
foreach ($results as $myItem) { |
190
|
|
|
//transform this items for options |
191
|
|
|
$element = array("id" => $myItem->getCode(), "descrizione" => $myItem->getText(), "valore" => $myItem->getText()); |
192
|
|
|
array_push($arrayContainer, $element); |
193
|
|
|
} |
194
|
|
|
$this->options[$tools['entity']] = $arrayContainer; |
195
|
|
|
} elseif (\str_contains($fieldName, '_enum')) { |
196
|
|
|
//dump("in fieldname ".$fieldName ); |
197
|
|
|
$tools = $this->getApiTools($fieldName, '_enum'); |
198
|
|
|
$apiController = $tools['controller']; |
199
|
|
|
$apiBook = $tools['book']; |
200
|
|
|
|
201
|
|
|
$getAllToStringMethod = $apiBook->getAllToString(); |
202
|
|
|
$results = $apiController->$getAllToStringMethod(); |
203
|
|
|
|
204
|
|
|
$decodeMap = array(); |
205
|
|
|
$arrayContainer = array(); |
206
|
|
|
foreach ($results as $result) { |
207
|
|
|
$decodeMap[$result['code']] = $result['text']; |
208
|
|
|
$element = array("id" => $result['code'], "descrizione" => $result['text'], "valore" => $result['text']); |
209
|
|
|
array_push($arrayContainer, $element); |
210
|
|
|
} |
211
|
|
|
$this->options[$tools['entity']] = $arrayContainer; |
212
|
|
|
|
213
|
|
|
$arrayItem = array('nometabella' => $this->controller, 'nomecampo' => "$this->controller.$fieldName", 'etichetta' => "$fieldName", |
214
|
|
|
'escluso' => false, |
215
|
|
|
'decodifiche' => $decodeMap); |
216
|
|
|
|
217
|
|
|
array_push($this->enumOptions, $arrayItem); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* It checks if there are some enum collections having a match with defined column fields |
224
|
|
|
* and append attribute "decofiche" to them. |
225
|
|
|
* THIS HAVE TO BE INVOKED BY SPECIFIC ENTITY CONTROLLER. |
226
|
|
|
*/ |
227
|
|
|
/** |
228
|
|
|
* It returns an array with 'controller' with the apiController |
229
|
|
|
* and 'book' the apiBook |
230
|
|
|
* and 'entity' the given fieldName less the suffix |
231
|
|
|
|
232
|
|
|
* @param array<mixed> $modellocolonne |
233
|
|
|
*/ |
234
|
|
|
protected function mergeColumnsAndEnumOptions(array &$modellocolonne) : void |
235
|
|
|
{ |
236
|
|
|
foreach ($this->enumOptions as $enumOption) { |
237
|
|
|
if (isset($modellocolonne[$enumOption['nomecampo']])) { |
238
|
|
|
$modellocolonne[$enumOption['nomecampo']]['decodifiche'] = $enumOption['decodifiche']; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* It returns an array with 'controller' with the apiController |
245
|
|
|
* and 'book' the apiBook |
246
|
|
|
* and 'entity' the given fieldName less the suffix |
247
|
|
|
|
248
|
|
|
* @param string $fieldName |
249
|
|
|
* @param string $suffixString |
250
|
|
|
* @return array<mixed> |
251
|
|
|
*/ |
252
|
|
|
protected function getApiTools($fieldName, $suffixString): array |
253
|
|
|
{ |
254
|
|
|
$entityName = substr($fieldName, 0, strpos($fieldName, $suffixString)); |
255
|
|
|
|
256
|
|
|
$parametri = array('str' => $entityName, 'primamaiuscola' => true); |
257
|
|
|
$outcome = StringUtils::toCamelCase($parametri); |
258
|
|
|
$outcome = $this->pluralize($outcome); |
259
|
|
|
|
260
|
|
|
$apiUtil = new ApiUtils(); |
261
|
|
|
$apiControllerClass = $apiUtil->getApiControllerClass($this->project, $outcome); |
262
|
|
|
$apiController = new $apiControllerClass(); |
263
|
|
|
|
264
|
|
|
//$apiBook = new ApiUtils($entityName); |
265
|
|
|
$apiBook = new ApiUtils($outcome); |
266
|
|
|
|
267
|
|
|
$results = [ |
268
|
|
|
'controller' => $apiController, |
269
|
|
|
'book' => $apiBook, |
270
|
|
|
'entity' => $entityName, |
271
|
|
|
]; |
272
|
|
|
return $results; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
protected function getBundle() : string |
276
|
|
|
{ |
277
|
|
|
return $this->bundle; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
protected function getController() : string |
281
|
|
|
{ |
282
|
|
|
return $this->controller; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
protected function getPermessi() : PermessiManager |
286
|
|
|
{ |
287
|
|
|
return $this->permessi; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
protected function getTemplate() : Environment |
291
|
|
|
{ |
292
|
|
|
return $this->template; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function getProject() : string |
296
|
|
|
{ |
297
|
|
|
$annotations = array(); |
298
|
|
|
$r = new ReflectionClass(get_class($this)); |
299
|
|
|
$doc = $r->getDocComment(); |
300
|
|
|
preg_match_all('#@var\(biproject="(.*?)"\)[\r\n]+#s', $doc, $annotations); |
301
|
|
|
return $annotations[1][0]; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|