Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / CacheContextCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\CacheContextCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
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;
23
24 class CacheContextCommand extends Command
25 {
26     use ModuleTrait;
27     use ConfirmationTrait;
28     use ContainerAwareCommandTrait;
29     use ServicesTrait;
30
31     /**
32    * @var CacheContextGenerator
33    */
34     protected $generator;
35
36     /**
37    * @var ChainQueue
38    */
39     protected $chainQueue;
40
41     /**
42    * @var Manager
43    */
44     protected $extensionManager;
45
46     /**
47    * @var StringConverter
48    */
49     protected $stringConverter;
50
51     /**
52    * CacheContextCommand constructor.
53    *
54    * @param CacheContextGenerator $generator
55    * @param ChainQueue            $chainQueue
56    * @param Manager               $extensionManager
57    * @param StringConverter       $stringConverter
58    */
59     public function __construct(
60         CacheContextGenerator $generator,
61         ChainQueue $chainQueue,
62         Manager $extensionManager,
63         StringConverter $stringConverter
64     ) {
65         $this->generator = $generator;
66         $this->chainQueue = $chainQueue;
67         $this->extensionManager = $extensionManager;
68         $this->stringConverter = $stringConverter;
69         parent::__construct();
70     }
71
72     /**
73    * {@inheritdoc}
74    */
75     protected function configure()
76     {
77         $this
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'))
82             ->addOption(
83                 'cache_context',
84                 null,
85                 InputOption::VALUE_OPTIONAL,
86                 $this->trans('commands.generate.cache.context.questions.name')
87             )
88             ->addOption(
89                 'class',
90                 null,
91                 InputOption::VALUE_OPTIONAL,
92                 $this->trans('commands.generate.cache.context.questions.class')
93             )
94             ->addOption(
95                 'services',
96                 null,
97                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
98                 $this->trans('commands.common.options.services')
99             );
100     }
101
102     /**
103    * {@inheritdoc}
104    */
105     protected function execute(InputInterface $input, OutputInterface $output)
106     {
107         $io = new DrupalStyle($input, $output);
108
109         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
110         if (!$this->confirmGeneration($io)) {
111             return 1;
112         }
113
114         $module = $input->getOption('module');
115         $cache_context = $input->getOption('cache_context');
116         $class = $input->getOption('class');
117         $services = $input->getOption('services');
118
119         // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
120         $buildServices = $this->buildServices($services);
121
122         $this->generator->generate($module, $cache_context, $class, $buildServices);
123
124         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
125     }
126
127     /**
128    * {@inheritdoc}
129    */
130     protected function interact(InputInterface $input, OutputInterface $output)
131     {
132         $io = new DrupalStyle($input, $output);
133
134         // --module option
135         $module = $input->getOption('module');
136         if (!$module) {
137             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
138             $module = $this->moduleQuestion($io);
139             $input->setOption('module', $module);
140         }
141
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)
148             );
149             $input->setOption('cache_context', $cache_context);
150         }
151
152         // --class option
153         $class = $input->getOption('class');
154         if (!$class) {
155             $class = $io->ask(
156                 $this->trans('commands.generate.cache.context.questions.class'),
157                 'DefaultCacheContext'
158             );
159             $input->setOption('class', $class);
160         }
161
162         // --services option
163         $services = $input->getOption('services');
164         if (!$services) {
165             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
166             $services = $this->servicesQuestion($io);
167             $input->setOption('services', $services);
168         }
169     }
170 }