5 * Contains \Drupal\Console\Command\Generate\PostUpdateCommand.
8 namespace Drupal\Console\Command\Generate;
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\PostUpdateGenerator;
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\Style\DrupalStyle;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\ChainQueue;
20 use Drupal\Console\Core\Command\Shared\CommandTrait;
21 use Drupal\Console\Utils\Site;
22 use Drupal\Console\Utils\Validator;
25 * Class PostUpdateCommand
27 * @package Drupal\Console\Command\Generate
29 class PostUpdateCommand extends Command
32 use ConfirmationTrait;
38 protected $extensionManager;
41 * @var PostUpdateGenerator
58 protected $chainQueue;
61 * PostUpdateCommand constructor.
63 * @param Manager $extensionManager
64 * @param PostUpdateGenerator $generator
66 * @param Validator $validator
67 * @param ChainQueue $chainQueue
69 public function __construct(
70 Manager $extensionManager,
71 PostUpdateGenerator $generator,
74 ChainQueue $chainQueue
76 $this->extensionManager = $extensionManager;
77 $this->generator = $generator;
79 $this->validator = $validator;
80 $this->chainQueue = $chainQueue;
81 parent::__construct();
84 protected function configure()
87 ->setName('generate:post:update')
88 ->setDescription($this->trans('commands.generate.post:update.description'))
89 ->setHelp($this->trans('commands.generate.post.update.help'))
93 InputOption::VALUE_REQUIRED,
94 $this->trans('commands.common.options.module')
99 InputOption::VALUE_REQUIRED,
100 $this->trans('commands.generate.post.update.options.post-update-name')
107 protected function execute(InputInterface $input, OutputInterface $output)
109 $io = new DrupalStyle($input, $output);
111 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
112 if (!$this->confirmGeneration($io)) {
116 $module = $input->getOption('module');
117 $postUpdateName = $input->getOption('post-update-name');
119 $this->validatePostUpdateName($module, $postUpdateName);
121 $this->generator->generate($module, $postUpdateName);
123 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
126 protected function interact(InputInterface $input, OutputInterface $output)
128 $io = new DrupalStyle($input, $output);
130 $this->site->loadLegacyFile('/core/includes/update.inc');
131 $this->site->loadLegacyFile('/core/includes/schema.inc');
133 $module = $input->getOption('module');
135 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
136 $module = $this->moduleQuestion($io);
137 $input->setOption('module', $module);
140 $postUpdateName = $input->getOption('post-update-name');
141 if (!$postUpdateName) {
142 $postUpdateName = $io->ask(
143 $this->trans('commands.generate.post.update.questions.post-update-name'),
145 function ($postUpdateName) {
146 return $this->validator->validateSpaces($postUpdateName);
150 $input->setOption('post-update-name', $postUpdateName);
155 protected function createGenerator()
157 return new PostUpdateGenerator();
160 protected function getLastUpdate($module)
162 $this->site->loadLegacyFile('/core/includes/update.inc');
163 $this->site->loadLegacyFile('/core/includes/schema.inc');
165 $updates = update_get_update_list();
167 if (empty($updates[$module]['pending'])) {
168 $lastUpdateSchema = drupal_get_schema_versions($module);
170 $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
173 return $lastUpdateSchema;
176 protected function validatePostUpdateName($module, $postUpdateName)
178 if (!$this->validator->validateSpaces($postUpdateName)) {
179 throw new \InvalidArgumentException(
181 $this->trans('commands.generate.post.update.messages.wrong-post-update-name'),
187 if ($this->extensionManager->validateModuleFunctionExist($module, $module . '_post_update_' . $postUpdateName, $module . '.post_update.php')) {
188 throw new \InvalidArgumentException(
190 $this->trans('commands.generate.post.update.messages.post-update-name-already-implemented'),