Version 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                 '',
85                 InputOption::VALUE_REQUIRED,
86                 $this->trans('commands.common.options.module')
87             )
88             ->addOption(
89                 'update-n',
90                 '',
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;
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
127     protected function interact(InputInterface $input, OutputInterface $output)
128     {
129         $io = new DrupalStyle($input, $output);
130
131         $this->site->loadLegacyFile('/core/includes/update.inc');
132         $this->site->loadLegacyFile('/core/includes/schema.inc');
133
134         $module = $input->getOption('module');
135         if (!$module) {
136             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
137             $module = $this->moduleQuestion($io);
138             $input->setOption('module', $module);
139         }
140
141         $lastUpdateSchema = $this->getLastUpdate($module);
142         $nextUpdateSchema = $lastUpdateSchema ? ($lastUpdateSchema + 1): 8001;
143
144         $updateNumber = $input->getOption('update-n');
145         if (!$updateNumber) {
146             $updateNumber = $io->ask(
147                 $this->trans('commands.generate.update.questions.update-n'),
148                 $nextUpdateSchema,
149                 function ($updateNumber) use ($lastUpdateSchema) {
150                     if (!is_numeric($updateNumber)) {
151                         throw new \InvalidArgumentException(
152                             sprintf(
153                                 $this->trans('commands.generate.update.messages.wrong-update-n'),
154                                 $updateNumber
155                             )
156                         );
157                     } else {
158                         if ($updateNumber <= $lastUpdateSchema) {
159                             throw new \InvalidArgumentException(
160                                 sprintf(
161                                     $this->trans('commands.generate.update.messages.wrong-update-n'),
162                                     $updateNumber
163                                 )
164                             );
165                         }
166                         return $updateNumber;
167                     }
168                 }
169             );
170
171             $input->setOption('update-n', $updateNumber);
172         }
173     }
174
175
176     protected function createGenerator()
177     {
178         return new UpdateGenerator();
179     }
180
181     protected function getLastUpdate($module)
182     {
183         $this->site->loadLegacyFile('/core/includes/update.inc');
184         $this->site->loadLegacyFile('/core/includes/schema.inc');
185
186         $updates = update_get_update_list();
187
188         if (empty($updates[$module]['pending'])) {
189             $lastUpdateSchema = drupal_get_schema_versions($module);
190             $lastUpdateSchema = $lastUpdateSchema[0];
191         } else {
192             $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
193         }
194
195         return $lastUpdateSchema;
196     }
197 }