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