3 namespace Drupal\search_api_synonym\Command;
5 use Symfony\Component\Console\Input\InputInterface;
6 use Symfony\Component\Console\Input\InputOption;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Symfony\Component\Console\Command\Command;
9 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
10 use Drupal\Console\Core\Style\DrupalStyle;
13 * Drupal Console Command for export synonyms.
15 * @package Drupal\search_api_synonym
17 class ExportCommand extends Command {
19 use ContainerAwareCommandTrait;
24 protected function configure() {
26 ->setName('searchapi:synonym:export')
27 ->setDescription($this->trans('commands.searchapi.synonym.export.description'))
31 InputOption::VALUE_REQUIRED,
32 $this->trans('commands.searchapi.synonym.export.options.plugin.description')
37 InputOption::VALUE_REQUIRED,
38 $this->trans('commands.searchapi.synonym.export.options.langcode.description')
43 InputOption::VALUE_OPTIONAL,
44 $this->trans('commands.searchapi.synonym.export.options.type.description'),
50 InputOption::VALUE_OPTIONAL,
51 $this->trans('commands.searchapi.synonym.export.options.filter.description'),
57 InputOption::VALUE_OPTIONAL,
58 $this->trans('commands.searchapi.synonym.export.options.incremental.description')
63 InputOption::VALUE_OPTIONAL,
64 $this->trans('commands.searchapi.synonym.export.options.file.description')
71 protected function execute(InputInterface $input, OutputInterface $output) {
73 $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
76 $plugin = $input->getOption('plugin');
77 $langcode = $input->getOption('langcode');
78 $type = $input->getOption('type');
79 $filter = $input->getOption('filter');
80 $file = $input->getOption('file');
81 $incremental = $input->getOption('incremental');
84 $io = new DrupalStyle($input, $output);
86 // Validate option: plugin
87 if (!$pluginManager->validatePlugin($plugin)) {
89 $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidplugin'));
92 // Validate option: langcode
93 if (empty($langcode)) {
95 $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidlangcode'));
98 // Validate option: type
99 if (!empty($type) && !$this->validateOptionType($type)) {
101 $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidtype'));
104 // Validate option: filter
105 if (!empty($filter) && !$this->validateOptionFilter($filter)) {
107 $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidfilter'));
111 if (!isset($error)) {
112 $io->info($this->trans('commands.searchapi.synonym.export.messages.start'));
115 'langcode' => $langcode,
119 'incremental' => (int) $incremental,
121 $pluginManager->setPluginId($plugin);
122 $pluginManager->setExportOptions($options);
125 if ($result = $pluginManager->executeExport()) {
128 $io->info($this->trans('commands.searchapi.synonym.export.messages.success'));
135 * Validate that the type option is valid.
137 * @param string $type
138 * Type value from --type command option.
141 * TRUE if valid, FALSE if invalid.
143 private function validateOptionType($type) {
144 $types = ['synonym', 'spelling_error', 'all'];
145 return in_array($type, $types);
149 * Validate that the filter option is valid.
151 * @param string $filter
152 * Type value from --filter command option.
155 * TRUE if valid, FALSE if invalid.
157 private function validateOptionFilter($filter) {
158 $filters = ['nospace', 'onlyspace', 'all'];
159 return in_array($filter, $filters);