These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\GeneratorBundle\Generator; |
||
4 | |||
5 | use Faker\Provider\Base; |
||
6 | use Faker\Provider\DateTime; |
||
7 | use Faker\Provider\Lorem; |
||
8 | use Kunstmaan\MediaBundle\Entity\Folder; |
||
9 | use Kunstmaan\MediaBundle\Entity\Media; |
||
10 | use Symfony\Component\DependencyInjection\Container; |
||
11 | use Symfony\Component\Finder\Finder; |
||
12 | use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
||
13 | use Symfony\Component\Yaml\Yaml; |
||
14 | |||
15 | /** |
||
16 | * Generates all classes/files for a new pagepart |
||
17 | */ |
||
18 | class PagePartGenerator extends KunstmaanGenerator |
||
19 | { |
||
20 | /** |
||
21 | * @var BundleInterface |
||
22 | */ |
||
23 | private $bundle; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $entity; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $prefix; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $fields; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $sections; |
||
44 | |||
45 | /** |
||
46 | * Generate the pagepart. |
||
47 | * |
||
48 | * @param BundleInterface $bundle The bundle |
||
49 | * @param string $entity The entity name |
||
50 | * @param string $prefix The database prefix |
||
51 | * @param array $fields The fields |
||
52 | * @param array $sections The page sections |
||
53 | * @param bool $behatTest If we need to generate a behat test for this pagepart |
||
54 | * |
||
55 | * @throws \RuntimeException |
||
56 | */ |
||
57 | public function generate(BundleInterface $bundle, $entity, $prefix, array $fields, array $sections, $behatTest) |
||
58 | { |
||
59 | $this->bundle = $bundle; |
||
60 | $this->entity = $entity; |
||
61 | $this->prefix = $prefix; |
||
62 | $this->fields = $fields; |
||
63 | $this->sections = $sections; |
||
64 | |||
65 | $this->generatePagePartEntity(); |
||
66 | $this->generateFormType(); |
||
67 | $this->generateResourceTemplate(); |
||
68 | $this->generateSectionConfig(); |
||
69 | if ($behatTest) { |
||
70 | $this->generateBehatTest(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Generate the pagepart entity. |
||
76 | * |
||
77 | * @throws \RuntimeException |
||
78 | */ |
||
79 | private function generatePagePartEntity() |
||
80 | { |
||
81 | if (file_exists($this->bundle->getPath() . '/Entity/PageParts/AbstractPagePart.php')) { |
||
82 | $abstractClass = $this->bundle->getNamespace() . '\Entity\PageParts\AbstractPagePart'; |
||
83 | } else { |
||
84 | $abstractClass = 'Kunstmaan\PagePartBundle\Entity\AbstractPagePart'; |
||
85 | } |
||
86 | |||
87 | [$entityCode, $entityPath] = $this->generateEntity( |
||
0 ignored issues
–
show
|
|||
88 | $this->bundle, |
||
89 | $this->entity, |
||
90 | $this->fields, |
||
91 | 'PageParts', |
||
92 | $this->prefix, |
||
93 | $abstractClass |
||
94 | ); |
||
95 | |||
96 | // Add some extra functions in the generated entity :s |
||
97 | $params = array( |
||
98 | 'bundle' => $this->bundle->getName(), |
||
99 | 'pagepart' => $this->entity, |
||
100 | 'adminType' => '\\' . $this->bundle->getNamespace() . '\\Form\\PageParts\\' . $this->entity . 'AdminType', |
||
101 | 'isV4' => $this->isSymfony4(), |
||
102 | ); |
||
103 | $extraCode = $this->render('/Entity/PageParts/ExtraFunctions.php', $params); |
||
104 | |||
105 | $pos = strrpos($entityCode, "\n}"); |
||
106 | $trimmed = substr($entityCode, 0, $pos); |
||
107 | $entityCode = $trimmed."\n\n".$extraCode."\n}\n"; |
||
108 | |||
109 | // Write class to filesystem |
||
110 | $this->filesystem->mkdir(dirname($entityPath)); |
||
111 | file_put_contents($entityPath, $entityCode); |
||
112 | |||
113 | $this->assistant->writeLine('Generating entity : <info>OK</info>'); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Generate the admin form type entity. |
||
118 | */ |
||
119 | private function generateFormType() |
||
120 | { |
||
121 | $this->generateEntityAdminType($this->bundle, $this->entity, 'PageParts', $this->fields); |
||
122 | |||
123 | $this->assistant->writeLine('Generating form type : <info>OK</info>'); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Generate the twig template. |
||
128 | */ |
||
129 | private function generateResourceTemplate() |
||
130 | { |
||
131 | $savePath = $this->getTemplateDir($this->bundle) . '/PageParts/'.$this->entity.'/view.html.twig'; |
||
132 | |||
133 | $params = array( |
||
134 | 'pagepart' => strtolower( |
||
135 | preg_replace('/([a-z])([A-Z])/', '$1-$2', str_ireplace('PagePart', '', $this->entity)) |
||
136 | ) . '-pp', |
||
137 | 'fields' => $this->fields, |
||
138 | ); |
||
139 | $this->renderFile('/Resources/views/PageParts/view.html.twig', $savePath, $params); |
||
140 | |||
141 | $this->assistant->writeLine('Generating template : <info>OK</info>'); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Update the page section config files |
||
146 | */ |
||
147 | private function generateSectionConfig() |
||
148 | { |
||
149 | if (count($this->sections) > 0) { |
||
150 | if ($this->isSymfony4()) { |
||
151 | $dir = $this->container->getParameter('kernel.project_dir') . '/config/kunstmaancms/pageparts/'; |
||
152 | } else { |
||
153 | $dir = $this->bundle->getPath().'/Resources/config/pageparts/'; |
||
154 | } |
||
155 | |||
156 | foreach ($this->sections as $section) { |
||
157 | $data = Yaml::parse(file_get_contents($dir . $section)); |
||
158 | |||
159 | // grab the sf4 style data if needed |
||
160 | if (array_key_exists('kunstmaan_page_part', $data) && array_key_exists('pageparts', $data['kunstmaan_page_part'])) { |
||
161 | $pagePartKey = array_keys($data['kunstmaan_page_part']['pageparts'])[0]; |
||
162 | $data = $data['kunstmaan_page_part']['pageparts'][$pagePartKey]; |
||
163 | } |
||
164 | |||
165 | if (!array_key_exists('types', $data)) { |
||
166 | $data['types'] = array(); |
||
167 | } |
||
168 | |||
169 | $data['types'][] = array( |
||
170 | 'name' => str_replace('PagePart', '', $this->entity), |
||
171 | 'class' => $this->bundle->getNamespace() . '\\Entity\\PageParts\\' . $this->entity, |
||
172 | ); |
||
173 | |||
174 | // restore the sf4 style data |
||
175 | if (isset($pagePartKey)) { |
||
176 | $data = [ |
||
177 | 'kunstmaan_page_part' => [ |
||
178 | 'pageparts' => [$pagePartKey => $data], |
||
179 | ], |
||
180 | ]; |
||
181 | } |
||
182 | |||
183 | $ymlData = Yaml::dump($data, 5); |
||
184 | file_put_contents($dir . $section, $ymlData); |
||
185 | } |
||
186 | |||
187 | $this->assistant->writeLine('Updating section config : <info>OK</info>'); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Generate the admin form type entity. |
||
193 | */ |
||
194 | private function generateBehatTest() |
||
195 | { |
||
196 | $configDir = $this->bundle->getPath() . '/Resources/config'; |
||
197 | |||
198 | // Get the context names for each section config file |
||
199 | $sectionInfo = array(); |
||
200 | $dir = $configDir . '/pageparts/'; |
||
201 | foreach ($this->sections as $section) { |
||
202 | $data = Yaml::parse(file_get_contents($dir . $section)); |
||
203 | $sectionInfo[basename($section, '.yml')] = array('context' => $data['context'], 'pagetempates' => array()); |
||
204 | } |
||
205 | |||
206 | /* |
||
207 | Example $sectionInfo contents: |
||
208 | Array |
||
209 | ( |
||
210 | [main] => Array |
||
211 | ( |
||
212 | [context] => main |
||
213 | [pagetempates] => Array |
||
214 | ( |
||
215 | ) |
||
216 | ) |
||
217 | ) |
||
218 | */ |
||
219 | |||
220 | // Get a list of page templates that use this context |
||
221 | $templateFinder = new Finder(); |
||
222 | $templateFinder->files()->in($configDir . '/pagetemplates')->name('*.yml'); |
||
223 | |||
224 | $contextTemplates = array(); |
||
225 | foreach ($templateFinder as $templatePath) { |
||
226 | $parts = explode('/', $templatePath); |
||
227 | $fileName = basename($parts[count($parts) - 1], '.yml'); |
||
228 | |||
229 | $data = Yaml::parse(file_get_contents($templatePath)); |
||
230 | $templateName = $data['name']; |
||
231 | if (array_key_exists('rows', $data) && is_array($data['rows'])) { |
||
232 | foreach ($data['rows'] as $row) { |
||
233 | if (is_array($row) && array_key_exists('regions', $row) && is_array($row['regions'])) { |
||
234 | foreach ($row['regions'] as $region) { |
||
235 | $contextTemplates[$region['name']][$fileName] = $templateName; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | /* |
||
243 | Example $contextTemplates contents: |
||
244 | Array |
||
245 | ( |
||
246 | [main] => Array |
||
247 | ( |
||
248 | [full-width-page] => Full width page |
||
249 | [homepage] => Home page |
||
250 | [sidebar-page] => Page with left sidebar |
||
251 | ) |
||
252 | [top] => Array |
||
253 | ( |
||
254 | [homepage] => Home page |
||
255 | ) |
||
256 | [sidebar] => Array |
||
257 | ( |
||
258 | [homepage] => Home page |
||
259 | [sidebar-page] => Page with left sidebar |
||
260 | ) |
||
261 | ) |
||
262 | */ |
||
263 | |||
264 | // Link the page templates to the sections |
||
265 | foreach ($sectionInfo as $fileName => $info) { |
||
266 | $context = $info['context']; |
||
267 | if (array_key_exists($context, $contextTemplates)) { |
||
268 | $sectionInfo[$fileName]['pagetempates'] = $contextTemplates[$context]; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /* |
||
273 | Example $sectionInfo contents: |
||
274 | Array |
||
275 | ( |
||
276 | [main] => Array |
||
277 | ( |
||
278 | [context] => main |
||
279 | [pagetempates] => Array |
||
280 | ( |
||
281 | [full-width-page] => Full width page |
||
282 | [homepage] => Home page |
||
283 | [sidebar-page] => Page with left sidebar |
||
284 | ) |
||
285 | |||
286 | ) |
||
287 | |||
288 | ) |
||
289 | */ |
||
290 | |||
291 | $folder = $this->registry->getRepository(Folder::class)->findOneBy(array('rel' => 'image')); |
||
292 | $images = $this->registry->getRepository(Media::class)->findBy( |
||
293 | array('folder' => $folder, 'deleted' => false), |
||
294 | array(), |
||
295 | 2 |
||
296 | ); |
||
297 | |||
298 | // Get all the available pages |
||
299 | $finder = new Finder(); |
||
300 | $finder->files()->in($this->bundle->getPath() . '/Entity/Pages')->name('*.php'); |
||
301 | |||
302 | $pages = array(); |
||
303 | foreach ($finder as $pageFile) { |
||
304 | $parts = explode('/', $pageFile); |
||
305 | $className = basename($parts[count($parts) - 1], '.php'); |
||
306 | |||
307 | $contents = file_get_contents($pageFile); |
||
308 | if (strpos($contents, 'abstract class') === false && strpos($contents, 'interface ') === false && strpos($contents, 'trait ') === false) { |
||
309 | $classNamespace = '\\' . $this->bundle->getNamespace() . '\Entity\Pages\\' . $className; |
||
310 | $entity = new $classNamespace(); |
||
311 | |||
312 | if (!method_exists($entity, 'getPagePartAdminConfigurations') || !method_exists( |
||
313 | $entity, |
||
314 | 'getPageTemplates' |
||
315 | ) |
||
316 | ) { |
||
317 | continue; |
||
318 | } |
||
319 | |||
320 | $ppConfigs = $entity->getPagePartAdminConfigurations(); |
||
321 | $ptConfigs = $entity->getPageTemplates(); |
||
322 | |||
323 | foreach ($ppConfigs as $ppConfig) { |
||
324 | $parts = explode(':', $ppConfig); |
||
325 | $ppConfigFilename = $parts[count($parts) - 1]; |
||
326 | |||
327 | // Context found in this Page class |
||
328 | if (array_key_exists($ppConfigFilename, $sectionInfo)) { |
||
329 | // Search for templates |
||
330 | foreach ($ptConfigs as $ptConfig) { |
||
331 | $parts = explode(':', $ptConfig); |
||
332 | $ptConfigFilename = $parts[count($parts) - 1]; |
||
333 | |||
334 | // Page template found |
||
335 | if (array_key_exists($ptConfigFilename, $sectionInfo[$ppConfigFilename]['pagetempates'])) { |
||
336 | // Get all page properties |
||
337 | $form = $this->container->get('form.factory')->create($entity->getDefaultAdminType()); |
||
338 | $children = $form->createView()->children; |
||
339 | |||
340 | $pageFields = array(); |
||
341 | foreach ($children as $field) { |
||
342 | $name = $field->vars['name']; |
||
343 | $attr = $field->vars['attr']; |
||
344 | $blocks = $field->vars['block_prefixes']; |
||
345 | |||
346 | if ($name == 'title' || $name == 'pageTitle') { |
||
347 | continue; |
||
348 | } |
||
349 | |||
350 | if ($blocks[1] == 'hidden') { |
||
351 | // do nothing |
||
352 | } elseif ($blocks[1] == 'choice' && $blocks[1] == 'entity') { |
||
353 | // do nothing |
||
354 | } elseif ($blocks[1] == 'datetime') { |
||
355 | $pageFields[]['datetime'] = array( |
||
356 | 'label' => $this->labelCase($name), |
||
357 | 'date_random' => DateTime::date('d/m/Y'), |
||
358 | 'time_random' => DateTime::time('H:i'), |
||
359 | ); |
||
360 | } elseif ($blocks[1] == 'number') { |
||
361 | $pageFields[]['decimal'] = array( |
||
362 | 'label' => $this->labelCase($name), |
||
363 | 'random' => Base::randomFloat(2, 0, 99999), |
||
364 | ); |
||
365 | } elseif ($blocks[1] == 'integer') { |
||
366 | $pageFields[]['integer'] = array( |
||
367 | 'label' => $this->labelCase($name), |
||
368 | 'random' => Base::randomNumber(3000, 99999), |
||
369 | ); |
||
370 | } elseif ($blocks[1] == 'checkbox') { |
||
371 | $pageFields[]['boolean'] = array( |
||
372 | 'label' => $this->labelCase($name), |
||
373 | ); |
||
374 | } elseif ($blocks[1] == 'media') { |
||
375 | $id = (count($images) > 0 ? $images[0]->getId() : 1); |
||
376 | $pageFields[]['media'] = array( |
||
377 | 'label' => $this->labelCase($name), |
||
378 | 'random' => $id, |
||
379 | ); |
||
380 | } elseif ($blocks[2] == 'urlchooser') { |
||
381 | $pageFields[]['link'] = array( |
||
382 | 'label' => $this->labelCase($name), |
||
383 | 'random' => 'http://www.' . strtolower(Lorem::word()) . '.com', |
||
384 | ); |
||
385 | } elseif ($blocks[2] == 'textarea' && array_key_exists( |
||
386 | 'class', |
||
387 | $attr |
||
388 | ) && $attr['class'] == 'js-rich-editor rich-editor' |
||
389 | ) { |
||
390 | $pageFields[]['rich_text'] = array( |
||
391 | 'label' => $this->labelCase($name), |
||
392 | 'random' => Lorem::sentence(), |
||
393 | ); |
||
394 | } elseif ($blocks[2] == 'textarea' || $blocks[1] == 'text') { |
||
395 | $pageFields[]['text'] = array( |
||
396 | 'label' => $this->labelCase($name), |
||
397 | 'random' => Lorem::word(), |
||
398 | ); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | $pages[] = array( |
||
403 | 'name' => $className, |
||
404 | 'section' => $sectionInfo[$ppConfigFilename]['context'], |
||
405 | 'template' => $sectionInfo[$ppConfigFilename]['pagetempates'][$ptConfigFilename], |
||
406 | 'fields' => $pageFields, |
||
407 | ); |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | /* |
||
416 | Example $pages contents: |
||
417 | Array |
||
418 | ( |
||
419 | [0] => Array |
||
420 | ( |
||
421 | [name] => ContentPage |
||
422 | [section] => main |
||
423 | [template] => Page with left sidebar |
||
424 | [fields] => Array |
||
425 | ( |
||
426 | ... |
||
427 | ) |
||
428 | ) |
||
429 | [1] => Array |
||
430 | ( |
||
431 | [name] => ContentPage |
||
432 | [section] => main |
||
433 | [template] => Full width page |
||
434 | [fields] => Array |
||
435 | ( |
||
436 | ... |
||
437 | ) |
||
438 | ) |
||
439 | [2] => Array |
||
440 | ( |
||
441 | [name] => HomePage |
||
442 | [section] => main |
||
443 | [template] => Home page |
||
444 | [fields] => Array |
||
445 | ( |
||
446 | ... |
||
447 | ) |
||
448 | ) |
||
449 | ) |
||
450 | */ |
||
451 | |||
452 | // Add some random values in the field array, so that this values can be uses as test values |
||
453 | foreach ($this->fields as $fkey => $fieldSet) { |
||
454 | foreach ($fieldSet as $key => $values) { |
||
455 | switch ($key) { |
||
456 | case 'multi_line': |
||
457 | case 'single_line': |
||
458 | $values[0]['random1'] = Lorem::word(); |
||
459 | $values[0]['random2'] = Lorem::word(); |
||
460 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
461 | |||
462 | break; |
||
463 | case 'rich_text': |
||
464 | $values[0]['random1'] = Lorem::sentence(); |
||
465 | $values[0]['random2'] = Lorem::sentence(); |
||
466 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
467 | |||
468 | break; |
||
469 | case 'link': |
||
470 | $values['url']['random1'] = 'http://www.' . strtolower(Lorem::word()) . '.com'; |
||
471 | $values['url']['random2'] = 'http://www.' . strtolower(Lorem::word()) . '.com'; |
||
472 | $values['url']['lName'] = $this->labelCase($values['url']['fieldName']); |
||
473 | $values['text']['random1'] = Lorem::word(); |
||
474 | $values['text']['random2'] = Lorem::word(); |
||
475 | $values['text']['lName'] = $this->labelCase($values['text']['fieldName']); |
||
476 | $values['new_window']['lName'] = $this->labelCase($values['new_window']['fieldName']); |
||
477 | |||
478 | break; |
||
479 | case 'image': |
||
480 | if (count($images) > 0) { |
||
481 | if (count($images) > 1) { |
||
482 | $values['image']['id_random1'] = $images[0]->getId(); |
||
483 | $values['image']['url_random1'] = $images[0]->getUrl(); |
||
484 | $values['image']['id_random2'] = $images[1]->getId(); |
||
485 | $values['image']['url_random2'] = $images[1]->getUrl(); |
||
486 | } else { |
||
487 | $values['image']['id_random1'] = $values['image']['id_random2'] = $images[0]->getId(); |
||
488 | $values['image']['url_random1'] = $values['image']['url_random2'] = $images[0]->getUrl( |
||
489 | ); |
||
490 | } |
||
491 | } else { |
||
492 | $values['image']['id_random1'] = $values['image']['id_random2'] = '1'; |
||
493 | $values['image']['url_random1'] = $values['image']['url_random2'] = 'XXX'; |
||
494 | } |
||
495 | $values['image']['lName'] = $this->labelCase($values['image']['fieldName']); |
||
496 | $values['alt_text']['random1'] = Lorem::word(); |
||
497 | $values['alt_text']['random2'] = Lorem::word(); |
||
498 | $values['alt_text']['lName'] = $this->labelCase($values['alt_text']['fieldName']); |
||
499 | |||
500 | break; |
||
501 | case 'boolean': |
||
502 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
503 | |||
504 | break; |
||
505 | case 'integer': |
||
506 | $values[0]['random1'] = Base::randomNumber(3000, 99999); |
||
507 | $values[0]['random2'] = Base::randomNumber(3000, 99999); |
||
508 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
509 | |||
510 | break; |
||
511 | case 'decimal': |
||
512 | $values[0]['random1'] = Base::randomFloat(2, 0, 99999); |
||
513 | $values[0]['random2'] = Base::randomFloat(2, 0, 99999); |
||
514 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
515 | |||
516 | break; |
||
517 | case 'datetime': |
||
518 | $values[0]['date_random1'] = DateTime::date('d/m/Y'); |
||
519 | $values[0]['date_random2'] = DateTime::date('d/m/Y'); |
||
520 | $values[0]['time_random1'] = DateTime::time('H:i'); |
||
521 | $values[0]['time_random2'] = DateTime::time('H:i'); |
||
522 | $dparts = explode('/', $values[0]['date_random1']); |
||
523 | $values[0]['datetime_random1'] = $dparts[2] . '-' . $dparts[1] . '-' . $dparts[0] . ' ' . $values[0]['time_random1'] . ':00'; |
||
524 | $dparts = explode('/', $values[0]['date_random2']); |
||
525 | $values[0]['datetime_random2'] = $dparts[2] . '-' . $dparts[1] . '-' . $dparts[0] . ' ' . $values[0]['time_random2'] . ':00'; |
||
526 | $values[0]['lName'] = $this->labelCase($values[0]['fieldName']); |
||
527 | |||
528 | break; |
||
529 | } |
||
530 | |||
531 | $this->fields[$fkey][$key] = $values; |
||
532 | } |
||
533 | } |
||
534 | |||
535 | $params = array( |
||
536 | 'name' => $this->entity, |
||
537 | 'pages' => $pages, |
||
538 | 'fields' => $this->fields, |
||
539 | ); |
||
540 | $this->renderFile( |
||
541 | '/Features/PagePart.feature', |
||
542 | $this->bundle->getPath() . '/Features/Admin' . $this->entity . '.feature', |
||
543 | $params |
||
544 | ); |
||
545 | |||
546 | $this->assistant->writeLine('Generating behat test : <info>OK</info>'); |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Camel case string to space delimited string that will be used for form labels. |
||
551 | * |
||
552 | * @param string $text |
||
553 | * |
||
554 | * @return string |
||
555 | */ |
||
556 | private function labelCase($text) |
||
557 | { |
||
558 | return ucfirst(str_replace('_', ' ', Container::underscore($text))); |
||
559 | } |
||
560 | } |
||
561 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.