|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
|
6
|
|
|
use hanneskod\classtools\Iterator\ClassIterator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @SuppressWarnings(PHPMD.TooManyFields) |
|
10
|
|
|
* @codeCoverageIgnore |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class ApiUtils |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
private string $getAll; |
|
17
|
|
|
private string $getCount; |
|
18
|
|
|
private string $create; |
|
19
|
|
|
private string $apiCollection; |
|
20
|
|
|
//namespaces |
|
21
|
|
|
private string $namespacePrefix = 'Swagger'; |
|
22
|
|
|
private string $namespaceApi = 'Api'; |
|
23
|
|
|
private string $namespaceModel = "Model"; |
|
24
|
|
|
private string $namespaceForm = "App\\Form"; |
|
25
|
|
|
//suffix and prefix |
|
26
|
|
|
private string $suffixApiController = 'Api'; |
|
27
|
|
|
private string $prefixControllerModelItem = "DaosRow"; |
|
28
|
|
|
private string $prefixModelItem = "DaosRow"; |
|
29
|
|
|
|
|
30
|
|
|
private string $delete; |
|
31
|
|
|
private string $get; |
|
32
|
|
|
private string $update; |
|
33
|
|
|
private string $getAllToString; |
|
34
|
|
|
|
|
35
|
|
|
//TODO: evaluate to move this variable into configs |
|
36
|
|
|
private string $regexPathModels = '/Swagger\\\(.*)\\\Model\\\DaosRow/'; |
|
37
|
|
|
|
|
38
|
|
|
private string $rootDir; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(string $apiCollection = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if (isset($apiCollection)) { |
|
43
|
|
|
$this->apiCollection = lcfirst($apiCollection); |
|
44
|
|
|
} |
|
45
|
|
|
$this->getAll = "ControllerReadAll"; |
|
46
|
|
|
$this->getCount = "ControllerCount"; |
|
47
|
|
|
$this->create = "ControllerCreate"; |
|
48
|
|
|
$this->delete = "ControllerDeleteItem"; |
|
49
|
|
|
$this->get = "ControllerReadItem"; |
|
50
|
|
|
$this->update = "ControllerUpdateItem"; |
|
51
|
|
|
$this->getAllToString = "ControllerReadAllToString"; |
|
52
|
|
|
|
|
53
|
|
|
$this->initVariables(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function initVariables() : void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->namespacePrefix = 'Swagger'; |
|
59
|
|
|
$this->namespaceApi = 'Api'; |
|
60
|
|
|
$this->namespaceModel = "Model"; |
|
61
|
|
|
$this->namespaceForm = "App\\Form"; |
|
62
|
|
|
//suffix and prefix |
|
63
|
|
|
$this->suffixApiController = 'Api'; |
|
64
|
|
|
$this->prefixControllerModelItem = "DaosRow"; |
|
65
|
|
|
$this->prefixModelItem = "DaosRow"; |
|
66
|
|
|
$this->regexPathModels = '/Swagger\\\(.*)\\\Model\\\DaosRow/'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function setRootDir(string $rootDir) : void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->rootDir = $rootDir; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return path where core bundle will look for api models |
|
76
|
|
|
*/ |
|
77
|
|
|
public function bundlesPath() : string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->rootDir . '/fi'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return namespace prefix for api external bundles, i.e. Swagger |
|
84
|
|
|
*/ |
|
85
|
|
|
public function namespacePrefix() : string |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->namespacePrefix; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return namespace component for api models of external bundles, i.e. \\\Model\\\Models* |
|
92
|
|
|
*/ |
|
93
|
|
|
public function namespaceModels() : string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->namespaceModel; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Return the project Root path string. |
|
100
|
|
|
* In case of project Insurance, it will return the string "\\Swagger\\Insurance\\" |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getProjectRoot(string $projectName) : string |
|
103
|
|
|
{ |
|
104
|
|
|
return "\\".$this->namespacePrefix()."\\".$projectName."\\"; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return the name of Api Controller. |
|
109
|
|
|
* Given the project name (i.e. Insurance) and the collection name (i.e. Claims) it returns the complete path of API controller |
|
110
|
|
|
* class (i.e. \\Swagger\\Insurance\\Api\\ClaimsApi) |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getApiControllerClass(string $project, string $entityName): string |
|
113
|
|
|
{ |
|
114
|
|
|
$className = "\\" . $this->namespacePrefix . "\\$project\\" . $this->namespaceApi . "\\$entityName" . $this->suffixApiController; |
|
115
|
|
|
return $className; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return the name of Model Controller. |
|
120
|
|
|
* Given the project name (i.e. Insurance) and the model name (i.e. Claim) it returns the complete path of API Model controller item |
|
121
|
|
|
* class (i.e. \\Swagger\\Insurance\\Model\\ControllersItemClaim) |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getModelControllerClass(string $project, string $modelName): string |
|
124
|
|
|
{ |
|
125
|
|
|
$className = "\\" . $this->namespacePrefix . "\\$project\\" . $this->namespaceModel . "\\" . $this->prefixControllerModelItem . $modelName; |
|
126
|
|
|
return $className; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Return the name of Model Controller. |
|
131
|
|
|
* Given the project name (i.e. Insurance) and the model name (i.e. Claim) it returns the complete path of API Model controller item |
|
132
|
|
|
* class (i.e. \\Swagger\\Insurance\\Model\\ModelsClaim) |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getModelClass(string $project, string $modelName): string |
|
135
|
|
|
{ |
|
136
|
|
|
$className = "\\" . $this->namespacePrefix . "\\$project\\" . $this->namespaceModel . "\\" . $this->prefixModelItem . $modelName; |
|
137
|
|
|
return $className; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Return the name of stored collection |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getApiCollection(): string |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->apiCollection; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Return the name of Form class. |
|
150
|
|
|
* Given the model name (i.e. Claim) it returns the complete path of API Model controller item |
|
151
|
|
|
* class (i.e. App\\Form\\Claim) |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getFormClass(string $modelName): string |
|
154
|
|
|
{ |
|
155
|
|
|
$className = $this->namespaceForm . "\\" . $modelName; |
|
156
|
|
|
return $className; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Return namespace component for api models of external bundles, i.e. \\\Model\\\Models* |
|
161
|
|
|
*/ |
|
162
|
|
|
public function regexPathModels() : string |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->regexPathModels; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Return the method string to retrieve all elements / or filtering on them |
|
169
|
|
|
*/ |
|
170
|
|
|
public function getAll(): string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->apiCollection . $this->getAll; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Return the method string to retrieve all elements descriptions (it's possible to filter them as for getAll) |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getAllToString(): string |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->apiCollection . $this->getAllToString; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Return the method string to retrieve 1 element |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getItem(): string |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->apiCollection . $this->get; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Return the method string to update 1 element |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getUpdateItem(): string |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->apiCollection . $this->update; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Return the method string to count all elemements inside a collection |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getCount(): string |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->apiCollection . $this->getCount; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Return the method string to create an element |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getCreate(): string |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->apiCollection . $this->create; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return the method string to delete an element |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getDelete(): string |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->apiCollection . $this->delete; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* It looks for Models existent into included external bundles. |
|
225
|
|
|
* It uses ApiUtils in order to know where to search and what look for. |
|
226
|
|
|
* |
|
227
|
|
|
* @return array<mixed> |
|
228
|
|
|
* |
|
229
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
230
|
|
|
*/ |
|
231
|
|
|
public function apiModels(): array |
|
232
|
|
|
{ |
|
233
|
|
|
//where to look for |
|
234
|
|
|
$path = $this->bundlesPath(); |
|
235
|
|
|
$regex = $this->regexPathModels(); |
|
236
|
|
|
//what to look for |
|
237
|
|
|
$models = array(); |
|
238
|
|
|
$finder = new Finder(); |
|
239
|
|
|
$iter = new ClassIterator($finder->files()->in($path)); |
|
240
|
|
|
|
|
241
|
|
|
$matches = array(); |
|
242
|
|
|
// Print the file names of classes, interfaces and traits in given path |
|
243
|
|
|
foreach ($iter->getClassMap() as $classname => $splFileInfo) { |
|
244
|
|
|
preg_match($regex, $classname, $matches); |
|
245
|
|
|
|
|
246
|
|
|
if (count($matches) > 0) { |
|
247
|
|
|
$models[] = $matches[1] . '.' . substr($classname, strlen($matches[0])) . ' (API)'; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
return $models; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|