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 Drupal\Console\Core\Command\Command;
17 use Drupal\Console\Extension\Manager;
18 use Drupal\Console\Core\Utils\ChainQueue;
19 use Drupal\Console\Utils\Site;
20 use Drupal\Console\Utils\Validator;
23 * Class PostUpdateCommand
25 * @package Drupal\Console\Command\Generate
27 class PostUpdateCommand extends Command
30 use ConfirmationTrait;
35 protected $extensionManager;
38 * @var PostUpdateGenerator
55 protected $chainQueue;
58 * PostUpdateCommand constructor.
60 * @param Manager $extensionManager
61 * @param PostUpdateGenerator $generator
63 * @param Validator $validator
64 * @param ChainQueue $chainQueue
66 public function __construct(
67 Manager $extensionManager,
68 PostUpdateGenerator $generator,
71 ChainQueue $chainQueue
73 $this->extensionManager = $extensionManager;
74 $this->generator = $generator;
76 $this->validator = $validator;
77 $this->chainQueue = $chainQueue;
78 parent::__construct();
81 protected function configure()
84 ->setName('generate:post:update')
85 ->setDescription($this->trans('commands.generate.post.update.description'))
86 ->setHelp($this->trans('commands.generate.post.update.help'))
90 InputOption::VALUE_REQUIRED,
91 $this->trans('commands.common.options.module')
96 InputOption::VALUE_REQUIRED,
97 $this->trans('commands.generate.post.update.options.post-update-name')
98 )->setAliases(['gpu']);
104 protected function execute(InputInterface $input, OutputInterface $output)
106 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
107 if (!$this->confirmOperation()) {
111 $module = $input->getOption('module');
112 $postUpdateName = $input->getOption('post-update-name');
114 $this->validatePostUpdateName($module, $postUpdateName);
116 $this->generator->generate([
118 'post_update_name' => $postUpdateName,
121 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
126 protected function interact(InputInterface $input, OutputInterface $output)
128 $this->site->loadLegacyFile('/core/includes/update.inc');
129 $this->site->loadLegacyFile('/core/includes/schema.inc');
132 $this->getModuleOption();
134 $postUpdateName = $input->getOption('post-update-name');
135 if (!$postUpdateName) {
136 $postUpdateName = $this->getIo()->ask(
137 $this->trans('commands.generate.post.update.questions.post-update-name'),
139 function ($postUpdateName) {
140 return $this->validator->validateSpaces($postUpdateName);
144 $input->setOption('post-update-name', $postUpdateName);
148 protected function getLastUpdate($module)
150 $this->site->loadLegacyFile('/core/includes/update.inc');
151 $this->site->loadLegacyFile('/core/includes/schema.inc');
153 $updates = update_get_update_list();
155 if (empty($updates[$module]['pending'])) {
156 $lastUpdateSchema = drupal_get_schema_versions($module);
158 $lastUpdateSchema = reset(array_keys($updates[$module]['pending'], max($updates[$module]['pending'])));
161 return $lastUpdateSchema;
164 protected function validatePostUpdateName($module, $postUpdateName)
166 if (!$this->validator->validateSpaces($postUpdateName)) {
167 throw new \InvalidArgumentException(
169 $this->trans('commands.generate.post.update.messages.wrong-post-update-name'),
175 if ($this->extensionManager->validateModuleFunctionExist($module, $module . '_post_update_' . $postUpdateName, $module . '.post_update.php')) {
176 throw new \InvalidArgumentException(
178 $this->trans('commands.generate.post.update.messages.post-update-name-already-implemented'),