You are here

abstract class TextExtractorPluginBase in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/TextExtractorPluginBase.php \Drupal\search_api_attachments\TextExtractorPluginBase

Base class for plugins able to extract file content.

Hierarchy

Expanded class hierarchy of TextExtractorPluginBase

6 files declare their use of TextExtractorPluginBase
DocconvExtractor.php in src/Plugin/search_api_attachments/DocconvExtractor.php
PdftotextExtractor.php in src/Plugin/search_api_attachments/PdftotextExtractor.php
PythonPdf2txtExtractor.php in src/Plugin/search_api_attachments/PythonPdf2txtExtractor.php
SolrExtractor.php in src/Plugin/search_api_attachments/SolrExtractor.php
TikaExtractor.php in src/Plugin/search_api_attachments/TikaExtractor.php

... See full list

File

src/TextExtractorPluginBase.php, line 21

Namespace

Drupal\search_api_attachments
View source
abstract class TextExtractorPluginBase extends PluginBase implements TextExtractorPluginInterface, ContainerFactoryPluginInterface {

  /**
   * Name of the config being edited.
   */
  const CONFIGNAME = 'search_api_attachments.admin_config';

  /**
   * Config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Stream wrapper manager service.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManager;

  /**
   * Mime type guesser service.
   *
   * @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
   */
  protected $mimeTypeGuesser;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function extract(File $file) {
    $filename = $file;
    $mode = 'r';
    return fopen($filename, $mode);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getmessenger() {
    return $this->messenger;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration += $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * Helper method to get the real path from an uri.
   *
   * @param string $uri
   *   The URI of the file, e.g. public://directory/file.jpg.
   *
   * @return mixed
   *   The real path to the file if it is a local file. An URL otherwise.
   */
  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();
    }
  }

  /**
   * Helper method to get the PDF MIME types.
   *
   * @return array
   *   An array of the PDF MIME types.
   */
  public function getPdfMimeTypes() {
    $pdf_mime_types = [];
    $pdf_mime_types[] = $this->mimeTypeGuesser
      ->guess('dummy.pdf');
    return $pdf_mime_types;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginFormInterface::buildConfigurationForm public function Form constructor. 36
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TextExtractorPluginBase::$configFactory protected property Config factory service.
TextExtractorPluginBase::$messenger protected property The messenger. Overrides MessengerTrait::$messenger
TextExtractorPluginBase::$mimeTypeGuesser protected property Mime type guesser service.
TextExtractorPluginBase::$streamWrapperManager protected property Stream wrapper manager service.
TextExtractorPluginBase::calculateDependencies public function
TextExtractorPluginBase::CONFIGNAME constant Name of the config being edited.
TextExtractorPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 2
TextExtractorPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
TextExtractorPluginBase::extract public function Extract method. Overrides TextExtractorPluginInterface::extract 6
TextExtractorPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
TextExtractorPluginBase::getmessenger public function
TextExtractorPluginBase::getPdfMimeTypes public function Helper method to get the PDF MIME types.
TextExtractorPluginBase::getRealpath public function Helper method to get the real path from an uri.
TextExtractorPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
TextExtractorPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 6
TextExtractorPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 5
TextExtractorPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 2