View source
<?php
namespace Drupal\search_api_synonym\Export;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class ExportPluginManager extends DefaultPluginManager {
protected $pluginId;
protected $exportOptions;
protected $fileSystem;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system = NULL) {
parent::__construct('Plugin/search_api_synonym/export', $namespaces, $module_handler, 'Drupal\\search_api_synonym\\Export\\ExportPluginInterface', 'Drupal\\search_api_synonym\\Annotation\\SearchApiSynonymExport');
$this
->alterInfo('search_api_synonym_export_info');
$this
->setCacheBackend($cache_backend, 'search_api_synonym_export_info_plugins');
$this->fileSystem = $file_system;
}
public function setPluginId($plugin_id) {
$this->pluginId = $plugin_id;
}
public function getPluginId() {
return $this->pluginId;
}
public function setExportOptions(array $export_options) {
$this->exportOptions = $export_options;
}
public function getExportOptions() {
return $this->exportOptions;
}
public function getExportOption($key) {
return isset($this->exportOptions[$key]) ? $this->exportOptions[$key] : '';
}
public function getAvailableExportPlugins() {
$plugins = $this
->getDefinitions();
$output = [];
foreach ($plugins as $id => $definition) {
$output[$id] = $definition;
}
return $output;
}
public function validatePlugin($plugin) {
if ($this
->getDefinition($plugin, FALSE)) {
return TRUE;
}
else {
return FALSE;
}
}
public function executeExport() {
$instance = $this
->createInstance($this
->getPluginId(), []);
$synonyms = $this
->getSynonymsData();
if (!($this
->getExportOption('incremental') && empty($synonyms))) {
$data = $instance
->getFormattedSynonyms($synonyms);
return $this
->saveSynonymsFile($data);
}
else {
return FALSE;
}
}
private function getSynonymsData() {
$query = \Drupal::database()
->select('search_api_synonym', 's');
$query
->fields('s', [
'sid',
'type',
'word',
'synonyms',
]);
$query
->condition('s.status', 1);
$query
->condition('s.langcode', $this
->getExportOption('langcode'));
$query
->orderBy('s.word');
$type = $this
->getExportOption('type');
if ($type && $type != 'all') {
$query
->condition('s.type', $type);
}
$filter = $this
->getExportOption('filter');
if ($filter && $filter != 'all') {
switch ($filter) {
case 'nospace':
$query
->condition('s.word', '% %', 'NOT LIKE');
$query
->condition('s.synonyms', '% %', 'NOT LIKE');
break;
case 'onlyspace':
$group = $query
->orConditionGroup()
->condition('s.word', '% %', 'LIKE')
->condition('s.synonyms', '% %', 'LIKE');
$query = $query
->condition($group);
break;
}
}
if ($incremental = $this
->getExportOption('incremental')) {
$query
->condition('s.changed', $incremental, '>=');
}
return $query
->execute()
->fetchAllAssoc('sid');
}
private function saveSynonymsFile($data) {
if ($file = $this
->getExportOption('file')) {
$filename = $file;
}
else {
$filename = $this
->generateFileName();
}
$folder = 'public://synonyms';
$this->fileSystem
->prepareDirectory($folder, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$path = $folder . '/' . $filename;
return $this->fileSystem
->saveData($data, $path, FileSystemInterface::EXISTS_REPLACE);
}
private function generateFileName() {
$options = $this
->getExportOptions();
$name[] = 'synonyms';
$name[] = "lang_{$options['langcode']}";
if (!empty($options['type'])) {
$name[] = "type_{$options['type']}";
}
if (!empty($options['filter'])) {
$name[] = "filter_{$options['filter']}";
}
return implode('__', $name) . '.txt';
}
}