You are here

class CleanerCacheController in Cleaner 8

Class CleanerCacheController.

@package Drupal\cleaner\Controller

Hierarchy

Expanded class hierarchy of CleanerCacheController

1 string reference to 'CleanerCacheController'
cleaner.services.yml in ./cleaner.services.yml
cleaner.services.yml
1 service uses CleanerCacheController
cleaner_tables in ./cleaner.services.yml
Drupal\cleaner\Controller\CleanerCacheController

File

src/Controller/CleanerCacheController.php, line 13

Namespace

Drupal\cleaner\Controller
View source
class CleanerCacheController implements CleanerControllersInterface {

  /**
   * Configuration name.
   *
   * @var string
   */
  public static $configName = 'cleaner_clear_cache';

  /**
   * Additional config name.
   *
   * @var string
   */
  public static $additionalConfigName = 'cleaner_additional_tables';

  /**
   * Log level.
   *
   * @type string
   */
  protected static $logLevel;

  /**
   * Log message.
   *
   * @type string
   */
  protected static $logMessage;

  /**
   * Database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * {@inheritdoc}
   */
  public function execute() {
    $this
      ->clearTables();
  }

  /**
   * Clear cache tables.
   */
  protected function clearTables() {
    if (\Drupal::config(CLEANER_SETTINGS)
      ->get(self::$configName)) {
      $cleared = 0;
      $this->connection = \Drupal::database();

      // Prepare cache tables list.
      $tables = (array) $this
        ->cleanerGetCacheTables();

      // Ensure tables exists.
      if (!empty($tables)) {

        // Clear the tables and increments the cleared entries count.
        $cleared += (int) $this
          ->performClearing($tables);
      }

      // Log the operation.
      self::logResults($cleared);
    }
  }

  /**
   * Write log about a clearing results.
   *
   * @param int|null $cleared
   *   Count of cleared tables.
   */
  protected static function logResults($cleared = NULL) {
    if ($cleared <= 0 || empty($cleared)) {
      static::$logLevel = LogLevel::ERROR;
      static::$logMessage = 'There are no selected tables in the database.';
    }
    else {
      static::$logLevel = LogLevel::INFO;
      static::$logMessage = 'Cleared tables by Cleaner.';
    }
    \Drupal::service('cleaner_logger')
      ->log(static::$logLevel, static::$logMessage);
  }

  /**
   * Perform caches clearing work.
   *
   * @param array $tables
   *   Table names array.
   *
   * @return int
   *   Count of cleared tables.
   */
  protected function performClearing(array $tables) {
    $cleared = 0;

    // Additionally clearing caches for the static caches.
    static::cleanerClearStaticCaches();
    foreach ($tables as $table) {
      if (!$this->connection
        ->schema()
        ->tableExists($table)) {
        continue;
      }
      if ($this->connection
        ->query("TRUNCATE {$table}")
        ->execute()) {
        $cleared++;
      }
    }
    return $cleared;
  }

  /**
   * Clear Drupal statically cached data.
   */
  protected static function cleanerClearStaticCaches() {
    \Drupal::cache()
      ->deleteAll();
  }

  /**
   * Helper function for gathering all names of cache tables in DB.
   *
   * @return array
   *   List of all cache tables names.
   */
  protected function cleanerGetCacheTables() {
    $tables = [];
    $database_name = $this
      ->getDatabaseName();
    if (!empty($database_name)) {
      $query = $this->connection
        ->select('INFORMATION_SCHEMA.TABLES', 'tables')
        ->fields('tables', [
        'table_name',
        'table_schema',
      ])
        ->condition('table_schema', $database_name)
        ->condition('table_name', 'cache_%', 'LIKE')
        ->condition('table_name', 'cachetags', '<>');
      $tables = $query
        ->execute()
        ->fetchCol();
    }
    return array_merge((array) $tables, (array) $this
      ->getAdditionalTables());
  }

  /**
   * Get an additional tables for clearing.
   *
   * @return array
   *   Additional tables array.
   */
  protected function getAdditionalTables() {
    $tables = [];
    $additional = \Drupal::config(CLEANER_SETTINGS)
      ->get(self::$additionalConfigName);
    foreach (self::explode($additional) as $table) {
      if ($this->connection
        ->schema()
        ->tableExists($table)) {
        $tables[] = $table;
      }
    }
    return $tables;
  }

  /**
   * Explode the string into the array.
   *
   * @param string $string
   *   String to be exploded.
   *
   * @return array
   *   Exploded string in array format.
   */
  private static function explode($string = '') {
    return is_string($string) && !empty($string) ? explode(',', self::sanitize($string)) : [];
  }

  /**
   * Sanitize the string.
   *
   * @param string $input
   *   Input to be sanitized.
   *
   * @return string|null
   *   Sanitized string.
   */
  private static function sanitize($input = '') {
    return !empty($input) && is_string($input) ? Xss::filter(trim($input)) : NULL;
  }

  /**
   * Get the current database name.
   *
   * @return null|string
   *   Current database name.
   */
  private function getDatabaseName() {
    $options = $this->connection
      ->getConnectionOptions();
    return isset($options['database']) ? $options['database'] : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanerCacheController::$additionalConfigName public static property Additional config name.
CleanerCacheController::$configName public static property Configuration name.
CleanerCacheController::$connection protected property Database connection.
CleanerCacheController::$logLevel protected static property Log level.
CleanerCacheController::$logMessage protected static property Log message.
CleanerCacheController::cleanerClearStaticCaches protected static function Clear Drupal statically cached data.
CleanerCacheController::cleanerGetCacheTables protected function Helper function for gathering all names of cache tables in DB.
CleanerCacheController::clearTables protected function Clear cache tables.
CleanerCacheController::execute public function Perform the operation. Overrides CleanerControllersInterface::execute
CleanerCacheController::explode private static function Explode the string into the array.
CleanerCacheController::getAdditionalTables protected function Get an additional tables for clearing.
CleanerCacheController::getDatabaseName private function Get the current database name.
CleanerCacheController::logResults protected static function Write log about a clearing results.
CleanerCacheController::performClearing protected function Perform caches clearing work.
CleanerCacheController::sanitize private static function Sanitize the string.