5 * Contains \Drupal\Console\Command\Generate\ServiceCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ServicesTrait;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Generator\ServiceGenerator;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Console\Core\Utils\StringConverter;
25 * Class ServiceCommand
27 * @package Drupal\Console\Command\Generate
29 class ServiceCommand extends Command
33 use ConfirmationTrait;
34 use ContainerAwareCommandTrait;
39 protected $extensionManager;
42 * @var ServiceGenerator
47 * @var StringConverter
49 protected $stringConverter;
54 protected $chainQueue;
57 * ServiceCommand constructor.
59 * @param Manager $extensionManager
60 * @param ServiceGenerator $generator
61 * @param StringConverter $stringConverter
62 * @param ChainQueue $chainQueue
64 public function __construct(
65 Manager $extensionManager,
66 ServiceGenerator $generator,
67 StringConverter $stringConverter,
68 ChainQueue $chainQueue
70 $this->extensionManager = $extensionManager;
71 $this->generator = $generator;
72 $this->stringConverter = $stringConverter;
73 $this->chainQueue = $chainQueue;
74 parent::__construct();
81 protected function configure()
84 ->setName('generate:service')
85 ->setDescription($this->trans('commands.generate.service.description'))
86 ->setHelp($this->trans('commands.generate.service.description'))
90 InputOption::VALUE_REQUIRED,
91 $this->trans('commands.common.options.module')
96 InputOption::VALUE_REQUIRED,
97 $this->trans('commands.generate.service.options.name')
102 InputOption::VALUE_REQUIRED,
103 $this->trans('commands.generate.service.options.class')
108 InputOption::VALUE_NONE,
109 $this->trans('commands.common.service.options.interface')
114 InputOption::VALUE_OPTIONAL,
115 $this->trans('commands.common.service.options.interface_name')
120 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
121 $this->trans('commands.common.options.services')
126 InputOption::VALUE_OPTIONAL,
127 $this->trans('commands.generate.service.options.path')
134 protected function execute(InputInterface $input, OutputInterface $output)
136 $io = new DrupalStyle($input, $output);
138 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
139 if (!$this->confirmGeneration($io)) {
143 $module = $input->getOption('module');
144 $name = $input->getOption('name');
145 $class = $input->getOption('class');
146 $interface = $input->getOption('interface');
147 $interface_name = $input->getOption('interface_name');
148 $services = $input->getOption('services');
149 $path_service = $input->getOption('path_service');
151 $available_services = $this->container->getServiceIds();
153 if (in_array($name, array_values($available_services))) {
154 throw new \Exception(
156 $this->trans('commands.generate.service.messages.service-already-taken'),
162 // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
163 $build_services = $this->buildServices($services);
164 $this->generator->generate($module, $name, $class, $interface, $interface_name, $build_services, $path_service);
166 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
174 protected function interact(InputInterface $input, OutputInterface $output)
176 $io = new DrupalStyle($input, $output);
179 $module = $input->getOption('module');
181 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
182 $module = $this->moduleQuestion($io);
183 $input->setOption('module', $module);
187 $name = $input->getOption('name');
190 $this->trans('commands.generate.service.questions.service-name'),
193 $input->setOption('name', $name);
197 $class = $input->getOption('class');
200 $this->trans('commands.generate.service.questions.class'),
203 $input->setOption('class', $class);
206 // --interface option
207 $interface = $input->getOption('interface');
209 $interface = $io->confirm(
210 $this->trans('commands.generate.service.questions.interface'),
213 $input->setOption('interface', $interface);
216 // --interface_name option
217 $interface_name = $input->getOption('interface_name');
218 if ($interface && !$interface_name) {
219 $interface_name = $io->askEmpty(
220 $this->trans('commands.generate.service.questions.interface_name')
222 $input->setOption('interface_name', $interface_name);
226 $services = $input->getOption('services');
228 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
229 $services = $this->servicesQuestion($io);
230 $input->setOption('services', $services);
233 // --path_service option
234 $path_service = $input->getOption('path_service');
235 if (!$path_service) {
236 $path_service = $io->ask(
237 $this->trans('commands.generate.service.questions.path'),
238 '/modules/custom/' . $module . '/src/'
240 $input->setOption('path_service', $path_service);