Interim commit.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Batch / Batch.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Batch;
4
5 use Drupal\Core\StringTranslation\StringTranslationTrait;
6
7 /**
8  *
9  */
10 class Batch {
11
12   use StringTranslationTrait;
13
14   /**
15    * @var array
16    */
17   protected $batch;
18
19   /**
20    * @var array
21    */
22   protected $batchInfo;
23
24   const BATCH_TITLE = 'Generating XML sitemap';
25   const BATCH_INIT_MESSAGE = 'Initializing batch...';
26   const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
27   const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
28
29   /**
30    * Batch constructor.
31    */
32   public function __construct() {
33     $this->batch = [
34       'title' => $this->t(self::BATCH_TITLE),
35       'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
36       'error_message' => $this->t(self::BATCH_ERROR_MESSAGE),
37       'progress_message' => $this->t(self::BATCH_PROGRESS_MESSAGE),
38       'operations' => [],
39     // __CLASS__ . '::finishGeneration' not working possibly due to a drush error.
40       'finished' => [__CLASS__, 'finishGeneration'],
41     ];
42   }
43
44   /**
45    * @param array $batch_info
46    */
47   public function setBatchInfo(array $batch_info) {
48     $this->batchInfo = $batch_info;
49   }
50
51   /**
52    * Starts the batch process depending on where it was requested from.
53    */
54   public function start() {
55     switch ($this->batchInfo['from']) {
56
57       case 'form':
58         // Start batch process.
59         batch_set($this->batch);
60         break;
61
62       case 'drush':
63         // Start drush batch process.
64         batch_set($this->batch);
65         $this->batch =& batch_get();
66         $this->batch['progressive'] = FALSE;
67         drush_log($this->t(self::BATCH_INIT_MESSAGE), 'status');
68         drush_backend_batch_process();
69         break;
70
71       case 'backend':
72         // Start backend batch process.
73         batch_set($this->batch);
74         $this->batch =& batch_get();
75         $this->batch['progressive'] = FALSE;
76         // todo: Does not take advantage of batch API and eventually runs out of memory on very large sites. Use queue API instead?
77         batch_process();
78         break;
79
80       case 'nobatch':
81         // Call each batch operation the way the Drupal batch API would do, but
82         // within one process (so in fact not using batch API here, just
83         // mimicking it to avoid code duplication).
84         $context = [];
85         foreach ($this->batch['operations'] as $i => $operation) {
86           $operation[1][] = &$context;
87           call_user_func_array($operation[0], $operation[1]);
88         }
89         $this->finishGeneration(TRUE, $context['results'], []);
90         break;
91     }
92   }
93
94   /**
95    * Adds an operation to the batch.
96    *
97    * @param string $processing_method
98    * @param array $data
99    */
100   public function addOperation($processing_method, array $data) {
101     $this->batch['operations'][] = [
102       __CLASS__ . '::' . $processing_method, [$data, $this->batchInfo],
103     ];
104   }
105
106   /**
107    * Batch callback function which generates urls to entity paths.
108    *
109    * @param array $entity_info
110    * @param array $batch_info
111    * @param array &$context
112    *
113    * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
114    */
115   public static function generateBundleUrls(array $entity_info, array $batch_info, &$context) {
116     \Drupal::service('simple_sitemap.batch_url_generator')
117       ->setContext($context)
118       ->setBatchInfo($batch_info)
119       ->generateBundleUrls($entity_info);
120   }
121
122   /**
123    * Batch callback function which generates urls to custom paths.
124    *
125    * @param array $custom_paths
126    * @param array $batch_info
127    * @param array &$context
128    *
129    * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
130    */
131   public static function generateCustomUrls(array $custom_paths, array $batch_info, &$context) {
132     \Drupal::service('simple_sitemap.batch_url_generator')
133       ->setContext($context)
134       ->setBatchInfo($batch_info)
135       ->generateCustomUrls($custom_paths);
136   }
137
138   /**
139    * Callback function called by the batch API when all operations are finished.
140    *
141    * @param $success
142    * @param $results
143    * @param $operations
144    *
145    * @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
146    */
147   public static function finishGeneration($success, $results, $operations) {
148     \Drupal::service('simple_sitemap.batch_url_generator')
149       ->finishGeneration($success, $results, $operations);
150   }
151
152 }