Commit fe971ab5 authored by gbyte.co's avatar gbyte.co

Batch API now calls static method on every iteration which returns a Drupal service

parent 27657785
......@@ -43,10 +43,12 @@ class Batch {
switch ($this->batchInfo['from']) {
case 'form':
// Start batch process.
batch_set($this->batch);
break;
case 'drush':
// Start drush batch process.
batch_set($this->batch);
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
......@@ -55,6 +57,7 @@ class Batch {
break;
case 'backend':
// Start backend batch process.
batch_set($this->batch);
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
......@@ -62,6 +65,9 @@ class Batch {
break;
case 'nobatch':
// Call each batch operation the way the Drupal batch API would do, but
// within one process (so in fact not using batch API here, just
// mimicking it to avoid code duplication.
$context = [];
foreach($this->batch['operations'] as $i => $operation) {
$operation[1][] = &$context;
......@@ -92,7 +98,8 @@ class Batch {
* @param array &$context
*/
public static function generateBundleUrls($entity_info, $batch_info, &$context) {
\Drupal::service('simple_sitemap.batch_url_generator')->generateBundleUrls($entity_info, $batch_info, $context);
BatchUrlGenerator::service()
->generateBundleUrls($entity_info, $batch_info, $context);
}
/**
......@@ -103,7 +110,8 @@ class Batch {
* @param array &$context
*/
public static function generateCustomUrls($custom_paths, $batch_info, &$context) {
\Drupal::service('simple_sitemap.batch_url_generator')->generateCustomUrls($custom_paths, $batch_info, $context);
BatchUrlGenerator::service()
->generateCustomUrls($custom_paths, $batch_info, $context);
}
/**
......@@ -114,6 +122,7 @@ class Batch {
* @param $operations
*/
public static function finishGeneration($success, $results, $operations) {
\Drupal::service('simple_sitemap.batch_url_generator')->finishGeneration($success, $results, $operations);
BatchUrlGenerator::service()
->finishGeneration($success, $results, $operations);
}
}
......@@ -25,6 +25,15 @@ class BatchUrlGenerator {
protected $entityQuery;
protected $anonUser;
/**
* BatchUrlGenerator constructor.
*
* @param $sitemap_generator
* @param $language_manager
* @param $entity_type_manager
* @param $path_validator
* @param $entity_query
*/
public function __construct(
$sitemap_generator,
$language_manager,
......@@ -40,6 +49,22 @@ class BatchUrlGenerator {
$this->anonUser = $this->entityTypeManager->getStorage('user')->load(self::ANONYMOUS_USER_ID);
}
/**
* The Drupal batch API can only call procedural functions or static methods.
* To circumvent exclusively procedural code, on every batch iteration this
* static method is called by the batch API and returns a freshly created
* Drupal service object of this class. All following calls can be made on
* the returned service the OOP way. This is is obviously trading performance
* for cleanness. The service is created within its own class to improve
* testability.
*
* @return object
* Symfony service object of this class
*/
public static function service() {
return \Drupal::service('simple_sitemap.batch_url_generator');
}
/**
* @param $batch_info
* @return bool
......
......@@ -351,8 +351,9 @@ class Simplesitemap {
*
* @return string|false
* If no sitemap id provided, either a sitemap index is returned, or the
* whole sitemap, if the amount of links does not exceed the max links setting.
* If a sitemap id is provided, a sitemap chunk is returned.
* whole sitemap, if the amount of links does not exceed the max links
* setting. If a sitemap id is provided, a sitemap chunk is returned. False
* if sitemap is not retrievable from the database.
*/
public function getSitemap($chunk_id = NULL) {
$chunks = $this->fetchSitemapChunks();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment