CommandGcuSubscriber.php in Update helper 2.x
File
modules/update_helper_checklist/src/Events/CommandGcuSubscriber.php
View source
<?php
namespace Drupal\update_helper_checklist\Events;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\update_helper\Events\CommandExecuteEvent;
use Drupal\update_helper\Events\UpdateHelperEvents;
use Drupal\update_helper\Events\CommandInteractEvent;
use Drupal\update_helper\Generators\ConfigurationUpdate;
use Drupal\update_helper_checklist\UpdateChecklist;
use DrupalCodeGenerator\Asset;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CommandGcuSubscriber implements EventSubscriberInterface {
protected static $updateVersionName = 'update_version';
protected static $updateDescription = 'update_description';
protected static $successMessageName = 'success_message';
protected static $failureMessageName = 'failure_message';
protected $updateChecklist;
protected $moduleHandler;
public function __construct(UpdateChecklist $update_checklist, ModuleHandlerInterface $module_handler) {
$this->updateChecklist = $update_checklist;
$this->moduleHandler = $module_handler;
}
public static function getSubscribedEvents() {
return [
UpdateHelperEvents::COMMAND_GCU_INTERACT => [
[
'onInteract',
10,
],
],
UpdateHelperEvents::COMMAND_GCU_EXECUTE => [
[
'onExecute',
10,
],
],
];
}
public function onInteract(CommandInteractEvent $interact_event) {
$update_versions = $this->updateChecklist
->getUpdateVersions($interact_event
->getVars()['module']);
end($update_versions);
$questions[static::$updateVersionName] = new Question('Please enter a update version for checklist collection', empty($update_versions) ? '8.x-1.0' : current($update_versions));
$questions[static::$updateDescription] = new Question('Please enter a detailed update description that will be used for checklist', 'This configuration update will update site configuration to newly provided configuration');
$questions[static::$successMessageName] = new Question('Please enter a detailed update description that will be used for checklist', 'Configuration is successfully updated.');
$questions[static::$failureMessageName] = new Question('Please enter a message that will be displayed in checklist entry when the update has failed', 'Update of configuration has failed.');
$interact_event
->setQuestions($questions);
}
public function onExecute(CommandExecuteEvent $execute_event) {
$vars = $execute_event
->getVars();
$module_path = $this->moduleHandler
->getModule($vars['module'])
->getPath();
$checklist_file = $module_path . DIRECTORY_SEPARATOR . UpdateChecklist::$updateChecklistFileName;
$update_versions = $this->updateChecklist
->getUpdateVersions($vars['module']);
end($update_versions);
$last_update_version = current($update_versions);
$vars['update_hook_name'] = ConfigurationUpdate::getUpdateFunctionName($vars['module'], $vars['update-n']);
$vars['update_version'] = $vars[static::$updateVersionName] === $last_update_version ? '' : $vars[static::$updateVersionName];
$vars['file_exists'] = file_exists($checklist_file);
$asset = (new Asset())
->type('file')
->vars($vars)
->path($checklist_file)
->action('append')
->template('configuration_update_checklist.yml.twig');
$execute_event
->addAsset($asset);
$execute_event
->addTemplatePath(__DIR__ . '/../../templates');
}
}