5 * Contains \Drupal\Console\Command\Generate\CacheContextCommand.
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\ModuleTrait;
14 use Drupal\Console\Generator\CacheContextGenerator;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Command\Shared\ServicesTrait;
22 use Drupal\Console\Core\Utils\StringConverter;
24 class CacheContextCommand extends Command
27 use ConfirmationTrait;
28 use ContainerAwareCommandTrait;
32 * @var CacheContextGenerator
39 protected $chainQueue;
44 protected $extensionManager;
47 * @var StringConverter
49 protected $stringConverter;
52 * CacheContextCommand constructor.
54 * @param CacheContextGenerator $generator
55 * @param ChainQueue $chainQueue
56 * @param Manager $extensionManager
57 * @param StringConverter $stringConverter
59 public function __construct(
60 CacheContextGenerator $generator,
61 ChainQueue $chainQueue,
62 Manager $extensionManager,
63 StringConverter $stringConverter
65 $this->generator = $generator;
66 $this->chainQueue = $chainQueue;
67 $this->extensionManager = $extensionManager;
68 $this->stringConverter = $stringConverter;
69 parent::__construct();
75 protected function configure()
78 ->setName('generate:cache:context')
79 ->setDescription($this->trans('commands.generate.cache.context.description'))
80 ->setHelp($this->trans('commands.generate.cache.context.description'))
81 ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
85 InputOption::VALUE_OPTIONAL,
86 $this->trans('commands.generate.cache.context.questions.name')
91 InputOption::VALUE_OPTIONAL,
92 $this->trans('commands.generate.cache.context.questions.class')
97 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
98 $this->trans('commands.common.options.services')
105 protected function execute(InputInterface $input, OutputInterface $output)
107 $io = new DrupalStyle($input, $output);
109 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
110 if (!$this->confirmGeneration($io)) {
114 $module = $input->getOption('module');
115 $cache_context = $input->getOption('cache_context');
116 $class = $input->getOption('class');
117 $services = $input->getOption('services');
119 // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
120 $buildServices = $this->buildServices($services);
122 $this->generator->generate($module, $cache_context, $class, $buildServices);
124 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
130 protected function interact(InputInterface $input, OutputInterface $output)
132 $io = new DrupalStyle($input, $output);
135 $module = $input->getOption('module');
137 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
138 $module = $this->moduleQuestion($io);
139 $input->setOption('module', $module);
142 // --cache_context option
143 $cache_context = $input->getOption('cache_context');
144 if (!$cache_context) {
145 $cache_context = $io->ask(
146 $this->trans('commands.generate.cache.context.questions.name'),
147 sprintf('%s', $module)
149 $input->setOption('cache_context', $cache_context);
153 $class = $input->getOption('class');
156 $this->trans('commands.generate.cache.context.questions.class'),
157 'DefaultCacheContext'
159 $input->setOption('class', $class);
163 $services = $input->getOption('services');
165 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
166 $services = $this->servicesQuestion($io);
167 $input->setOption('services', $services);