You are here

class ExportDrupalCommand in Search API Synonym 8

Drupal Console Command for export synonyms.

@package Drupal\search_api_synonym

@DrupalCommand ( extension="search_api_synonym", extensionType="module" )

Hierarchy

  • class \Drupal\search_api_synonym\Command\ExportDrupalCommand extends \Symfony\Component\Console\Command\Command uses \Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait

Expanded class hierarchy of ExportDrupalCommand

1 string reference to 'ExportDrupalCommand'
search_api_synonym.services.yml in ./search_api_synonym.services.yml
search_api_synonym.services.yml
1 service uses ExportDrupalCommand
search_api_synonym.command.export in ./search_api_synonym.services.yml
Drupal\search_api_synonym\Command\ExportDrupalCommand

File

src/Command/ExportDrupalCommand.php, line 23

Namespace

Drupal\search_api_synonym\Command
View source
class ExportDrupalCommand extends Command {
  use ContainerAwareCommandTrait;

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('searchapi:synonym:export')
      ->setDescription($this
      ->trans('commands.searchapi.synonym.export.description'))
      ->addOption('plugin', null, InputOption::VALUE_REQUIRED, $this
      ->trans('commands.searchapi.synonym.export.options.plugin.description'))
      ->addOption('langcode', null, InputOption::VALUE_REQUIRED, $this
      ->trans('commands.searchapi.synonym.export.options.langcode.description'))
      ->addOption('type', null, InputOption::VALUE_OPTIONAL, $this
      ->trans('commands.searchapi.synonym.export.options.type.description'), 'all')
      ->addOption('filter', null, InputOption::VALUE_OPTIONAL, $this
      ->trans('commands.searchapi.synonym.export.options.filter.description'), 'all')
      ->addOption('incremental', null, InputOption::VALUE_OPTIONAL, $this
      ->trans('commands.searchapi.synonym.export.options.incremental.description'))
      ->addOption('file', null, InputOption::VALUE_OPTIONAL, $this
      ->trans('commands.searchapi.synonym.export.options.file.description'));
  }

  /**
   * {@inheritdoc}
   */
  protected function execute(InputInterface $input, OutputInterface $output) {

    // Plugin manager
    $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');

    // Options
    $plugin = $input
      ->getOption('plugin');
    $langcode = $input
      ->getOption('langcode');
    $type = $input
      ->getOption('type');
    $filter = $input
      ->getOption('filter');
    $file = $input
      ->getOption('file');
    $incremental = $input
      ->getOption('incremental');

    // Command output
    $io = new DrupalStyle($input, $output);

    // Validate option: plugin
    if (!$pluginManager
      ->validatePlugin($plugin)) {
      $error = TRUE;
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.invalidplugin'));
    }

    // Validate option: langcode
    if (empty($langcode)) {
      $error = TRUE;
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.invalidlangcode'));
    }

    // Validate option: type
    if (!empty($type) && !$this
      ->validateOptionType($type)) {
      $error = TRUE;
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.invalidtype'));
    }

    // Validate option: filter
    if (!empty($filter) && !$this
      ->validateOptionFilter($filter)) {
      $error = TRUE;
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.invalidfilter'));
    }

    // Prepare export
    if (!isset($error)) {
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.start'));
      $options = [
        'langcode' => $langcode,
        'type' => $type,
        'filter' => $filter,
        'file' => $file,
        'incremental' => (int) $incremental,
      ];
      $pluginManager
        ->setPluginId($plugin);
      $pluginManager
        ->setExportOptions($options);

      // Execute export
      if ($result = $pluginManager
        ->executeExport()) {

        // Output result
        $io
          ->info($this
          ->trans('commands.searchapi.synonym.export.messages.success'));
        $io
          ->info($result);
      }
    }
  }

  /**
   * Validate that the type option is valid.
   *
   * @param string $type
   *   Type value from --type command option.
   *
   * @return boolean
   *   TRUE if valid, FALSE if invalid.
   */
  private function validateOptionType($type) {
    $types = [
      'synonym',
      'spelling_error',
      'all',
    ];
    return in_array($type, $types);
  }

  /**
   * Validate that the filter option is valid.
   *
   * @param string $filter
   *   Type value from --filter command option.
   *
   * @return boolean
   *   TRUE if valid, FALSE if invalid.
   */
  private function validateOptionFilter($filter) {
    $filters = [
      'nospace',
      'onlyspace',
      'all',
    ];
    return in_array($filter, $filters);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExportDrupalCommand::configure protected function
ExportDrupalCommand::execute protected function
ExportDrupalCommand::validateOptionFilter private function Validate that the filter option is valid.
ExportDrupalCommand::validateOptionType private function Validate that the type option is valid.