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: /**
11: *
12: */
13: final class MakeController extends BaseMaker
14: {
15:
16: protected $dependencies = array(
17: ['class' => Annotation::class, 'value' => 'doctrine/annotations'],
18: );
19:
20: /**
21: * Configure the command: set description, input arguments, options, etc.
22: */
23: protected function ticConfigure(Command $command)
24: {
25: $command->addArgument('name', InputArgument::OPTIONAL, "Nom, chemin ou classname du controller");
26: $command->addOption('crud', null, InputOption::VALUE_NONE, "Ajouter les actions CRUD");
27: $command->addOption('split', null, InputOption::VALUE_NONE, "Séparer les actions create & update");
28: }
29:
30: /**
31: * Called after normal code generation: allows you to do anything.
32: */
33: protected function ticGenerate(InputInterface $input)
34: {
35: $inputName = $this->fixClassName($input->getArgument('name'));
36: $this->io->title("Construction du controller : '$inputName'");
37:
38: // détermination des variables à utiliser
39: $variables = $this->getVariablesFromName($inputName);
40: $tplFolder = $input->getOption('crud') ? "crud" : "controller";
41:
42: // affichage des valeurs des variables
43: $this->io->section("Paramètres utilisés :");
44: $this->io->table(["Variable","Valeur"], self::hash2list($variables));
45: if ($this->test) return;
46:
47: if (! $this->confirm("Lancer la construction ?")) return;
48: $results = array();
49:
50: // construction Controller
51: $this->io->section("Construction du controlleur...");
52: $controllerPath = $this->ticGenerateController(
53: $variables['ctrl_class'],
54: self::$skelPath . $tplFolder . '/Controller.tpl.php',
55: $variables + [
56: 'entity_key' => "id",
57: 'split_form' => $input->getOption('split'),
58: 'with_template' => $this->twig,
59: ]
60: );
61: $results[] = [ $variables['ctrl_class'], $controllerPath ];
62: $this->io->success("Génération du controlleur : " . $variables['ctrl_class']);
63:
64: // construction Twig templates
65: if ($this->twig) {
66: $this->io->section("Construction des templates...");
67: $tpls = ['index'];
68: if ($input->getOption('crud')) {
69: $tpls = ['base', 'list', '_list_table', 'show', '_show_main'];
70: $tpls = array_merge($tpls, $input->getOption('split') ? ['create', 'update'] : ['form']);
71: }
72: $paths = $this->ticGenerateTemplates($tplFolder, $tpls, $variables + [
73: 'entity_key' => "id",
74: 'entity_fields' => ["id" => ["fieldName"=>"id", "type"=>"integer"]],
75: 'ctrl_path' => $controllerPath,
76: ]);
77: $this->io->success("Génération des views twig : " . implode(", ", $tpls));
78: foreach ($tpls as $id => $tpl) $results[] = [ "Template '$tpl'", $paths[$id] ];
79: }
80:
81: // récapitulatif des fichiers générés
82: return $this->summary($results);
83: }
84:
85: }
86: