You are here

class AcsfDuplicationScrubTruncateTablesHandler 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

Truncates various undesirable Drupal core tables.

Hierarchy

Expanded class hierarchy of AcsfDuplicationScrubTruncateTablesHandler

File

src/Event/AcsfDuplicationScrubTruncateTablesHandler.php, line 8

Namespace

Drupal\acsf\Event
View source
class AcsfDuplicationScrubTruncateTablesHandler extends AcsfEventHandler {

  /**
   * Implements AcsfEventHandler::handle().
   */
  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);
  }

  /**
   * Truncates database tables.
   *
   * @param array $tables
   *   The list of tables to be truncated.
   */
  public function truncateTables(array $tables = []) {
    $connection = \Drupal::database();
    foreach ($tables as $table) {
      if ($connection
        ->schema()
        ->tableExists($table)) {
        $connection
          ->truncate($table)
          ->execute();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcsfDuplicationScrubTruncateTablesHandler::handle public function Implements AcsfEventHandler::handle(). Overrides AcsfEventHandler::handle
AcsfDuplicationScrubTruncateTablesHandler::truncateTables public function Truncates database tables.
AcsfEventHandler::$completed public property The time that the handler was completed.
AcsfEventHandler::$message public property Any messages triggered by the handler.
AcsfEventHandler::$started public property The time that the handler was started.
AcsfEventHandler::__construct public function Constructor. 1