annotate core/modules/update/src/Form/UpdateReady.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\update\Form;
Chris@0 4
Chris@0 5 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 6 use Drupal\Core\FileTransfer\Local;
Chris@0 7 use Drupal\Core\Form\FormBase;
Chris@0 8 use Drupal\Core\Form\FormStateInterface;
Chris@0 9 use Drupal\Core\State\StateInterface;
Chris@0 10 use Drupal\Core\Updater\Updater;
Chris@0 11 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 12 use Symfony\Component\HttpFoundation\Response;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Configure update settings for this site.
Chris@14 16 *
Chris@14 17 * @internal
Chris@0 18 */
Chris@0 19 class UpdateReady extends FormBase {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The root location under which updated projects will be saved.
Chris@0 23 *
Chris@0 24 * @var string
Chris@0 25 */
Chris@0 26 protected $root;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The module handler.
Chris@0 30 *
Chris@0 31 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 32 */
Chris@0 33 protected $moduleHandler;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * The state key value store.
Chris@0 37 *
Chris@0 38 * @var \Drupal\Core\State\StateInterface
Chris@0 39 */
Chris@0 40 protected $state;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * The Site path.
Chris@0 44 *
Chris@0 45 * @var string
Chris@0 46 */
Chris@0 47 protected $sitePath;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Constructs a new UpdateReady object.
Chris@0 51 *
Chris@0 52 * @param string $root
Chris@0 53 * The root location under which updated projects will be saved.
Chris@0 54 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 55 * The object that manages enabled modules in a Drupal installation.
Chris@0 56 * @param \Drupal\Core\State\StateInterface $state
Chris@0 57 * The state key value store.
Chris@0 58 * @param string $site_path
Chris@0 59 * The site path.
Chris@0 60 */
Chris@0 61 public function __construct($root, ModuleHandlerInterface $module_handler, StateInterface $state, $site_path) {
Chris@0 62 $this->root = $root;
Chris@0 63 $this->moduleHandler = $module_handler;
Chris@0 64 $this->state = $state;
Chris@0 65 $this->sitePath = $site_path;
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function getFormId() {
Chris@0 72 return 'update_manager_update_ready_form';
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 public static function create(ContainerInterface $container) {
Chris@0 79 return new static(
Chris@0 80 $container->get('update.root'),
Chris@0 81 $container->get('module_handler'),
Chris@0 82 $container->get('state'),
Chris@0 83 $container->get('site.path')
Chris@0 84 );
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function buildForm(array $form, FormStateInterface $form_state) {
Chris@0 91 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
Chris@0 92 if (!_update_manager_check_backends($form, 'update')) {
Chris@0 93 return $form;
Chris@0 94 }
Chris@0 95
Chris@0 96 $form['backup'] = [
Chris@0 97 '#prefix' => '<strong>',
Chris@0 98 '#markup' => $this->t('Back up your database and site before you continue. <a href=":backup_url">Learn how</a>.', [':backup_url' => 'https://www.drupal.org/node/22281']),
Chris@0 99 '#suffix' => '</strong>',
Chris@0 100 ];
Chris@0 101
Chris@0 102 $form['maintenance_mode'] = [
Chris@0 103 '#title' => $this->t('Perform updates with site in maintenance mode (strongly recommended)'),
Chris@0 104 '#type' => 'checkbox',
Chris@0 105 '#default_value' => TRUE,
Chris@0 106 ];
Chris@0 107
Chris@0 108 $form['actions'] = ['#type' => 'actions'];
Chris@0 109 $form['actions']['submit'] = [
Chris@0 110 '#type' => 'submit',
Chris@0 111 '#value' => $this->t('Continue'),
Chris@0 112 ];
Chris@0 113
Chris@0 114 return $form;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * {@inheritdoc}
Chris@0 119 */
Chris@0 120 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 121 // Store maintenance_mode setting so we can restore it when done.
Chris@0 122 $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
Chris@0 123 if ($form_state->getValue('maintenance_mode') == TRUE) {
Chris@0 124 $this->state->set('system.maintenance_mode', TRUE);
Chris@0 125 }
Chris@0 126
Chris@0 127 if (!empty($_SESSION['update_manager_update_projects'])) {
Chris@0 128 // Make sure the Updater registry is loaded.
Chris@0 129 drupal_get_updaters();
Chris@0 130
Chris@0 131 $updates = [];
Chris@0 132 $directory = _update_manager_extract_directory();
Chris@0 133
Chris@0 134 $projects = $_SESSION['update_manager_update_projects'];
Chris@0 135 unset($_SESSION['update_manager_update_projects']);
Chris@0 136
Chris@0 137 $project_real_location = NULL;
Chris@0 138 foreach ($projects as $project => $url) {
Chris@0 139 $project_location = $directory . '/' . $project;
Chris@0 140 $updater = Updater::factory($project_location, $this->root);
Chris@14 141 $project_real_location = \Drupal::service('file_system')->realpath($project_location);
Chris@0 142 $updates[] = [
Chris@0 143 'project' => $project,
Chris@0 144 'updater_name' => get_class($updater),
Chris@0 145 'local_url' => $project_real_location,
Chris@0 146 ];
Chris@0 147 }
Chris@0 148
Chris@0 149 // If the owner of the last directory we extracted is the same as the
Chris@0 150 // owner of our configuration directory (e.g. sites/default) where we're
Chris@0 151 // trying to install the code, there's no need to prompt for FTP/SSH
Chris@0 152 // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
Chris@0 153 // and invoke update_authorize_run_update() directly.
Chris@0 154 if (fileowner($project_real_location) == fileowner($this->sitePath)) {
Chris@0 155 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
Chris@0 156 $filetransfer = new Local($this->root);
Chris@0 157 $response = update_authorize_run_update($filetransfer, $updates);
Chris@0 158 if ($response instanceof Response) {
Chris@0 159 $form_state->setResponse($response);
Chris@0 160 }
Chris@0 161 }
Chris@0 162 // Otherwise, go through the regular workflow to prompt for FTP/SSH
Chris@0 163 // credentials and invoke update_authorize_run_update() indirectly with
Chris@0 164 // whatever FileTransfer object authorize.php creates for us.
Chris@0 165 else {
Chris@0 166 // The page title must be passed here to ensure it is initially used
Chris@0 167 // when authorize.php loads for the first time with the FTP/SSH
Chris@0 168 // credentials form.
Chris@0 169 system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', [$updates], $this->t('Update manager'));
Chris@0 170 $form_state->setRedirectUrl(system_authorized_get_url());
Chris@0 171 }
Chris@0 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 }