forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorServiceCommand.php
More file actions
135 lines (117 loc) · 4.7 KB
/
Copy pathGeneratorServiceCommand.php
File metadata and controls
135 lines (117 loc) · 4.7 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
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\GeneratorServiceCommand.
*/
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\ModuleTrait;
use Drupal\AppConsole\Generator\ServiceGenerator;
use Drupal\AppConsole\Command\Helper\ConfirmationTrait;
class GeneratorServiceCommand extends GeneratorCommand
{
use ServicesTrait;
use ModuleTrait;
use ConfirmationTrait;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('generate:service')
->setDescription($this->trans('commands.generate.service.description'))
->setHelp($this->trans('commands.generate.service.description'))
->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
->addOption('service-name', null, InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.service.options.service-name'))
->addOption('class-name', null, InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.service.options.class-name'))
->addOption('interface', null, InputOption::VALUE_OPTIONAL,
$this->trans('commands.common.service.options.interface'))
->addOption('services', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.common.options.services'));
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
// @see use Drupal\AppConsole\Command\Helper\ConfirmationTrait::confirmationQuestion
if ($this->confirmationQuestion($input, $output, $dialog)) {
return;
}
$module = $input->getOption('module');
$service_name = $input->getOption('service-name');
$class_name = $input->getOption('class-name');
$interface = $input->getOption('interface');
$services = $input->getOption('services');
$interface = ($interface===true || strtolower($interface)==='yes');
// @see Drupal\AppConsole\Command\Helper\ServicesTrait::buildServices
$build_services = $this->buildServices($services);
$this
->getGenerator()
->generate($module, $service_name, $class_name, $interface, $build_services);
$this->getHelper('chain')->addCommand('cache:rebuild', ['--cache' => 'all']);
}
/**
* {@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);
// --service-name option
$service_name = $input->getOption('service-name');
if (!$service_name) {
$service_name = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.service.questions.service-name'),
$module . '.default'),
$module . '.default'
);
}
$input->setOption('service-name', $service_name);
// --class-name option
$class_name = $input->getOption('class-name');
if (!$class_name) {
$class_name = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.service.questions.class-name'), 'DefaultService'),
'DefaultService'
);
}
$input->setOption('class-name', $class_name);
// --interface option
$interface = $input->getOption('interface');
if (!$interface) {
$interface = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.service.questions.interface'), 'yes', '?'),
true
);
}
$input->setOption('interface', $interface);
// --services option
$services = $input->getOption('services');
if (!$services) {
// @see Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
$services = $this->servicesQuestion($output, $dialog);
}
$input->setOption('services', $services);
}
protected function createGenerator()
{
return new ServiceGenerator();
}
}