Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / UpdateCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\UpdateCommand.
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\Generator\UpdateGenerator;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Command\Shared\ConfirmationTrait;
16 use Symfony\Component\Console\Command\Command;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Core\Utils\ChainQueue;
21 use Drupal\Console\Utils\Site;
22
23 /**
24  * Class UpdateCommand
25  *
26  * @package Drupal\Console\Command\Generate
27  */
28 class UpdateCommand extends Command
29 {
30     use ModuleTrait;
31     use ConfirmationTrait;
32     use CommandTrait;
33
34     /**
35  * @var Manager
36 */
37     protected $extensionManager;
38
39     /**
40  * @var UpdateGenerator
41 */
42     protected $generator;
43
44     /**
45      * @var Site
46      */
47     protected $site;
48
49     /**
50      * @var ChainQueue
51      */
52     protected $chainQueue;
53
54
55     /**
56      * UpdateCommand constructor.
57      *
58      * @param Manager         $extensionManager
59      * @param UpdateGenerator $generator
60      * @param Site            $site
61      * @param ChainQueue      $chainQueue
62      */
63     public function __construct(
64         Manager $extensionManager,
65         UpdateGenerator $generator,
66         Site $site,
67         ChainQueue $chainQueue
68     ) {
69         $this->extensionManager = $extensionManager;
70         $this->generator = $generator;
71         $this->site = $site;
72         $this->chainQueue = $chainQueue;
73         parent::__construct();
74     }
75
76     protected function configure()
77     {
78         $this
79             ->setName('generate:update')
80             ->setDescription($this->trans('commands.generate.update.description'))
81             ->setHelp($this->trans('commands.generate.update.help'))
82             ->addOption(
83                 'module',
84                 null,
85                 InputOption::VALUE_REQUIRED,
86                 $this->trans('commands.common.options.module')
87             )
88             ->addOption(
89                 'update-n',
90                 null,
91                 InputOption::VALUE_REQUIRED,
92                 $this->trans('commands.generate.update.options.update-n')
93             );
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     protected function execute(InputInterface $input, OutputInterface $output)
100     {
101         $io = new DrupalStyle($input, $output);
102
103         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
104         if (!$this->confirmGeneration($io)) {
105             return 1;
106         }
107
108         $module = $input->getOption('module');
109         $updateNumber = $input->getOption('update-n');
110
111         $lastUpdateSchema = $this->getLastUpdate($module);
112
113         if ($updateNumber <= $lastUpdateSchema) {
114             throw new \InvalidArgumentException(
115                 sprintf(
116                     $this->trans('commands.generate.update.messages.wrong-update-n'),
117                     $updateNumber
118                 )
119             );
120         }
121
122         $this->generator->generate($module, $updateNumber);
123
124         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
125
126         return 0;
127     }
128
129     protected function interact(InputInterface $input, OutputInterface $output)
130     {
131         $io = new DrupalStyle($input, $output);
132
133         $this->site->loadLegacyFile('/core/includes/update.inc');
134         $this->site->loadLegacyFile('/core/includes/schema.inc');
135
136         $module = $input->getOption('module');
137         if (!$module) {
138             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
139             $module = $this->moduleQuestion($io);
140             $input->setOption('module', $module);
141         }
142
143         $lastUpdateSchema = $this->getLastUpdate($module);
144         $nextUpdateSchema = $lastUpdateSchema ? ($lastUpdateSchema + 1): 8001;
145
146         $updateNumber = $input->getOption('update-n');
147         if (!$updateNumber) {
148             $updateNumber = $io->ask(
149                 $this->trans('commands.generate.update.questions.update-n'),
150                 $nextUpdateSchema,
151                 function ($updateNumber) use ($lastUpdateSchema) {
152                     if (!is_numeric($updateNumber)) {
153                         throw new \InvalidArgumentException(
154                             sprintf(
155                                 $this->trans('commands.generate.update.messages.wrong-update-n'),
156                                 $updateNumber
157                             )
158                         );
159                     } else {
160                         if ($updateNumber <= $lastUpdateSchema) {
161                             throw new \InvalidArgumentException(
162                                 sprintf(
163                                     $this->trans('commands.generate.update.messages.wrong-update-n'),
164                                     $updateNumber
165                                 )
166                             );
167                         }
168                         return $updateNumber;
169                     }
170                 }
171             );
172
173             $input->setOption('update-n', $updateNumber);
174         }
175     }
176
177
178     protected function createGenerator()
179     {
180         return new UpdateGenerator();
181     }
182
183     protected function getLastUpdate($module)
184     {
185         $this->site->loadLegacyFile('/core/includes/update.inc');
186         $this->site->loadLegacyFile('/core/includes/schema.inc');
187
188         $updates = update_get_update_list();
189
190         if (empty($updates[$module]['pending'])) {
191             $lastUpdateSchema = drupal_get_schema_versions($module);
192             $lastUpdateSchema = $lastUpdateSchema[0];
193         } else {
194             $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
195         }
196
197         return $lastUpdateSchema;
198     }
199 }