You are here

public function AcsfDuplicationScrubTruncateTablesHandler::handle in Acquia Cloud Site Factory Connector 8

Same name and namespace in other branches
  1. 8.2 src/Event/AcsfDuplicationScrubTruncateTablesHandler.php \Drupal\acsf\Event\AcsfDuplicationScrubTruncateTablesHandler::handle()

Implements AcsfEventHandler::handle().

Overrides AcsfEventHandler::handle

File

src/Event/AcsfDuplicationScrubTruncateTablesHandler.php, line 13

Class

AcsfDuplicationScrubTruncateTablesHandler
Truncates various undesirable Drupal core tables.

Namespace

Drupal\acsf\Event

Code

public function handle() {
  drush_print(dt('Entered @class', [
    '@class' => get_class($this),
  ]));
  $tables = [];

  // Clear search indexes and associated caches.
  if (\Drupal::moduleHandler()
    ->moduleExists('search')) {

    // In Drupal 8.8.0 and later, we need to use
    // \Drupal\search\SearchIndex::clear() to avoid deprecated code.
    // But in previous versions of Drupal, we need to use
    // search_index_clear(), which we need to invoke in an obscure way so that
    // PHPStan doesn't get mad at us for calling a deprecated function.
    if (version_compare(\Drupal::VERSION, '8.8.0', '>=')) {
      try {
        \Drupal::service('search.index')
          ->clear();
      } catch (\Exception $e) {
        drush_print(dt('Error occurred during clearing search indexes: @error', [
          '@error' => $e
            ->getMessage(),
        ]));
      }
    }
    else {
      call_user_func('search_index_clear');
    }

    // search_index_clear() does not truncate the following tables:
    $tables[] = 'search_total';
  }
  $tables[] = 'node_counter';
  $tables[] = 'batch';
  $tables[] = 'queue';
  $tables[] = 'semaphore';
  $tables[] = 'sessions';
  $tables[] = 'themebuilder_session';
  $this
    ->truncateTables($tables);
}