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');
139 // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
140 $build_services = $this->buildServices($services);
142 $this->generator->generate($module, $name, $class, $build_services);
144 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
152 protected function interact(InputInterface $input, OutputInterface $output)
154 $io = new DrupalStyle($input, $output);
157 $module = $input->getOption('module');
159 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
160 $module = $this->moduleQuestion($io);
161 $input->setOption('module', $module);
165 $name = $input->getOption('name');
168 $this->trans('commands.generate.twig.extension.questions.twig-extension'),
169 $module.'.twig.extension'
171 $input->setOption('name', $name);
175 $class = $input->getOption('class');
178 $this->trans('commands.common.options.class'),
179 'DefaultTwigExtension'
181 $input->setOption('class', $class);
185 $services = $input->getOption('services');
187 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
188 $services = $this->servicesQuestion($io);
189 $input->setOption('services', $services);