ConfigImporterBatch.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Drupal\Core\Config\Importer;
  3. use Drupal\Core\Config\ConfigImporter;
  4. use Drupal\Core\Installer\InstallerKernel;
  5. /**
  6. * Methods for running the ConfigImporter in a batch.
  7. *
  8. * @see \Drupal\Core\Config\ConfigImporter
  9. */
  10. class ConfigImporterBatch {
  11. /**
  12. * Processes the config import batch and persists the importer.
  13. *
  14. * @param \Drupal\Core\Config\ConfigImporter $config_importer
  15. * The batch config importer object to persist.
  16. * @param string $sync_step
  17. * The synchronization step to do.
  18. * @param array $context
  19. * The batch context.
  20. */
  21. public static function process(ConfigImporter $config_importer, $sync_step, &$context) {
  22. if (!isset($context['sandbox']['config_importer'])) {
  23. $context['sandbox']['config_importer'] = $config_importer;
  24. }
  25. $config_importer = $context['sandbox']['config_importer'];
  26. $config_importer->doSyncStep($sync_step, $context);
  27. if ($errors = $config_importer->getErrors()) {
  28. if (!isset($context['results']['errors'])) {
  29. $context['results']['errors'] = [];
  30. }
  31. $context['results']['errors'] = array_merge($errors, $context['results']['errors']);
  32. }
  33. }
  34. /**
  35. * Finish batch.
  36. *
  37. * This function is a static function to avoid serializing the ConfigSync
  38. * object unnecessarily.
  39. *
  40. * @param bool $success
  41. * Indicate that the batch API tasks were all completed successfully.
  42. * @param array $results
  43. * An array of all the results that were updated in update_do_one().
  44. * @param array $operations
  45. * A list of the operations that had not been completed by the batch API.
  46. */
  47. public static function finish($success, $results, $operations) {
  48. $messenger = \Drupal::messenger();
  49. if ($success) {
  50. if (!empty($results['errors'])) {
  51. $logger = \Drupal::logger('config_sync');
  52. foreach ($results['errors'] as $error) {
  53. $messenger->addError($error);
  54. $logger->error($error);
  55. }
  56. $messenger->addWarning(t('The configuration was imported with errors.'));
  57. }
  58. elseif (!InstallerKernel::installationAttempted()) {
  59. // Display a success message when not installing Drupal.
  60. $messenger->addStatus(t('The configuration was imported successfully.'));
  61. }
  62. }
  63. else {
  64. // An error occurred.
  65. // $operations contains the operations that remained unprocessed.
  66. $error_operation = reset($operations);
  67. $message = t('An error occurred while processing %error_operation with arguments: @arguments', ['%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)]);
  68. $messenger->addError($message);
  69. }
  70. }
  71. }