vendor/elasticsearch/elasticsearch/src/Client.php line 149

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elasticsearch PHP Client
  4.  *
  5.  * @link      https://github.com/elastic/elasticsearch-php
  6.  * @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
  7.  * @license   https://opensource.org/licenses/MIT MIT License
  8.  *
  9.  * Licensed to Elasticsearch B.V under one or more agreements.
  10.  * Elasticsearch B.V licenses this file to you under the MIT License.
  11.  * See the LICENSE file in the project root for more information.
  12.  */
  13. declare(strict_types 1);
  14. namespace Elastic\Elasticsearch;
  15. use Elastic\Elasticsearch\Response\Elasticsearch;
  16. use Elastic\Elasticsearch\Traits\ClientEndpointsTrait;
  17. use Elastic\Elasticsearch\Traits\EndpointTrait;
  18. use Elastic\Elasticsearch\Traits\NamespaceTrait;
  19. use Elastic\Elasticsearch\Transport\AsyncOnSuccess;
  20. use Elastic\Elasticsearch\Transport\AsyncOnSuccessNoException;
  21. use Elastic\Transport\Transport;
  22. use Http\Promise\Promise;
  23. use Psr\Http\Message\RequestInterface;
  24. use Psr\Log\LoggerInterface;
  25. final class Client implements ClientInterface
  26. {
  27.     const CLIENT_NAME 'es';
  28.     const VERSION '8.7.1';
  29.     const API_COMPATIBILITY_HEADER '%s/vnd.elasticsearch+%s; compatible-with=8';
  30.     
  31.     use ClientEndpointsTrait;
  32.     use EndpointTrait;
  33.     use NamespaceTrait;
  34.     
  35.     protected Transport $transport;
  36.     protected LoggerInterface $logger;
  37.     /**
  38.      * Specify is the request is asyncronous
  39.      */
  40.     protected bool $async false;
  41.     
  42.     /**
  43.      * Enable or disable the x-elastic-meta-header
  44.      */
  45.     protected bool $elasticMetaHeader true;
  46.     /**
  47.      * Enable or disable the response Exception
  48.      */
  49.     protected bool $responseException true;
  50.     /**
  51.      * The endpoint namespace storage 
  52.      */
  53.     protected array $namespace;
  54.     public function __construct(
  55.         Transport $transport
  56.         LoggerInterface $logger
  57.     ) {
  58.         $this->transport $transport;
  59.         $this->logger $logger;       
  60.         
  61.         $this->defaultTransportSettings($this->transport);
  62.     }
  63.     /**
  64.      * @inheritdoc
  65.      */
  66.     public function getTransport(): Transport
  67.     {
  68.         return $this->transport;
  69.     }
  70.     /**
  71.      * @inheritdoc
  72.      */
  73.     public function getLogger(): LoggerInterface
  74.     {
  75.         return $this->logger;
  76.     }
  77.     /**
  78.      * Set the default settings for Elasticsearch
  79.      */
  80.     protected function defaultTransportSettings(Transport $transport): void
  81.     {
  82.         $transport->setUserAgent('elasticsearch-php'self::VERSION);
  83.     }
  84.     /**
  85.      * @inheritdoc
  86.      */
  87.     public function setAsync(bool $async): self
  88.     {
  89.         $this->async $async;
  90.         return $this;
  91.     }
  92.     /**
  93.      * @inheritdoc
  94.      */
  95.     public function getAsync(): bool
  96.     {
  97.         return $this->async;
  98.     }
  99.     /**
  100.      * @inheritdoc
  101.      */
  102.     public function setElasticMetaHeader(bool $active): self
  103.     {
  104.         $this->elasticMetaHeader $active;
  105.         return $this;
  106.     }
  107.     /**
  108.      * @inheritdoc
  109.      */
  110.     public function getElasticMetaHeader(): bool
  111.     {
  112.         return $this->elasticMetaHeader;
  113.     }
  114.     /**
  115.      * @inheritdoc
  116.      */
  117.     public function setResponseException(bool $active): self
  118.     {
  119.         $this->responseException $active;
  120.         return $this;
  121.     }
  122.     /**
  123.      * @inheritdoc
  124.      */
  125.     public function getResponseException(): bool
  126.     {
  127.         return $this->responseException;
  128.     }
  129.     /**
  130.      * @inheritdoc
  131.      */
  132.     public function sendRequest(RequestInterface $request)
  133.     {   
  134.         // If async returns a Promise
  135.         if ($this->getAsync()) {
  136.             if ($this->getElasticMetaHeader()) {
  137.                 $this->transport->setElasticMetaHeader(Client::CLIENT_NAMEClient::VERSIONtrue);
  138.             }
  139.             $this->transport->setAsyncOnSuccess(
  140.                 $request->getMethod() === 'HEAD'
  141.                     ? new AsyncOnSuccessNoException
  142.                     : ($this->getResponseException() ? new AsyncOnSuccess : new AsyncOnSuccessNoException)
  143.             );
  144.             return $this->transport->sendAsyncRequest($request);
  145.         }     
  146.         if ($this->getElasticMetaHeader()) {
  147.             $this->transport->setElasticMetaHeader(Client::CLIENT_NAMEClient::VERSIONfalse);
  148.         }
  149.         $start microtime(true);
  150.         $response $this->transport->sendRequest($request);
  151.         $this->logger->info(sprintf("Response time in %.3f sec"microtime(true) - $start));       
  152.         $result = new Elasticsearch;
  153.         $result->setResponse($response$request->getMethod() === 'HEAD' false $this->getResponseException());
  154.         return $result;
  155.     }
  156. }