forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorControllerCommand.php
More file actions
160 lines (140 loc) · 5.73 KB
/
Copy pathGeneratorControllerCommand.php
File metadata and controls
160 lines (140 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* @file
* Contains Drupal\AppConsole\Command\GeneratorControllerCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ServicesTrait;
use Drupal\AppConsole\Command\Helper\ConfirmationTrait;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Generator\ControllerGenerator;
class GeneratorControllerCommand extends GeneratorCommand
{
use ModuleTrait;
use ServicesTrait;
use ConfirmationTrait;
protected function configure()
{
$this
->setName('generate:controller')
->setDescription($this->trans('commands.generate.controller.description'))
->setHelp($this->trans('commands.generate.controller.command.help'))
->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
->addOption('class-name', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.controller.options.class-name'))
->addOption('method-name', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.controller.options.method-name'))
->addOption('route', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.controller.options.route'))
->addOption('services', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $this->trans('commands.common.options.services'))
->addOption('test', '', InputOption::VALUE_NONE, $this->trans('commands.generate.controller.options.test'));
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
if ($this->confirmationQuestion($input, $output, $dialog)) {
return;
}
$module = $input->getOption('module');
$class_name = $input->getOption('class-name');
$method_name = $input->getOption('method-name');
$route = $input->getOption('route');
$test = $input->getOption('test');
$services = $input->getOption('services');
$learning = false;
if ($input->hasOption('learning')) {
$learning = $input->getOption('learning');
}
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::buildServices
$build_services = $this->buildServices($services);
// Controller machine name
$class_machine_name = $this->getStringUtils()->camelCaseToMachineName($class_name);
$generator = $this->getGenerator();
$generator->setLearning($learning);
$generator->generate($module, $class_name, $method_name, $route, $test, $build_services, $class_machine_name);
$this->getHelper('chain')->addCommand('router:rebuild');
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);
// --class-name option
$class_name = $input->getOption('class-name');
if (!$class_name) {
$class_name = 'DefaultController';
$class_name = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('commands.generate.controller.questions.class-name'), $class_name),
function ($class_name) {
return $this->validateClassName($class_name);
},
false,
$class_name,
null
);
}
$input->setOption('class-name', $class_name);
// --method-name option & --route option
if ($class_name != 'DefaultController') {
$method_name = $input->getOption('method-name');
if (!$method_name) {
$method_name = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.controller.questions.method-name'), 'index'),
'index'
);
}
$route = $input->getOption('route');
if (!$route) {
$route = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.controller.questions.route'),
$module . '/' . $method_name),
$module . '/' . $method_name
);
}
} else {
$method_name = 'hello';
$route = $module . '/hello/{name}';
}
$input->setOption('method-name', $method_name);
$input->setOption('route', $route);
// --test option
$test = $input->getOption('test');
if (!$test && $dialog->askConfirmation(
$output,
$dialog->getQuestion($this->trans('commands.generate.controller.questions.test'), 'yes', '?'),
true
)
) {
$test = true;
}
$input->setOption('test', $test);
// --services option
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
$services_collection = $this->servicesQuestion($output, $dialog);
$input->setOption('services', $services_collection);
}
/**
* @return \Drupal\AppConsole\Generator\ControllerGenerator
*/
protected function createGenerator()
{
return new ControllerGenerator();
}
}