View source
<?php
namespace Drupal\update\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\FileTransfer\Local;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Updater\Updater;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
class UpdateReady extends FormBase {
protected $root;
protected $moduleHandler;
protected $state;
protected $sitePath;
public function __construct($root, ModuleHandlerInterface $module_handler, StateInterface $state, $site_path) {
$this->root = $root;
$this->moduleHandler = $module_handler;
$this->state = $state;
$this->sitePath = $site_path;
}
public function getFormId() {
return 'update_manager_update_ready_form';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('update.root'), $container
->get('module_handler'), $container
->get('state'), $container
->get('site.path'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->moduleHandler
->loadInclude('update', 'inc', 'update.manager');
if (!_update_manager_check_backends($form, 'update')) {
return $form;
}
$form['backup'] = [
'#prefix' => '<strong>',
'#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',
]),
'#suffix' => '</strong>',
];
$form['maintenance_mode'] = [
'#title' => $this
->t('Perform updates with site in maintenance mode (strongly recommended)'),
'#type' => 'checkbox',
'#default_value' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$session = $this
->getRequest()
->getSession();
$session
->set('maintenance_mode', $this->state
->get('system.maintenance_mode'));
if ($form_state
->getValue('maintenance_mode') == TRUE) {
$this->state
->set('system.maintenance_mode', TRUE);
}
$projects = $session
->remove('update_manager_update_projects');
if ($projects) {
drupal_get_updaters();
$updates = [];
$directory = _update_manager_extract_directory();
$project_real_location = NULL;
foreach ($projects as $project => $url) {
$project_location = $directory . '/' . $project;
$updater = Updater::factory($project_location, $this->root);
$project_real_location = \Drupal::service('file_system')
->realpath($project_location);
$updates[] = [
'project' => $project,
'updater_name' => get_class($updater),
'local_url' => $project_real_location,
];
}
if (fileowner($project_real_location) == fileowner($this->sitePath)) {
$this->moduleHandler
->loadInclude('update', 'inc', 'update.authorize');
$filetransfer = new Local($this->root, \Drupal::service('file_system'));
$response = update_authorize_run_update($filetransfer, $updates);
if ($response instanceof Response) {
$form_state
->setResponse($response);
}
}
else {
system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', [
$updates,
], $this
->t('Update manager'));
$form_state
->setRedirectUrl(system_authorized_get_url());
}
}
}
}