3d29bdf6d6c207ec213cf57eb2d3e7c0e9b27899
[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 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;
22
23 class CacheContextCommand extends ContainerAwareCommand
24 {
25     use ModuleTrait;
26     use ConfirmationTrait;
27     use ServicesTrait;
28
29     /**
30      * @var CacheContextGenerator
31      */
32     protected $generator;
33
34     /**
35      * @var ChainQueue
36      */
37     protected $chainQueue;
38
39     /**
40      * @var Manager
41      */
42     protected $extensionManager;
43
44     /**
45      * @var StringConverter
46      */
47     protected $stringConverter;
48
49     /**
50      * @var Validator
51      */
52     protected $validator;
53
54     /**
55      * CacheContextCommand constructor.
56      *
57      * @param CacheContextGenerator $generator
58      * @param ChainQueue            $chainQueue
59      * @param Manager               $extensionManager
60      * @param StringConverter       $stringConverter
61      * @param Validator             $validator
62      */
63     public function __construct(
64         CacheContextGenerator $generator,
65         ChainQueue $chainQueue,
66         Manager $extensionManager,
67         StringConverter $stringConverter,
68         Validator $validator
69     ) {
70         $this->generator = $generator;
71         $this->chainQueue = $chainQueue;
72         $this->extensionManager = $extensionManager;
73         $this->stringConverter = $stringConverter;
74         $this->validator = $validator;
75         parent::__construct();
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     protected function configure()
82     {
83         $this
84             ->setName('generate:cache:context')
85             ->setDescription($this->trans('commands.generate.cache.context.description'))
86             ->setHelp($this->trans('commands.generate.cache.context.description'))
87             ->addOption(
88                 'module',
89                 null,
90                 InputOption::VALUE_REQUIRED,
91                 $this->trans('commands.common.options.module')
92             )
93             ->addOption(
94                 'cache-context',
95                 null,
96                 InputOption::VALUE_OPTIONAL,
97                 $this->trans('commands.generate.cache.context.options.name')
98             )
99             ->addOption(
100                 'class',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.cache.context.options.class')
104             )
105             ->addOption(
106                 'services',
107                 null,
108                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
109                 $this->trans('commands.common.options.services')
110             )->setAliases(['gcc']);
111     }
112
113     /**
114      * {@inheritdoc}
115      */
116     protected function execute(InputInterface $input, OutputInterface $output)
117     {
118         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
119         if (!$this->confirmOperation()) {
120             return 1;
121         }
122
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');
127
128         // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
129         $buildServices = $this->buildServices($services);
130
131         $this->generator->generate([
132             'module' => $module,
133             'cache_context' => $cache_context,
134             'class' => $class,
135             'services' => $buildServices,
136         ]);
137
138         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
139     }
140
141     /**
142      * {@inheritdoc}
143      */
144     protected function interact(InputInterface $input, OutputInterface $output)
145     {
146         // --module option
147         $module = $this->getModuleOption();
148
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)
155             );
156             $input->setOption('cache-context', $cache_context);
157         }
158
159         // --class option
160         $class = $input->getOption('class');
161         if (!$class) {
162             $class = $this->getIo()->ask(
163                 $this->trans('commands.generate.cache.context.questions.class'),
164                 'DefaultCacheContext',
165                 function ($class) {
166                     return $this->validator->validateClassName($class);
167                 }
168             );
169             $input->setOption('class', $class);
170         }
171
172         // --services option
173         $services = $input->getOption('services');
174         if (!$services) {
175             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
176             $services = $this->servicesQuestion();
177             $input->setOption('services', $services);
178         }
179     }
180 }