You are here

class DbReplacer in Convert Media Tags to Markup 8

Same name and namespace in other branches
  1. 2.x src/ConvertMediaTagsToMarkup/DbReplacer.php \Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup\DbReplacer

Code to replace all code in the database.

Hierarchy

Expanded class hierarchy of DbReplacer

File

src/ConvertMediaTagsToMarkup/DbReplacer.php, line 11

Namespace

Drupal\convert_media_tags_to_markup\ConvertMediaTagsToMarkup
View source
class DbReplacer {
  use Singleton;
  use CommonUtilities;

  /**
   * Log an error.
   *
   * @param string $err
   *   An error message.
   * @param string $log
   *   A log function, for example 'print_r' or 'dpm'.
   */
  protected function err(string $err, $log = 'print_r') {
    $log('An error occurred: ' . $err . PHP_EOL);
    $this->err = TRUE;
  }

  /**
   * Replace all instances of legacy media tags in the database to images.
   *
   * Outputs the results using print_r(). This is designed to be used via
   * drush.
   *
   * @param string $type
   *   A type such as node.
   * @param string $bundle
   *   A bundle such as article.
   * @param bool $simulate
   *   Whether or not to Simulate the results.
   * @param string $log
   *   For example "print_r" or "dpm".
   */
  public function replaceAll(string $type, string $bundle, bool $simulate = TRUE, string $log = 'print_r') {
    try {
      foreach ($this
        ->getAllEntities($type, $bundle) as $entity) {
        try {
          $entity
            ->process($simulate, $log);
        } catch (\Throwable $t) {
          $this
            ->err($t
            ->getMessage(), $log);
        }
      }
    } catch (\Throwable $t) {
      $this
        ->err($t
        ->getMessage() . ' at ' . $t
        ->getFile() . ':' . $t
        ->getLine(), $log);
    }
    if (!empty($this->err)) {
      $log('Exiting with error code 1 because at least one error occurred during processing.' . PHP_EOL);
      exit(1);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommonUtilities::decodeEntities public function Mockable wrapper around decodeEntities().
CommonUtilities::drupalJsonDecode public function Mockable wrapper around Json::decode().
CommonUtilities::fileLoad public function Mockable wrapper around File::load(). Also throws an exception.
CommonUtilities::getAllEntities protected function Get all entities of a specific type and bundle.
CommonUtilities::watchdog public function Log a string to the watchdog.
CommonUtilities::watchdogError public function Log an error to the watchdog.
CommonUtilities::watchdogThrowable public function Log a \Throwable to the watchdog.
DbReplacer::err protected function Log an error.
DbReplacer::replaceAll public function Replace all instances of legacy media tags in the database to images.
Singleton::$instance private static property Interal instance variable used with the instance() method.
Singleton::instance public static function Implements the Singleton design pattern.