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