You are here

class SynonymDrushCommands in Search API Synonym 8

Drush commands for the Search API Synonym module.

@package Drupal\search_api_synonym\Command

Hierarchy

  • class \Drupal\search_api_synonym\Command\SynonymDrushCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of SynonymDrushCommands

1 string reference to 'SynonymDrushCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses SynonymDrushCommands
search_api_synonym.drush.commands in ./drush.services.yml
Drupal\search_api_synonym\Command\SynonymDrushCommands

File

src/Command/SynonymDrushCommands.php, line 19
Drush commands for the Search API Synonym module.

Namespace

Drupal\search_api_synonym\Command
View source
class SynonymDrushCommands extends DrushCommands {

  /**
   * @var \Drupal\search_api_synonym\Export\ExportPluginManager
   */
  protected $pluginManager;

  /**
   * Constructs the Drush command.
   *
   * @param \Drupal\search_api_synonym\Export\ExportPluginManager $pluginManager
   */
  public function __construct(ExportPluginManager $pluginManager) {
    $this->pluginManager = $pluginManager;
  }

  /**
   * Export search synonyms to a specific format.
   *
   * @command search-api-synonym:export
   * @option plugin Machine name of the export plugin. E.g. solr.
   * @option langcode Language being exported. Use the language code. E.g. en or da.
   * @option type Synonym type. Allowed values: synonym = Synomyms, spelling_error = Spelling errors, all = All types (synonyms and spelling errors). Defaults to "alL".
   * @option filter Export filter. Allowed values: nospace = Skip all words containing a space, onlyspace = Skip all words without a space. Defaults to "all".
   * @option incremental Incremental export - use Unix timestamp. Only export synonyms changed after the provided timestamp.
   * @option file File name used when saving the exported file. Include extension but not folder name!.
   * @aliases sapi-syn:export, sapi-syn-ex
   * @validate-module-enabled search_api_synonym
   * @usage drush search-api-synonym:export --plugin=solr langcode=da type=spelling_error filter=all
   *   Export all Danish spelling errors in the Solr format.
   * @usage sapi-syn:export --plugin=solr langcode=da type=spelling_error filter=all
   *   Export all Danish spelling errors in the Solr format.
   */
  public function export($options = [
    'plugin' => '',
    'langcode' => '',
    'type' => 'all',
    'filter' => 'all',
    'incremental' => 0,
    'file' => '',
  ]) {
    $error = FALSE;

    // Validate option: plugin
    if (!$this->pluginManager
      ->validatePlugin($options['plugin'])) {
      $error = TRUE;
      throw new \Exception('--plugin is not valid. Please, use an existing plugin machine name.');
    }

    // Validate option: langcode
    if (empty($options['langcode'])) {
      $error = TRUE;
      throw new \Exception('--langcode is not valid. Please, use an existing language code.');
    }

    // Validate option: type
    if (!$this
      ->validateOptionType($options['type'])) {
      $error = TRUE;
      throw new \Exception('--type option is not valid. The only allowed values are "synonym", "spelling_error", "all"');
    }

    // Validate option: filter
    if (!$this
      ->validateOptionFilter($options['filter'])) {
      $error = TRUE;
      throw new \Exception('--filter option is not valid. The only allowed values are "nospace", "onlyspace", "all"');
    }

    // Prepare export
    if (!$error) {
      $this
        ->output()
        ->writeln(dt('Starting synonym export....'));
      $options['incremental'] = (int) $options['incremental'];
      $this->pluginManager
        ->setPluginId($options['plugin']);
      $this->pluginManager
        ->setExportOptions($options);

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

        // Output result
        $this
          ->output()
          ->writeln(dt('Synonyms export and saved in the following file:'));
        $this
          ->output()
          ->writeln($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
SynonymDrushCommands::$pluginManager protected property
SynonymDrushCommands::export public function Export search synonyms to a specific format.
SynonymDrushCommands::validateOptionFilter private function Validate that the filter option is valid.
SynonymDrushCommands::validateOptionType private function Validate that the type option is valid.
SynonymDrushCommands::__construct public function Constructs the Drush command.