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']);
128 protected function interact(InputInterface $input, OutputInterface $output)
130 $io = new DrupalStyle($input, $output);
132 $this->site->loadLegacyFile('/core/includes/update.inc');
133 $this->site->loadLegacyFile('/core/includes/schema.inc');
135 $module = $input->getOption('module');
137 // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
138 $module = $this->moduleQuestion($io);
139 $input->setOption('module', $module);
142 $postUpdateName = $input->getOption('post-update-name');
143 if (!$postUpdateName) {
144 $postUpdateName = $io->ask(
145 $this->trans('commands.generate.post.update.questions.post-update-name'),
147 function ($postUpdateName) {
148 return $this->validator->validateSpaces($postUpdateName);
152 $input->setOption('post-update-name', $postUpdateName);
157 protected function createGenerator()
159 return new PostUpdateGenerator();
162 protected function getLastUpdate($module)
164 $this->site->loadLegacyFile('/core/includes/update.inc');
165 $this->site->loadLegacyFile('/core/includes/schema.inc');
167 $updates = update_get_update_list();
169 if (empty($updates[$module]['pending'])) {
170 $lastUpdateSchema = drupal_get_schema_versions($module);
172 $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
175 return $lastUpdateSchema;
178 protected function validatePostUpdateName($module, $postUpdateName)
180 if (!$this->validator->validateSpaces($postUpdateName)) {
181 throw new \InvalidArgumentException(
183 $this->trans('commands.generate.post.update.messages.wrong-post-update-name'),
189 if ($this->extensionManager->validateModuleFunctionExist($module, $module . '_post_update_' . $postUpdateName, $module . '.post_update.php')) {
190 throw new \InvalidArgumentException(
192 $this->trans('commands.generate.post.update.messages.post-update-name-already-implemented'),