You are here

public function WebformCliService::drush_webform_tidy in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::drush_webform_tidy()

Implements drush_hook_COMMAND().

Overrides WebformCliServiceInterface::drush_webform_tidy

File

src/Commands/WebformCliService.php, line 554

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_tidy($target = NULL) {
  global $config_directories;
  $target = $target ?: 'webform';
  $prefix = $this
    ->drush_get_option('prefix', 'webform');

  // [Drupal 8.8+] The sync directory is defined in $settings
  // and not $config_directories.
  // @see https://www.drupal.org/node/3018145
  $config_directory = Settings::get('config_' . $target . '_directory');
  if ($config_directory) {
    $file_directory_path = DRUPAL_ROOT . '/' . $config_directory;
    $dependencies = $this
      ->drush_get_option('dependencies');
  }
  elseif (isset($config_directories) && isset($config_directories[$target])) {
    $file_directory_path = DRUPAL_ROOT . '/' . $config_directories[$target];
    $dependencies = $this
      ->drush_get_option('dependencies');
  }
  elseif (\Drupal::moduleHandler()
    ->moduleExists($target)) {
    $file_directory_path = drupal_get_path('module', $target) . '/config';
    $dependencies = $this
      ->drush_get_option('dependencies');
  }
  else {
    $file_directory_path = realpath($target);
    $dependencies = FALSE;
  }
  $files = \Drupal::service('file_system')
    ->scanDirectory($file_directory_path, $prefix ? '/^' . preg_quote($prefix, '/.') . '.*\\.yml$/' : '/.*\\.yml$/');
  $this
    ->drush_print($this
    ->dt("Reviewing @count YAML configuration '@prefix.*' files in '@module'.", [
    '@count' => count($files),
    '@module' => $target,
    '@prefix' => $prefix,
  ]));
  $total = 0;
  foreach ($files as $file) {
    $original_yaml = file_get_contents($file->uri);
    $tidied_yaml = $original_yaml;
    try {
      $data = Yaml::decode($tidied_yaml);
    } catch (\Exception $exception) {
      $message = 'Error parsing: ' . $file->filename . PHP_EOL . $exception
        ->getMessage();
      if (strlen($message) > 255) {
        $message = substr($message, 0, 255) . '…';
      }
      $this
        ->drush_log($message, LogLevel::ERROR);
      $this
        ->drush_print($message);
      continue;
    }

    // Tidy elements.
    if (strpos($file->filename, 'webform.webform.') === 0 && isset($data['elements'])) {
      try {
        $elements = WebformYaml::tidy($data['elements']);
        $data['elements'] = $elements;
      } catch (\Exception $exception) {

        // Do nothing.
      }
    }

    // Add module dependency to exporter webform and webform options config entities.
    if ($dependencies && preg_match('/^(webform\\.webform\\.|webform\\.webform_options\\.)/', $file->filename)) {
      if (empty($data['dependencies']['enforced']['module']) || !in_array($target, $data['dependencies']['enforced']['module'])) {
        $this
          ->drush_print($this
          ->dt('Adding module dependency to @file…', [
          '@file' => $file->filename,
        ]));
        $data['dependencies']['enforced']['module'][] = $target;
      }
    }

    // Tidy and add new line to the end of the tidied file.
    $tidied_yaml = WebformYaml::encode($data) . PHP_EOL;
    if ($tidied_yaml !== $original_yaml) {
      $this
        ->drush_print($this
        ->dt('Tidying @file…', [
        '@file' => $file->filename,
      ]));
      file_put_contents($file->uri, $tidied_yaml);
      $total++;
    }
  }
  if ($total) {
    $this
      ->drush_print($this
      ->dt('@total YAML file(s) tidied.', [
      '@total' => $total,
    ]));
  }
  else {
    $this
      ->drush_print($this
      ->dt('No YAML files needed to be tidied.'));
  }
}