5 * Contains \Drupal\Console\Command\Generate\CacheContextCommand.
8 namespace Drupal\Console\Command\Generate;
10 use Drupal\Console\Utils\Validator;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Generator\CacheContextGenerator;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Command\ContainerAwareCommand;
18 use Drupal\Console\Core\Utils\ChainQueue;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Command\Shared\ServicesTrait;
21 use Drupal\Console\Core\Utils\StringConverter;
23 class CacheContextCommand extends ContainerAwareCommand
26 use ConfirmationTrait;
30 * @var CacheContextGenerator
37 protected $chainQueue;
42 protected $extensionManager;
45 * @var StringConverter
47 protected $stringConverter;
55 * CacheContextCommand constructor.
57 * @param CacheContextGenerator $generator
58 * @param ChainQueue $chainQueue
59 * @param Manager $extensionManager
60 * @param StringConverter $stringConverter
61 * @param Validator $validator
63 public function __construct(
64 CacheContextGenerator $generator,
65 ChainQueue $chainQueue,
66 Manager $extensionManager,
67 StringConverter $stringConverter,
70 $this->generator = $generator;
71 $this->chainQueue = $chainQueue;
72 $this->extensionManager = $extensionManager;
73 $this->stringConverter = $stringConverter;
74 $this->validator = $validator;
75 parent::__construct();
81 protected function configure()
84 ->setName('generate:cache:context')
85 ->setDescription($this->trans('commands.generate.cache.context.description'))
86 ->setHelp($this->trans('commands.generate.cache.context.description'))
90 InputOption::VALUE_REQUIRED,
91 $this->trans('commands.common.options.module')
96 InputOption::VALUE_OPTIONAL,
97 $this->trans('commands.generate.cache.context.options.name')
102 InputOption::VALUE_OPTIONAL,
103 $this->trans('commands.generate.cache.context.options.class')
108 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
109 $this->trans('commands.common.options.services')
110 )->setAliases(['gcc']);
116 protected function execute(InputInterface $input, OutputInterface $output)
118 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
119 if (!$this->confirmOperation()) {
123 $module = $input->getOption('module');
124 $cache_context = $input->getOption('cache-context');
125 $class = $this->validator->validateClassName($input->getOption('class'));
126 $services = $input->getOption('services');
128 // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
129 $buildServices = $this->buildServices($services);
131 $this->generator->generate([
133 'cache_context' => $cache_context,
135 'services' => $buildServices,
138 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
144 protected function interact(InputInterface $input, OutputInterface $output)
147 $module = $this->getModuleOption();
149 // --cache_context option
150 $cache_context = $input->getOption('cache-context');
151 if (!$cache_context) {
152 $cache_context = $this->getIo()->ask(
153 $this->trans('commands.generate.cache.context.questions.name'),
154 sprintf('%s', $module)
156 $input->setOption('cache-context', $cache_context);
160 $class = $input->getOption('class');
162 $class = $this->getIo()->ask(
163 $this->trans('commands.generate.cache.context.questions.class'),
164 'DefaultCacheContext',
166 return $this->validator->validateClassName($class);
169 $input->setOption('class', $class);
173 $services = $input->getOption('services');
175 // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
176 $services = $this->servicesQuestion();
177 $input->setOption('services', $services);