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