Newer
Older
<?php
namespace Drupal\automatic_updates;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* A batch processor for updates.
*/
class BatchProcessor {
/**
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
*
* @return \Drupal\automatic_updates\Updater
* The updater service.
*/
protected static function getUpdater(): Updater {
return \Drupal::service('automatic_updates.updater');
}
/**
* Records messages from a throwable, then re-throws it.
*
* @param \Throwable $error
* The caught exception.
* @param array $context
* The current context of the batch job.
*
* @throws \Throwable
* The caught exception, which will always be re-thrown once its messages
* have been recorded.
*/
protected static function handleException(\Throwable $error, array &$context): void {
$error_messages = [
$error->getMessage(),
];
if ($error instanceof UpdateException) {
foreach ($error->getValidationResults() as $result) {
$messages = $result->getMessages();
if (count($messages) > 1) {
array_unshift($messages, $result->getSummary());
}
$error_messages = array_merge($error_messages, $messages);
}
}
foreach ($error_messages as $error_message) {
$context['results']['errors'][] = $error_message;
}
throw $error;
}
/**
* Calls the updater's begin() method.
*

Ted Bowman
committed
* @param string[] $project_versions
* The project versions to be staged in the update, keyed by package name.
* @param array $context
* The current context of the batch job.
*
* @see \Drupal\automatic_updates\Updater::begin()
*/

Ted Bowman
committed
public static function begin(array $project_versions, array &$context): void {
try {

Ted Bowman
committed
static::getUpdater()->begin($project_versions);
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
* Calls the updater's stageVersions() method.
*
* @param array $context
* The current context of the batch job.
*

Ted Bowman
committed
* @see \Drupal\automatic_updates\Updater::stage()

Ted Bowman
committed
public static function stage(array &$context): void {
try {

Ted Bowman
committed
static::getUpdater()->stage();
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
* Calls the updater's commit() method.
*
* @param array $context
* The current context of the batch job.
*
* @see \Drupal\automatic_updates\Updater::commit()
*/
public static function commit(array &$context): void {
try {
static::getUpdater()->commit();
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
* Calls the updater's clean() method.
*
* @param array $context
* The current context of the batch job.
*
* @see \Drupal\automatic_updates\Updater::clean()
*/
public static function clean(array &$context): void {
try {
static::getUpdater()->clean();
}
catch (\Throwable $e) {
static::handleException($e, $context);
}
}
/**
* Finishes the stage batch job.
*
* @param bool $success
* Indicate that the batch API tasks were all completed successfully.
* @param array $results
* An array of all the results.
* @param array $operations
* A list of the operations that had not been completed by the batch API.
*/
public static function finishStage(bool $success, array $results, array $operations): ?RedirectResponse {
if ($success) {
return new RedirectResponse(Url::fromRoute('automatic_updates.confirmation_page', [],
['absolute' => TRUE])->toString());
}
static::handleBatchError($results);
return NULL;
}
/**
* Finishes the commit batch job.
*
* @param bool $success
* Indicate that the batch API tasks were all completed successfully.
* @param array $results
* An array of all the results.
* @param array $operations
* A list of the operations that had not been completed by the batch API.
*/
public static function finishCommit(bool $success, array $results, array $operations): ?RedirectResponse {
if ($success) {
\Drupal::messenger()->addMessage('Update complete!');
// @todo redirect to update.php?

Kunal Sachdev
committed
return new RedirectResponse(Url::fromRoute('update.status', [],
['absolute' => TRUE])->toString());
static::handleBatchError($results);
return NULL;
}
/**
* Handles a batch job that finished with errors.
*
* @param array $results
* The batch results.
*/
protected static function handleBatchError(array $results): void {
if (isset($results['errors'])) {
foreach ($results['errors'] as $error) {
\Drupal::messenger()->addError($error);
}
}
else {
\Drupal::messenger()->addError("Update error");
}
}
}