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