comparison core/modules/update/src/Form/UpdateReady.php @ 0:c75dbcec494b

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