Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Command / ExportCommand.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Command;
4
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;
11
12 /**
13  * Drupal Console Command for export synonyms.
14  *
15  * @package Drupal\search_api_synonym
16  */
17 class ExportCommand extends Command {
18
19   use ContainerAwareCommandTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function configure() {
25     $this
26       ->setName('searchapi:synonym:export')
27       ->setDescription($this->trans('commands.searchapi.synonym.export.description'))
28       ->addOption(
29         'plugin',
30         null,
31         InputOption::VALUE_REQUIRED,
32         $this->trans('commands.searchapi.synonym.export.options.plugin.description')
33       )
34       ->addOption(
35         'langcode',
36         null,
37         InputOption::VALUE_REQUIRED,
38         $this->trans('commands.searchapi.synonym.export.options.langcode.description')
39       )
40       ->addOption(
41         'type',
42         null,
43         InputOption::VALUE_OPTIONAL,
44         $this->trans('commands.searchapi.synonym.export.options.type.description'),
45         'all'
46       )
47       ->addOption(
48         'filter',
49         null,
50         InputOption::VALUE_OPTIONAL,
51         $this->trans('commands.searchapi.synonym.export.options.filter.description'),
52         'all'
53       )
54       ->addOption(
55         'incremental',
56         null,
57         InputOption::VALUE_OPTIONAL,
58         $this->trans('commands.searchapi.synonym.export.options.incremental.description')
59       )
60       ->addOption(
61         'file',
62         null,
63         InputOption::VALUE_OPTIONAL,
64         $this->trans('commands.searchapi.synonym.export.options.file.description')
65       );
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function execute(InputInterface $input, OutputInterface $output) {
72     // Plugin manager
73     $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');
74
75     // Options
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');
82
83     // Command output
84     $io = new DrupalStyle($input, $output);
85
86     // Validate option: plugin
87     if (!$pluginManager->validatePlugin($plugin)) {
88       $error = TRUE;
89       $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidplugin'));
90     }
91
92     // Validate option: langcode
93     if (empty($langcode)) {
94       $error = TRUE;
95       $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidlangcode'));
96     }
97
98     // Validate option: type
99     if (!empty($type) && !$this->validateOptionType($type)) {
100       $error = TRUE;
101       $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidtype'));
102     }
103
104     // Validate option: filter
105     if (!empty($filter) && !$this->validateOptionFilter($filter)) {
106       $error = TRUE;
107       $io->info($this->trans('commands.searchapi.synonym.export.messages.invalidfilter'));
108     }
109
110     // Prepare export
111     if (!isset($error)) {
112       $io->info($this->trans('commands.searchapi.synonym.export.messages.start'));
113
114       $options = [
115         'langcode' => $langcode,
116         'type' => $type,
117         'filter' => $filter,
118         'file' => $file,
119         'incremental' => (int) $incremental,
120       ];
121       $pluginManager->setPluginId($plugin);
122       $pluginManager->setExportOptions($options);
123
124       // Execute export
125       if ($result = $pluginManager->executeExport()) {
126
127         // Output result
128         $io->info($this->trans('commands.searchapi.synonym.export.messages.success'));
129         $io->info($result);
130       }
131     }
132   }
133
134   /**
135    * Validate that the type option is valid.
136    *
137    * @param string $type
138    *   Type value from --type command option.
139    *
140    * @return boolean
141    *   TRUE if valid, FALSE if invalid.
142    */
143   private function validateOptionType($type) {
144     $types = ['synonym', 'spelling_error', 'all'];
145     return in_array($type, $types);
146   }
147
148   /**
149    * Validate that the filter option is valid.
150    *
151    * @param string $filter
152    *   Type value from --filter command option.
153    *
154    * @return boolean
155    *   TRUE if valid, FALSE if invalid.
156    */
157   private function validateOptionFilter($filter) {
158     $filters = ['nospace', 'onlyspace', 'all'];
159     return in_array($filter, $filters);
160   }
161
162 }