PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc

http://github.com/drupal/drupal
Pascal | 184 lines | 97 code | 19 blank | 68 comment | 15 complexity | c4abc36f5a90cf567508b1895aadce01 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Batch callbacks for the Batch API tests.
  5. */
  6. use Drupal\Component\Utility\Html;
  7. use Drupal\Core\Url;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. /**
  10. * Implements callback_batch_operation().
  11. *
  12. * Performs a simple batch operation.
  13. */
  14. function _batch_test_callback_1($id, $sleep, &$context) {
  15. // No-op, but ensure the batch take a couple iterations.
  16. // Batch needs time to run for the test, so sleep a bit.
  17. usleep($sleep);
  18. // Track execution, and store some result for post-processing in the
  19. // 'finished' callback.
  20. batch_test_stack("op 1 id $id");
  21. $context['results'][1][] = $id;
  22. }
  23. /**
  24. * Implements callback_batch_operation().
  25. *
  26. * Performs a multistep batch operation.
  27. */
  28. function _batch_test_callback_2($start, $total, $sleep, &$context) {
  29. // Initialize context with progress information.
  30. if (!isset($context['sandbox']['current'])) {
  31. $context['sandbox']['current'] = $start;
  32. $context['sandbox']['count'] = 0;
  33. }
  34. // Process by groups of 5 (arbitrary value).
  35. $limit = 5;
  36. for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
  37. // No-op, but ensure the batch take a couple iterations.
  38. // Batch needs time to run for the test, so sleep a bit.
  39. usleep($sleep);
  40. // Track execution, and store some result for post-processing in the
  41. // 'finished' callback.
  42. $id = $context['sandbox']['current'] + $i;
  43. batch_test_stack("op 2 id $id");
  44. $context['results'][2][] = $id;
  45. // Update progress information.
  46. $context['sandbox']['count']++;
  47. }
  48. $context['sandbox']['current'] += $i;
  49. // Inform batch engine about progress.
  50. if ($context['sandbox']['count'] != $total) {
  51. $context['finished'] = $context['sandbox']['count'] / $total;
  52. }
  53. }
  54. /**
  55. * Implements callback_batch_operation().
  56. */
  57. function _batch_test_callback_5($id, $sleep, &$context) {
  58. // No-op, but ensure the batch take a couple iterations.
  59. // Batch needs time to run for the test, so sleep a bit.
  60. usleep($sleep);
  61. // Track execution, and store some result for post-processing in the
  62. // 'finished' callback.
  63. batch_test_stack("op 5 id $id");
  64. $context['results'][5][] = $id;
  65. // This test is to test finished > 1
  66. $context['finished'] = 3.14;
  67. }
  68. /**
  69. * Implements callback_batch_operation().
  70. *
  71. * Performs a batch operation setting up its own batch.
  72. */
  73. function _batch_test_nested_batch_callback() {
  74. batch_test_stack('setting up batch 2');
  75. batch_set(_batch_test_batch_2());
  76. }
  77. /**
  78. * Provides a common 'finished' callback for batches 1 to 4.
  79. */
  80. function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
  81. if ($results) {
  82. foreach ($results as $op => $op_results) {
  83. $messages[] = 'op ' . Html::escape($op) . ': processed ' . count($op_results) . ' elements';
  84. }
  85. }
  86. else {
  87. $messages[] = 'none';
  88. }
  89. if (!$success) {
  90. // A fatal error occurred during the processing.
  91. $error_operation = reset($operations);
  92. $messages[] = t('An error occurred while processing @op with arguments:<br />@args', array('@op' => $error_operation[0], '@args' => print_r($error_operation[1], TRUE)));
  93. }
  94. // Use item list template to render the messages.
  95. $error_message = [
  96. '#type' => 'inline_template',
  97. '#template' => 'results for batch {{ batch_id }}{{ errors }}',
  98. '#context' => [
  99. 'batch_id' => $batch_id,
  100. 'errors' => [
  101. '#theme' => 'item_list',
  102. '#items' => $messages,
  103. ],
  104. ],
  105. ];
  106. drupal_set_message(\Drupal::service('renderer')->renderPlain($error_message));
  107. }
  108. /**
  109. * Implements callback_batch_finished().
  110. *
  111. * Triggers 'finished' callback for batch 0.
  112. */
  113. function _batch_test_finished_0($success, $results, $operations) {
  114. _batch_test_finished_helper(0, $success, $results, $operations);
  115. }
  116. /**
  117. * Implements callback_batch_finished().
  118. *
  119. * Triggers 'finished' callback for batch 1.
  120. */
  121. function _batch_test_finished_1($success, $results, $operations) {
  122. _batch_test_finished_helper(1, $success, $results, $operations);
  123. }
  124. /**
  125. * Implements callback_batch_finished().
  126. *
  127. * Triggers 'finished' callback for batch 1.
  128. */
  129. function _batch_test_finished_1_finished($success, $results, $operations) {
  130. _batch_test_finished_helper(1, $success, $results, $operations);
  131. return new RedirectResponse(Url::fromRoute('test_page_test.test_page', [], ['absolute' => TRUE])->toString());
  132. }
  133. /**
  134. * Implements callback_batch_finished().
  135. *
  136. * Triggers 'finished' callback for batch 2.
  137. */
  138. function _batch_test_finished_2($success, $results, $operations) {
  139. _batch_test_finished_helper(2, $success, $results, $operations);
  140. }
  141. /**
  142. * Implements callback_batch_finished().
  143. *
  144. * Triggers 'finished' callback for batch 3.
  145. */
  146. function _batch_test_finished_3($success, $results, $operations) {
  147. _batch_test_finished_helper(3, $success, $results, $operations);
  148. }
  149. /**
  150. * Implements callback_batch_finished().
  151. *
  152. * Triggers 'finished' callback for batch 4.
  153. */
  154. function _batch_test_finished_4($success, $results, $operations) {
  155. _batch_test_finished_helper(4, $success, $results, $operations);
  156. }
  157. /**
  158. * Implements callback_batch_finished().
  159. *
  160. * Triggers 'finished' callback for batch 5.
  161. */
  162. function _batch_test_finished_5($success, $results, $operations) {
  163. _batch_test_finished_helper(5, $success, $results, $operations);
  164. }