4 * Contains \Drupal\Console\Command\Generate\TwigExtensionCommand.
7 namespace Drupal\Console\Command\Generate;
9 use Symfony\Component\Console\Command\Command;
10 use Drupal\Console\Command\Shared\ConfirmationTrait;
11 use Drupal\Console\Command\Shared\ModuleTrait;
12 use Drupal\Console\Command\Shared\ServicesTrait;
13 use Drupal\Console\Generator\TwigExtensionGenerator;
14 use Drupal\Console\Core\Style\DrupalStyle;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Utils\Site;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
25 * Class TwigExtensionCommand
27 * @package Drupal\Console\Command\Generate
29 class TwigExtensionCommand extends Command
33 use ConfirmationTrait;
34 use ContainerAwareCommandTrait;
39 protected $extensionManager;
42 * @var TwigExtensionGenerator
52 * @var StringConverter
54 protected $stringConverter;
59 protected $chainQueue;
63 * TwigExtensionCommand constructor.
65 * @param Manager $extensionManager
66 * @param TwigExtensionGenerator $generator
67 * @param StringConverter $stringConverter
68 * @param ChainQueue $chainQueue
70 public function __construct(
71 Manager $extensionManager,
72 TwigExtensionGenerator $generator,
74 StringConverter $stringConverter,
75 ChainQueue $chainQueue
77 $this->extensionManager = $extensionManager;
78 $this->generator = $generator;
80 $this->stringConverter = $stringConverter;
81 $this->chainQueue = $chainQueue;
82 parent::__construct();
88 protected function configure()
91 ->setName('generate:twig:extension')
92 ->setDescription($this->trans('commands.generate.twig.extension.description'))
93 ->setHelp($this->trans('commands.generate.twig.extension.help'))
97 InputOption::VALUE_REQUIRED,
98 $this->trans('commands.common.options.module')
103 InputOption::VALUE_REQUIRED,
104 $this->trans('commands.generate.twig.extension.options.name')
109 InputOption::VALUE_REQUIRED,
110 $this->trans('commands.common.options.class')
115 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
116 $this->trans('commands.common.options.services')
123 protected function execute(InputInterface $input, OutputInterface $output)
125 $io = new DrupalStyle($input, $output);
127 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
128 if (!$this->confirmGeneration($io)) {
132 $module = $input->getOption('module');
133 $name = $input->getOption('name');
134 $class = $input->getOption('class');
135 $services = $input->getOption('services');
136 // Add renderer service as first parameter.
137 array_unshift($services, 'renderer');
140 // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
141 $build_services = $this->buildServices($services);
143 $this->generator->generate($module, $name, $class, $build_services);
145 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
151 protected function interact(InputInterface $input, OutputInterface $output)
153 $io = new DrupalStyle($input, $output);
156 $module = $input->getOption('module');
158 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
159 $module = $this->moduleQuestion($io);
160 $input->setOption('module', $module);
164 $name = $input->getOption('name');
167 $this->trans('commands.generate.twig.extension.questions.twig-extension'),
168 $module.'.twig.extension'
170 $input->setOption('name', $name);
174 $class = $input->getOption('class');
177 $this->trans('commands.common.options.class'),
178 'DefaultTwigExtension'
180 $input->setOption('class', $class);
184 $services = $input->getOption('services');
186 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
187 $services = $this->servicesQuestion($io);
188 $input->setOption('services', $services);