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: use TIC\MakeBundle\Renderer\FormTypeRenderer;
15:
16: /**
17: *
18: */
19: final class MakeCrud extends BaseMaker
20: {
21: private $doctrineHelper;
22: private $formTypeRenderer;
23:
24: public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer)
25: {
26: parent::__construct();
27: $this->doctrineHelper = $doctrineHelper;
28: $this->formTypeRenderer = $formTypeRenderer;
29: }
30:
31: protected $dependencies = array(
32: ['class' => Annotation::class, 'value' => 'doctrine/annotations'],
33: );
34:
35: /**
36: * Configure the command: set description, input arguments, options, etc.
37: */
38: protected function ticConfigure(Command $command)
39: {
40: $command->addArgument('name', InputArgument::OPTIONAL, "Nom, chemin ou classname du controller CRUD");
41: $command->addOption('split', null, InputOption::VALUE_NONE, "Séparer les actions create & update");
42: }
43:
44: /**
45: * Called after normal code generation: allows you to do anything.
46: */
47: protected function ticGenerate(InputInterface $input)
48: {
49: $inputName = $this->fixClassName($input->getArgument('name'));
50: $this->io->title("Construction du controller CRUD : '$inputName'");
51:
52: // détermination des variables à utiliser
53: $variables = $this->getVariablesFromName($inputName);
54: $doctrineDetails = $this->doctrineHelper->createDoctrineDetails($variables['item_class']);
55: $entityDetails = new ClassNameDetails($variables['item_class'], '');
56: $formDetails = new ClassNameDetails($variables['form_class'], '');
57:
58: // affichage des valeurs des variables
59: $this->io->section("Paramètres utilisés :");
60: $this->io->table(["Variable","Valeur"], self::hash2list($variables));
61: if (null === $doctrineDetails)
62: throw new RuntimeCommandException("Entité introuvable : " . $variables['item_class']);
63: if ($this->test) return;
64:
65: if (! $this->confirm("Lancer la construction ?")) return;
66: $results = array();
67:
68: // construction Controller
69: $this->io->section("Construction du controlleur...");
70: $controllerPath = $this->ticGenerateController(
71: $variables['ctrl_class'],
72: self::$skelPath . 'crud/Controller.tpl.php',
73: $variables + [
74: 'entity_key' => $doctrineDetails->getIdentifier(),
75: 'split_form' => $input->getOption('split'),
76: 'with_template' => $this->twig,
77: ]
78: );
79: $results[] = [ $variables['ctrl_class'], $controllerPath ];
80: $this->io->success("Génération du controlleur : " . $variables['ctrl_class']);
81:
82: // construction FormType
83: $this->io->section("Construction du formulaire...");
84: $formPath = $this->ticGenerateFormType($this->formTypeRenderer, $formDetails, $doctrineDetails, $entityDetails);
85: $results[] = [ $variables['form_class'], $formPath ];
86: $this->io->success("Génération du FormType : " . $variables['form_class']);
87:
88: // construction Twig templates
89: if ($this->twig) {
90: $this->io->section("Construction des templates...");
91: $tpls = ['base', 'list', '_list_table', 'show', '_show_main'];
92: $tpls = array_merge($tpls, $input->getOption('split') ? ['create', 'update'] : ['form']);
93: $paths = $this->ticGenerateTemplates('crud', $tpls, $variables + [
94: 'entity_key' => $doctrineDetails->getIdentifier(),
95: 'entity_fields' => $doctrineDetails->getDisplayFields(),
96: 'ctrl_path' => $controllerPath,
97: ]);
98: $this->io->success("Génération des views twig : " . implode(", ", $tpls));
99: foreach ($tpls as $id => $tpl) $results[] = [ "Template '$tpl'", $paths[$id] ];
100: }
101:
102: // récapitulatif des fichiers générés
103: return $this->summary($results);
104: }
105:
106: }
107: