solrlog-1.0.1/src/SolariumTrait.php

src/SolariumTrait.php
<?php

namespace Drupal\solrlog;

use Drupal\search_api\SearchApiException;
use Drupal\search_api_solr\SolrBackendInterface;
use Drupal\search_api_solr\SolrConnectorInterface;

/**
 * Interaction with Solr connector for the logging purposes.
 */
trait SolariumTrait {

  /**
   * @var \Drupal\search_api_solr\SolrConnectorInterface
   */
  protected SolrConnectorInterface $solrConnector;

  /**
   * Array of Solr field names, keyed by Drupal field names.
   *
   * @var string[]
   */
  protected static $logFieldMappings = [
    'type' => 'ss_type',
    'uid' => 'itm_uid',
    'message' => 'tm_message',
    'variables' => 'tm_variables',
    'severity' => 'its_severity',
    'link' => 'ss_link',
    'location' => 'ss_location',
    'referer' => 'ss_referer',
    'hostname' => 'ss_hostname',
    'timestamp' => 'dt_timestamp',
  ];

  /**
   * Get solr connector.
   *
   * @return \Drupal\search_api_solr\SolrConnectorInterface|null
   *   Solr connector.
   */
  protected function getConnector() : ?SolrConnectorInterface {
    if (!empty($this->solrConnector)) {
      return $this->solrConnector;
    }
    $server = \Drupal::config('solrlog.settings')
      ->get('server');
    if (!$server) {
      return NULL;
    }
    try {
      $backend = \Drupal::entityTypeManager()
        ->getStorage('search_api_server')
        ->load($server)
        ->getBackend();
      if (!$backend instanceof SolrBackendInterface) {
        return NULL;
      }
    }
    catch (\Throwable) {
      return NULL;
    }
    try {
      return $backend->getSolrConnector();
    }
    catch (SearchApiException) {
      $this->serviceFunctional = FALSE;
      return NULL;
    }
  }

  /**
   * Get the list of log message types.
   *
   * @return array
   *   Message types map, i.e. ['php' => 'php', 'system' => 'system'] etc.
   *
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  public function getLogTypes() : array {
    $connector = $this->getConnector();
    if (!$connector instanceof SolrConnectorInterface) {
      return [];
    }
    $query = $connector->getSelectQuery();
    $query->addFilterQuery([
      'key' => 'module_id',
      'query' => 'ss_module_id:solrlog',
    ]);
    $facetSet = $query->getFacetSet();
    $facetSet->createFacetField('types')->setField(self::$logFieldMappings['type']);

    $apiResponse = $connector->search($query);
    $searchResult = $connector->createSearchResult(
      $query, $apiResponse);

    $logTypes = [];
    foreach (array_keys($searchResult->getFacetSet()
      ->getFacet('types')->getValues()) as $key) {
      $logTypes[$key] = $key;
    }
    return $logTypes;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc