1: <?php
2: namespace TIC\MakeBundle\Maker;
3:
4: use Symfony\Component\Console\Command\Command;
5: use Symfony\Component\Console\Input\InputInterface;
6: use Symfony\Component\Console\Input\InputArgument;
7: use Symfony\Component\Console\Input\InputOption;
8: use Doctrine\Common\Annotations\Annotation;
9:
10: use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
11: use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
12: use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
13:
14: /**
15: *
16: */
17: final class MakeViews extends BaseMaker
18: {
19: private $doctrineHelper;
20:
21: public function __construct(DoctrineHelper $doctrineHelper)
22: {
23: parent::__construct();
24: $this->doctrineHelper = $doctrineHelper;
25: }
26:
27: /**
28: * Configure the command: set description, input arguments, options, etc.
29: */
30: protected function ticConfigure(Command $command)
31: {
32: $command->addArgument('name', InputArgument::OPTIONAL, "Nom, chemin ou classname du controller CRUD correspondant");
33: $command->addOption('split', null, InputOption::VALUE_NONE, "Séparer les actions create & update");
34: }
35:
36: /**
37: * Called after normal code generation: allows you to do anything.
38: */
39: protected function ticGenerate(InputInterface $input)
40: {
41: $inputName = $this->fixClassName($input->getArgument('name'));
42: $this->io->title("Construction des templates des vues CRUD : '$inputName'");
43:
44: // détermination des variables à utiliser
45: $variables = $this->getVariablesFromName($inputName);
46: $doctrineDetails = $this->doctrineHelper->createDoctrineDetails($variables['item_class']);
47:
48: // affichage des valeurs des variables
49: $this->io->section("Paramètres utilisés :");
50: $this->io->table(["Variable","Valeur"], self::hash2list($variables));
51: if (null === $doctrineDetails)
52: throw new RuntimeCommandException("Entité introuvable : " . $variables['item_class']);
53: if ($this->test) return;
54:
55: if (! $this->confirm("Lancer la construction ?")) return;
56: $results = array();
57:
58: // construction Twig templates
59: if ($this->twig) {
60: $this->io->section("Construction des templates...");
61: $tpls = ['base', 'list', '_list_table', 'show', '_show_main'];
62: $tpls = array_merge($tpls, $input->getOption('split') ? ['create', 'update'] : ['form']);
63: $paths = $this->ticGenerateTemplates('crud', $tpls, $variables + [
64: 'entity_key' => $doctrineDetails->getIdentifier(),
65: 'entity_fields' => $doctrineDetails->getDisplayFields(),
66: # 'ctrl_path' => $controllerPath,
67: ]);
68: $this->io->success("Génération des views twig : " . implode(", ", $tpls));
69: foreach ($tpls as $id => $tpl) $results[] = [ "Template '$tpl'", $paths[$id] ];
70: }
71:
72: // récapitulatif des fichiers générés
73: return $this->summary($results);
74: }
75:
76: }
77: