vendor/nelmio/solarium-bundle/src/Logger.php line 122

  1. <?php
  2. namespace Nelmio\SolariumBundle;
  3. use Psr\Log\LoggerInterface;
  4. use Solarium\Core\Client\Request as SolariumRequest;
  5. use Solarium\Core\Client\Endpoint as SolariumEndpoint;
  6. use Solarium\Core\Plugin\AbstractPlugin as SolariumPlugin;
  7. use Solarium\Core\Event\Events as SolariumEvents;
  8. use Solarium\Core\Event\PreExecuteRequest as SolariumPreExecuteRequestEvent;
  9. use Solarium\Core\Event\PostExecuteRequest as SolariumPostExecuteRequestEvent;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Symfony\Component\HttpFoundation\Request as HttpRequest;
  12. use Symfony\Component\HttpFoundation\Response as HttpResponse;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. class Logger extends SolariumPlugin implements DataCollectorInterface\Serializable
  16. {
  17.     private $data = array();
  18.     private $queries = array();
  19.     private $currentRequest;
  20.     private $currentStartTime;
  21.     private $currentEndpoint;
  22.     private $logger;
  23.     private $stopwatch;
  24.     private $eventDispatchers = array();
  25.     /**
  26.      * Plugin init function
  27.      *
  28.      * Register event listeners
  29.      */
  30.     protected function initPluginType()
  31.     {
  32.         $dispatcher $this->client->getEventDispatcher();
  33.         if (!in_array($dispatcher$this->eventDispatcherstrue)) {
  34.             if ($dispatcher instanceof EventDispatcherInterface) {
  35.                 $dispatcher->addListener(SolariumEvents::PRE_EXECUTE_REQUEST, array($this'preExecuteRequest'), 1000);
  36.                 $dispatcher->addListener(SolariumEvents::POST_EXECUTE_REQUEST, array($this'postExecuteRequest'), -1000);
  37.             }
  38.             $this->eventDispatchers[] = $dispatcher;
  39.         }
  40.     }
  41.     public function setLogger(LoggerInterface $logger)
  42.     {
  43.         $this->logger $logger;
  44.     }
  45.     public function setStopwatch(Stopwatch $stopwatch)
  46.     {
  47.         $this->stopwatch $stopwatch;
  48.     }
  49.     public function log(SolariumRequest $request$responseSolariumEndpoint $endpoint$duration)
  50.     {
  51.         $this->queries[] = array(
  52.             'request' => $request,
  53.             'response' => $response,
  54.             'duration' => $duration,
  55.             'base_uri' => $this->getEndpointBaseUrl($endpoint),
  56.         );
  57.     }
  58.     public function collect(HttpRequest $requestHttpResponse $response/** \Throwable */ $exception null)
  59.     {
  60.         if (isset($this->currentRequest)) {
  61.             $this->failCurrentRequest();
  62.         }
  63.         $time 0;
  64.         foreach ($this->queries as $queryStruct) {
  65.             $time += $queryStruct['duration'];
  66.         }
  67.         $this->data = array(
  68.             'queries'     => $this->queries,
  69.             'total_time'  => $time,
  70.         );
  71.     }
  72.     public function getName(): string
  73.     {
  74.         return 'solr';
  75.     }
  76.     public function getQueries()
  77.     {
  78.         return array_key_exists('queries'$this->data) ? $this->data['queries'] : array();
  79.     }
  80.     public function getQueryCount()
  81.     {
  82.         return count($this->getQueries());
  83.     }
  84.     public function getTotalTime()
  85.     {
  86.         return array_key_exists('total_time'$this->data) ? $this->data['total_time'] : 0;
  87.     }
  88.     public function preExecuteRequest(SolariumPreExecuteRequestEvent $event)
  89.     {
  90.         if (isset($this->currentRequest)) {
  91.             $this->failCurrentRequest();
  92.         }
  93.         if (null !== $this->stopwatch) {
  94.             $this->stopwatch->start('solr''solr');
  95.         }
  96.         $this->currentRequest $event->getRequest();
  97.         $this->currentEndpoint $event->getEndpoint();
  98.         if (null !== $this->logger) {
  99.             $this->logger->debug($this->getEndpointBaseUrl($this->currentEndpoint) . $this->currentRequest->getUri());
  100.         }
  101.         $this->currentStartTime microtime(true);
  102.     }
  103.     public function postExecuteRequest(SolariumPostExecuteRequestEvent $event)
  104.     {
  105.         $endTime microtime(true) - $this->currentStartTime;
  106.         if (!isset($this->currentRequest)) {
  107.             throw new \RuntimeException('Request not set');
  108.         }
  109.         if ($this->currentRequest !== $event->getRequest()) {
  110.             throw new \RuntimeException('Requests differ');
  111.         }
  112.         if (null !== $this->stopwatch && $this->stopwatch->isStarted('solr')) {
  113.             $this->stopwatch->stop('solr');
  114.         }
  115.         $this->log($event->getRequest(), $event->getResponse(), $event->getEndpoint(), $endTime);
  116.         $this->currentRequest null;
  117.         $this->currentStartTime null;
  118.         $this->currentEndpoint null;
  119.     }
  120.     public function failCurrentRequest()
  121.     {
  122.         $endTime microtime(true) - $this->currentStartTime;
  123.         if (null !== $this->stopwatch && $this->stopwatch->isStarted('solr')) {
  124.             $this->stopwatch->stop('solr');
  125.         }
  126.         $this->log($this->currentRequestnull$this->currentEndpoint$endTime);
  127.         $this->currentRequest null;
  128.         $this->currentStartTime null;
  129.         $this->currentEndpoint null;
  130.     }
  131.     public function serialize()
  132.     {
  133.         return serialize($this->data);
  134.     }
  135.     public function unserialize($data)
  136.     {
  137.         $this->data unserialize($data);
  138.     }
  139.     public function reset()
  140.     {
  141.         $this->data = array();
  142.         $this->queries = array();
  143.     }
  144.     public function __serialize(): array
  145.     {
  146.         return $this->data;
  147.     }
  148.     public function __unserialize(array $data): void
  149.     {
  150.         $this->data $data;
  151.     }
  152.     private function getEndpointBaseUrl(SolariumEndpoint $endpoint): string
  153.     {
  154.         // Support for Solarium v4.2: getBaseUri() has been deprecated in favor of getCoreBaseUri()
  155.         return method_exists($endpoint'getCoreBaseUri') ? $endpoint->getCoreBaseUri() : $endpoint->getBaseUri();
  156.     }
  157. }