TextExtractorPluginBase.php in Search API attachments 9.0.x
File
src/TextExtractorPluginBase.php
View source
<?php
namespace Drupal\search_api_attachments;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\file\Entity\File;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
abstract class TextExtractorPluginBase extends PluginBase implements TextExtractorPluginInterface, ContainerFactoryPluginInterface {
const CONFIGNAME = 'search_api_attachments.admin_config';
protected $configFactory;
protected $streamWrapperManager;
protected $mimeTypeGuesser;
protected $messenger;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager, MimeTypeGuesserInterface $mime_type_guesser, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->mimeTypeGuesser = $mime_type_guesser;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('stream_wrapper_manager'), $container
->get('file.mime_type.guesser'), $container
->get('messenger'));
}
public function extract(File $file) {
$filename = $file;
$mode = 'r';
return fopen($filename, $mode);
}
public function getConfiguration() {
return $this->configuration;
}
public function getmessenger() {
return $this->messenger;
}
public function setConfiguration(array $configuration) {
$this->configuration += $configuration;
}
public function defaultConfiguration() {
return [];
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$extractor_plugin_id = $form_state
->getValue('extraction_method');
$config = $this->configFactory
->getEditable(static::CONFIGNAME);
$config
->set($extractor_plugin_id . '_configuration', $this->configuration);
$config
->save();
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function calculateDependencies() {
return [];
}
public function getRealpath($uri) {
$wrapper = $this->streamWrapperManager
->getViaUri($uri);
$scheme = $this->streamWrapperManager
->getScheme($uri);
$local_wrappers = $this->streamWrapperManager
->getWrappers(StreamWrapperInterface::LOCAL);
if (in_array($scheme, array_keys($local_wrappers))) {
return $wrapper
->realpath();
}
else {
return $wrapper
->getExternalUrl();
}
}
public function getPdfMimeTypes() {
$pdf_mime_types = [];
$pdf_mime_types[] = $this->mimeTypeGuesser
->guessMimeType('dummy.pdf');
return $pdf_mime_types;
}
}