View source
<?php
namespace Drupal\search_api_solr_devel\Logging;
use Drupal\Component\Utility\Timer;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Component\Serialization\Json;
use Drupal\devel\DevelDumperManagerInterface;
use Drupal\search_api\LoggerTrait;
use Drupal\search_api_solr\Utility\Utility;
use Solarium\Core\Client\Adapter\AdapterHelper;
use Solarium\Core\Event\Events;
use Solarium\QueryType\Select\Query\Query;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SolariumRequestLogger implements EventSubscriberInterface {
use StringTranslationTrait;
use LoggerTrait;
protected $develDumperManager;
public function __construct(DevelDumperManagerInterface $develDumperManager) {
$this->develDumperManager = $develDumperManager;
}
public static function getSubscribedEvents() {
return [
Events::POST_CREATE_QUERY => 'postCreateQuery',
Events::PRE_EXECUTE_REQUEST => 'preExecuteRequest',
Events::POST_EXECUTE_REQUEST => 'postExecuteRequest',
];
}
public function postCreateQuery($event) {
$query = $event
->getQuery();
if ($query instanceof Query) {
$query
->getDebug();
$query
->addParam('echoParams', 'all')
->setOmitHeader(FALSE);
}
}
public function showMessage($counter, $data, $message) {
$message = 'Request #' . $counter . '. ' . $message;
$this->develDumperManager
->message($data, $message, 'debug', 'kint');
}
public function timerStart($counter) {
Timer::start('search_api_solr_devel_' . $counter);
}
public function timerStop($counter) {
return Timer::stop('search_api_solr_devel_' . $counter);
}
public function shouldIgnore($handler) {
$regex = '/.*admin.*/';
return preg_match($regex, $handler);
}
public function preExecuteRequest($event) {
static $counter = 0;
$counter++;
$request = $event
->getRequest();
$endpoint = $event
->getEndpoint();
if ($this
->shouldIgnore($request
->getHandler())) {
return;
}
$debug = [
'request count' => $counter,
'datetime' => gmdate("Y-m-d\\TH:i:sP"),
'Solr request' => $request,
'Solr endpoint' => $endpoint,
'Solr URI' => AdapterHelper::buildUri($request, $endpoint),
];
$this
->showMessage($counter, $debug, 'Search API Solr Debug: Request');
$this->develDumperManager
->debug($debug, 'Search API Solr Debug: Request', 'default');
$this
->timerStart($counter);
}
public function postExecuteRequest($event) {
static $counter = 0;
$counter++;
if ($this
->shouldIgnore($event
->getRequest()
->getHandler())) {
return;
}
$timer = $this
->timerStop($counter);
$response = $event
->getResponse();
$debug = [
'request count' => $counter,
'datetime' => gmdate("Y-m-d\\TH:i:sP"),
'query_time' => 'Solr query took ' . $timer['time'] . 'ms.',
'Solr response headers' => $response
->getHeaders(),
'Solr response body' => $response
->getBody(),
];
$this
->showMessage($counter, $debug, 'Search API Solr Debug: Response');
$this->develDumperManager
->debug($debug, 'Search API Solr Debug: Response', 'default');
}
}