1 | <?php |
||
2 | |||
3 | namespace SoliDry\Blocks; |
||
4 | |||
5 | use SoliDry\Controllers\BaseCommand; |
||
6 | use SoliDry\Extension\JSONApiInterface; |
||
7 | use SoliDry\Helpers\Classes; |
||
8 | use SoliDry\Helpers\Console; |
||
9 | use SoliDry\Helpers\MigrationsHelper; |
||
10 | use SoliDry\Types\ConfigInterface; |
||
11 | use SoliDry\Types\CustomsInterface; |
||
12 | use SoliDry\Types\ModelsInterface; |
||
13 | use SoliDry\Types\ModulesInterface; |
||
14 | use SoliDry\Types\PhpInterface; |
||
15 | use SoliDry\Types\ApiInterface; |
||
16 | |||
17 | /** |
||
18 | * Class Config |
||
19 | * |
||
20 | * @package SoliDry\Blocks |
||
21 | */ |
||
22 | class Config implements ConfigInterface |
||
23 | { |
||
24 | use ContentManager, ConfigTrait; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected string $sourceCode = ''; |
||
30 | |||
31 | /** |
||
32 | * @var BaseCommand |
||
33 | */ |
||
34 | protected BaseCommand $generator; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected string $className; |
||
40 | |||
41 | private array $queryParams = [ |
||
42 | ModelsInterface::PARAM_LIMIT, |
||
43 | ModelsInterface::PARAM_SORT, |
||
44 | ModelsInterface::PARAM_PAGE, |
||
45 | JSONApiInterface::PARAM_ACCESS_TOKEN, |
||
46 | ]; |
||
47 | |||
48 | private array $entityMethods = [ |
||
49 | ConfigInterface::STATE_MACHINE => ConfigInterface::STATE_MACHINE_METHOD, |
||
50 | ConfigInterface::SPELL_CHECK => ConfigInterface::SPELL_CHECK_METHOD, |
||
51 | ConfigInterface::BIT_MASK => ConfigInterface::BIT_MASK_METHOD, |
||
52 | ConfigInterface::CACHE => ConfigInterface::CACHE_METHOD, |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Config constructor. |
||
57 | * @param $generator |
||
58 | */ |
||
59 | public function __construct($generator) |
||
60 | { |
||
61 | $this->generator = $generator; |
||
62 | $this->className = Classes::getClassName($this->generator->objectName); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Creates common config file |
||
67 | */ |
||
68 | public function create() |
||
69 | { |
||
70 | $this->setContent(); |
||
71 | // create config file |
||
72 | $file = $this->generator->formatConfigPath() . |
||
73 | ModulesInterface::CONFIG_FILENAME . PhpInterface::PHP_EXT; |
||
74 | $isCreated = FileManager::createFile($file, $this->sourceCode, true); |
||
75 | if ($isCreated) { |
||
76 | Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Constructs the config structure |
||
82 | */ |
||
83 | private function setContent() |
||
84 | { |
||
85 | $this->setTag(); |
||
86 | $this->openRoot(); |
||
87 | $this->setParam(ModulesInterface::KEY_NAME, ApiInterface::RAML_TYPE_STRING, ucfirst($this->generator->version)); |
||
88 | $this->setParam(ConfigInterface::ATTRIBUTES_CASE, ApiInterface::RAML_TYPE_STRING, ConfigInterface::DEFAULT_CASE); |
||
89 | $this->setQueryParams(); |
||
90 | $this->setTrees(); |
||
91 | $this->setJwtContent(); |
||
92 | $this->setConfigEntities(); |
||
93 | $this->closeRoot(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Sets query params e.g.: |
||
98 | * - limit |
||
99 | * - sort |
||
100 | * - access |
||
101 | */ |
||
102 | private function setQueryParams() |
||
103 | { |
||
104 | if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]) === false) { |
||
105 | $queryParams = $this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]; |
||
106 | $this->openEntity(ConfigInterface::QUERY_PARAMS); |
||
107 | foreach ($this->queryParams as $param) { |
||
108 | if (empty($queryParams[$param][ApiInterface::RAML_KEY_DEFAULT]) === false) { |
||
109 | $this->setParam($param, $queryParams[$param][ApiInterface::RAML_TYPE], $queryParams[$param][ApiInterface::RAML_KEY_DEFAULT], 2); |
||
110 | } |
||
111 | } |
||
112 | $this->closeEntities(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Sets JWT config array |
||
118 | * @example |
||
119 | * 'jwt' => [ |
||
120 | * 'enabled' => true, |
||
121 | * 'table' => 'user', |
||
122 | * 'activate' => 30, |
||
123 | * 'expires' => 3600, |
||
124 | * ], |
||
125 | */ |
||
126 | private function setJwtContent() |
||
127 | { |
||
128 | foreach ($this->generator->types as $objName => $objData) { |
||
129 | if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources |
||
130 | $excluded = false; |
||
131 | foreach ($this->generator->excludedSubtypes as $type) { |
||
132 | if (strpos($objName, $type) !== false) { |
||
133 | $excluded = true; |
||
134 | } |
||
135 | } |
||
136 | // if the type is among excluded - continue |
||
137 | if ($excluded === true) { |
||
138 | continue; |
||
139 | } |
||
140 | $this->setJwtOptions($objName); |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Sets all global config entities by crawling yaml config |
||
147 | * |
||
148 | * @uses setFsmOptions |
||
149 | * @uses setSpellOptions |
||
150 | * @uses setBitMaskOptions |
||
151 | * @uses setCacheOptions |
||
152 | */ |
||
153 | private function setConfigEntities() |
||
154 | { |
||
155 | foreach ($this->entityMethods as $entity => $methodName) { |
||
156 | $this->openEntity($entity); |
||
157 | foreach ($this->generator->types as $objName => $objData) { |
||
158 | if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources |
||
159 | $excluded = false; |
||
160 | foreach ($this->generator->excludedSubtypes as $type) { |
||
161 | if (strpos($objName, $type) !== false) { |
||
162 | $excluded = true; |
||
163 | } |
||
164 | } |
||
165 | // if the type is among excluded - continue |
||
166 | if ($excluded === true) { |
||
167 | continue; |
||
168 | } |
||
169 | $this->setOptions($objName, $methodName); |
||
170 | } |
||
171 | } |
||
172 | $this->closeEntities(); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $objName |
||
178 | * @param string $methodName |
||
179 | */ |
||
180 | private function setOptions(string $objName, string $methodName) |
||
181 | { |
||
182 | if (empty($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS]) === false) { |
||
183 | foreach ($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS] as $propKey => $propVal) { |
||
184 | $this->$methodName($objName, $propKey, $propVal); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Sets cache config enabled option true to activate caching mechanism |
||
191 | * @param string $objName |
||
192 | */ |
||
193 | private function setCacheOptions(string $objName) |
||
194 | { |
||
195 | if (empty($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE]) === false |
||
196 | && $this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE] === CustomsInterface::CUSTOM_TYPE_REDIS) { |
||
197 | $this->openCache($objName); |
||
198 | foreach ($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_PROPS] as $prop => $value) { |
||
199 | $this->setParam($prop, $value[ApiInterface::RAML_TYPE], $value[ApiInterface::RAML_KEY_DEFAULT], 3); |
||
200 | } |
||
201 | $this->closeEntity(2, true); |
||
202 | // unset cache to prevent doubling |
||
203 | unset($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE]); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string $objName |
||
209 | * @param string $propKey |
||
210 | * @param $propVal |
||
211 | */ |
||
212 | private function setSpellOptions(string $objName, string $propKey, $propVal) |
||
213 | { |
||
214 | if (is_array($propVal) && empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_CHECK]) === false) { |
||
215 | // found FSM definition |
||
216 | $this->openSc($objName, $propKey); |
||
217 | $this->setParam(ConfigInterface::LANGUAGE, ApiInterface::RAML_TYPE_STRING, empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_LANGUAGE]) |
||
218 | ? ConfigInterface::DEFAULT_LANGUAGE |
||
219 | : $propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_LANGUAGE], 4); |
||
220 | $this->closeEntities(); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param string $objName |
||
226 | * @param string $propKey |
||
227 | * @param $propVal |
||
228 | */ |
||
229 | private function setFsmOptions(string $objName, string $propKey, $propVal) |
||
230 | { |
||
231 | if (is_array($propVal)) {// create fsm |
||
232 | if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE]) === false) { |
||
233 | // found FSM definition |
||
234 | $this->openFsm($objName, $propKey); |
||
235 | foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE] as $key => &$val) { |
||
236 | $this->setTabs(5); |
||
237 | $this->setArrayProperty($key, (array)$val); |
||
238 | } |
||
239 | $this->closeEntities(); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string $objName |
||
246 | * @param string $propKey |
||
247 | * @param $propVal |
||
248 | * @example |
||
249 | * 'bit_mask'=> [ |
||
250 | * 'user'=> [ |
||
251 | * 'permissions'=> [ |
||
252 | * 'enabled' => true, |
||
253 | * 'flags'=> [ |
||
254 | * 'publisher' => 1, |
||
255 | * 'editor' => 2, |
||
256 | * 'manager' => 4, |
||
257 | * 'photo_reporter' => 8, |
||
258 | * 'admin' => 16, |
||
259 | * ], |
||
260 | * ], |
||
261 | * ], |
||
262 | * ], |
||
263 | */ |
||
264 | private function setBitMaskOptions(string $objName, string $propKey, $propVal) |
||
265 | { |
||
266 | if (is_array($propVal)) { |
||
267 | if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK]) === false) { |
||
268 | // found FSM definition |
||
269 | $this->openBitMask($objName, $propKey); |
||
270 | foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK] as $key => $val) { |
||
271 | $this->setParam($key, ApiInterface::RAML_TYPE_INTEGER, $val, 5); |
||
272 | } |
||
273 | $this->closeEntities(); |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Sets jwt config options |
||
280 | * @param string $objName |
||
281 | * @example |
||
282 | * 'jwt'=> [ |
||
283 | * 'enabled' => true, |
||
284 | * 'table' => 'user', |
||
285 | * 'activate' => 30, |
||
286 | * 'expires' => 3600, |
||
287 | * ], |
||
288 | */ |
||
289 | private function setJwtOptions(string $objName) |
||
290 | { |
||
291 | if (empty($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS]) === false) { |
||
292 | foreach ($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS] as $propKey => $propVal) { |
||
293 | if (is_array($propVal) && $propKey === CustomsInterface::CUSTOM_PROP_JWT) {// create jwt config setting |
||
294 | $this->openEntity(ConfigInterface::JWT); |
||
295 | $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 2); |
||
296 | $this->setParam(ModelsInterface::MIGRATION_TABLE, ApiInterface::RAML_TYPE_STRING, MigrationsHelper::getTableName($objName), 2); |
||
297 | $this->setParam(ConfigInterface::ACTIVATE, ApiInterface::RAML_TYPE_INTEGER, ConfigInterface::DEFAULT_ACTIVATE, 2); |
||
298 | $this->setParam(ConfigInterface::EXPIRES, ApiInterface::RAML_TYPE_INTEGER, ConfigInterface::DEFAULT_EXPIRES, 2); |
||
299 | $this->closeEntities(); |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Sets config trees structure |
||
307 | * @example |
||
308 | * 'trees'=> [ |
||
309 | * 'menu' => true, |
||
310 | * ], |
||
311 | */ |
||
312 | private function setTrees() |
||
313 | { |
||
314 | if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_TREES][ApiInterface::RAML_PROPS]) === false) { |
||
315 | foreach ($this->generator->types[CustomsInterface::CUSTOM_TYPES_TREES][ApiInterface::RAML_PROPS] as $propKey => $propVal) { |
||
316 | if (is_array($propVal) && empty($this->generator->types[ucfirst($propKey)]) === false) { |
||
317 | // ensure that there is a type of propKey ex.: Menu with parent_id field set |
||
318 | $this->openEntity(ConfigInterface::TREES); |
||
319 | $this->setParamDefault($propKey, $propVal[ApiInterface::RAML_KEY_DEFAULT]); |
||
320 | $this->closeEntities(); |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } |