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']);
172 protected function interact(InputInterface $input, OutputInterface $output)
174 $io = new DrupalStyle($input, $output);
177 $module = $input->getOption('module');
179 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
180 $module = $this->moduleQuestion($io);
181 $input->setOption('module', $module);
185 $name = $input->getOption('name');
188 $this->trans('commands.generate.service.questions.service-name'),
191 $input->setOption('name', $name);
195 $class = $input->getOption('class');
198 $this->trans('commands.generate.service.questions.class'),
201 $input->setOption('class', $class);
204 // --interface option
205 $interface = $input->getOption('interface');
207 $interface = $io->confirm(
208 $this->trans('commands.generate.service.questions.interface'),
211 $input->setOption('interface', $interface);
214 // --interface_name option
215 $interface_name = $input->getOption('interface_name');
216 if ($interface && !$interface_name) {
217 $interface_name = $io->askEmpty(
218 $this->trans('commands.generate.service.questions.interface_name')
220 $input->setOption('interface_name', $interface_name);
224 $services = $input->getOption('services');
226 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
227 $services = $this->servicesQuestion($io);
228 $input->setOption('services', $services);
231 // --path_service option
232 $path_service = $input->getOption('path_service');
233 if (!$path_service) {
234 $path_service = $io->ask(
235 $this->trans('commands.generate.service.questions.path'),
236 '/modules/custom/' . $module . '/src/'
238 $input->setOption('path_service', $path_service);