vendor/sentry/sentry/src/ClientBuilder.php line 277

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry;
  4. use Http\Client\Common\Plugin as PluginInterface;
  5. use Http\Client\HttpAsyncClient;
  6. use Http\Discovery\MessageFactoryDiscovery;
  7. use Http\Discovery\StreamFactoryDiscovery;
  8. use Http\Discovery\UriFactoryDiscovery;
  9. use Http\Message\MessageFactory as MessageFactoryInterface;
  10. use Http\Message\StreamFactory as StreamFactoryInterface;
  11. use Http\Message\UriFactory as UriFactoryInterface;
  12. use Jean85\PrettyVersions;
  13. use Psr\Log\LoggerInterface;
  14. use Sentry\HttpClient\HttpClientFactory;
  15. use Sentry\HttpClient\PluggableHttpClientFactory;
  16. use Sentry\Serializer\RepresentationSerializer;
  17. use Sentry\Serializer\RepresentationSerializerInterface;
  18. use Sentry\Serializer\Serializer;
  19. use Sentry\Serializer\SerializerInterface;
  20. use Sentry\Transport\DefaultTransportFactory;
  21. use Sentry\Transport\TransportFactoryInterface;
  22. use Sentry\Transport\TransportInterface;
  23. /**
  24.  * The default implementation of {@link ClientBuilderInterface}.
  25.  *
  26.  * @author Stefano Arlandini <sarlandini@alice.it>
  27.  */
  28. final class ClientBuilder implements ClientBuilderInterface
  29. {
  30.     /**
  31.      * @var Options The client options
  32.      */
  33.     private $options;
  34.     /**
  35.      * @var UriFactoryInterface|null The PSR-7 URI factory
  36.      */
  37.     private $uriFactory;
  38.     /**
  39.      * @var StreamFactoryInterface|null The PSR-17 stream factory
  40.      */
  41.     private $streamFactory;
  42.     /**
  43.      * @var MessageFactoryInterface|null The PSR-7 message factory
  44.      */
  45.     private $messageFactory;
  46.     /**
  47.      * @var TransportFactoryInterface|null The transport factory
  48.      */
  49.     private $transportFactory;
  50.     /**
  51.      * @var TransportInterface|null The transport
  52.      */
  53.     private $transport;
  54.     /**
  55.      * @var HttpAsyncClient|null The HTTP client
  56.      */
  57.     private $httpClient;
  58.     /**
  59.      * @var PluginInterface[] The list of Httplug plugins
  60.      */
  61.     private $httpClientPlugins = [];
  62.     /**
  63.      * @var SerializerInterface|null The serializer to be injected in the client
  64.      */
  65.     private $serializer;
  66.     /**
  67.      * @var RepresentationSerializerInterface|null The representation serializer to be injected in the client
  68.      */
  69.     private $representationSerializer;
  70.     /**
  71.      * @var LoggerInterface|null A PSR-3 logger to log internal errors and debug messages
  72.      */
  73.     private $logger;
  74.     /**
  75.      * @var string The SDK identifier, to be used in {@see Event} and {@see SentryAuth}
  76.      */
  77.     private $sdkIdentifier Client::SDK_IDENTIFIER;
  78.     /**
  79.      * @var string The SDK version of the Client
  80.      */
  81.     private $sdkVersion;
  82.     /**
  83.      * Class constructor.
  84.      *
  85.      * @param Options|null $options The client options
  86.      */
  87.     public function __construct(Options $options null)
  88.     {
  89.         $this->options $options ?? new Options();
  90.         $this->sdkVersion PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion();
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public static function create(array $options = []): ClientBuilderInterface
  96.     {
  97.         return new static(new Options($options));
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function getOptions(): Options
  103.     {
  104.         return $this->options;
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function setUriFactory(UriFactoryInterface $uriFactory): ClientBuilderInterface
  110.     {
  111.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  112.         $this->uriFactory $uriFactory;
  113.         return $this;
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function setMessageFactory(MessageFactoryInterface $messageFactory): ClientBuilderInterface
  119.     {
  120.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  121.         $this->messageFactory $messageFactory;
  122.         return $this;
  123.     }
  124.     /**
  125.      * {@inheritdoc}
  126.      */
  127.     public function setTransport(TransportInterface $transport): ClientBuilderInterface
  128.     {
  129.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0. Use the setTransportFactory() method instead.'__METHOD__), \E_USER_DEPRECATED);
  130.         $this->transport $transport;
  131.         return $this;
  132.     }
  133.     /**
  134.      * {@inheritdoc}
  135.      */
  136.     public function setHttpClient(HttpAsyncClient $httpClient): ClientBuilderInterface
  137.     {
  138.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  139.         $this->httpClient $httpClient;
  140.         return $this;
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function addHttpClientPlugin(PluginInterface $plugin): ClientBuilderInterface
  146.     {
  147.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  148.         $this->httpClientPlugins[] = $plugin;
  149.         return $this;
  150.     }
  151.     /**
  152.      * {@inheritdoc}
  153.      */
  154.     public function removeHttpClientPlugin(string $className): ClientBuilderInterface
  155.     {
  156.         @trigger_error(sprintf('Method %s() is deprecated since version 2.3 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  157.         foreach ($this->httpClientPlugins as $index => $httpClientPlugin) {
  158.             if (!$httpClientPlugin instanceof $className) {
  159.                 continue;
  160.             }
  161.             unset($this->httpClientPlugins[$index]);
  162.         }
  163.         return $this;
  164.     }
  165.     /**
  166.      * {@inheritdoc}
  167.      */
  168.     public function setSerializer(SerializerInterface $serializer): ClientBuilderInterface
  169.     {
  170.         $this->serializer $serializer;
  171.         return $this;
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer): ClientBuilderInterface
  177.     {
  178.         $this->representationSerializer $representationSerializer;
  179.         return $this;
  180.     }
  181.     /**
  182.      * {@inheritdoc}
  183.      */
  184.     public function setLogger(LoggerInterface $logger): ClientBuilderInterface
  185.     {
  186.         $this->logger $logger;
  187.         return $this;
  188.     }
  189.     /**
  190.      * {@inheritdoc}
  191.      */
  192.     public function setSdkIdentifier(string $sdkIdentifier): ClientBuilderInterface
  193.     {
  194.         $this->sdkIdentifier $sdkIdentifier;
  195.         return $this;
  196.     }
  197.     /**
  198.      * {@inheritdoc}
  199.      */
  200.     public function setSdkVersion(string $sdkVersion): ClientBuilderInterface
  201.     {
  202.         $this->sdkVersion $sdkVersion;
  203.         return $this;
  204.     }
  205.     /**
  206.      * Sets the version of the SDK package that generated this Event using the Packagist name.
  207.      *
  208.      * @param string $packageName The package name that will be used to get the version from (i.e. "sentry/sentry")
  209.      *
  210.      * @return $this
  211.      *
  212.      * @deprecated since version 2.2, to be removed in 3.0
  213.      */
  214.     public function setSdkVersionByPackageName(string $packageName): ClientBuilderInterface
  215.     {
  216.         @trigger_error(sprintf('Method %s() is deprecated since version 2.2 and will be removed in 3.0.'__METHOD__), \E_USER_DEPRECATED);
  217.         $this->sdkVersion PrettyVersions::getVersion($packageName)->getPrettyVersion();
  218.         return $this;
  219.     }
  220.     /**
  221.      * {@inheritdoc}
  222.      */
  223.     public function getClient(): ClientInterface
  224.     {
  225.         $this->transport $this->transport ?? $this->createTransportInstance();
  226.         return new Client($this->options$this->transport$this->createEventFactory(), $this->logger);
  227.     }
  228.     /**
  229.      * Sets the transport factory.
  230.      *
  231.      * @param TransportFactoryInterface $transportFactory The transport factory
  232.      *
  233.      * @return $this
  234.      */
  235.     public function setTransportFactory(TransportFactoryInterface $transportFactory): ClientBuilderInterface
  236.     {
  237.         $this->transportFactory $transportFactory;
  238.         return $this;
  239.     }
  240.     /**
  241.      * Creates a new instance of the transport mechanism.
  242.      */
  243.     private function createTransportInstance(): TransportInterface
  244.     {
  245.         if (null !== $this->transport) {
  246.             return $this->transport;
  247.         }
  248.         $transportFactory $this->transportFactory ?? $this->createDefaultTransportFactory();
  249.         return $transportFactory->create($this->options);
  250.     }
  251.     /**
  252.      * Instantiate the {@see EventFactory} with the configured serializers.
  253.      */
  254.     private function createEventFactory(): EventFactoryInterface
  255.     {
  256.         $this->serializer $this->serializer ?? new Serializer($this->options);
  257.         $this->representationSerializer $this->representationSerializer ?? new RepresentationSerializer($this->options);
  258.         return new EventFactory($this->serializer$this->representationSerializer$this->options$this->sdkIdentifier$this->sdkVersion);
  259.     }
  260.     /**
  261.      * Creates a new instance of the {@see DefaultTransportFactory} factory.
  262.      */
  263.     private function createDefaultTransportFactory(): DefaultTransportFactory
  264.     {
  265.         $this->messageFactory $this->messageFactory ?? MessageFactoryDiscovery::find();
  266.         $this->uriFactory $this->uriFactory ?? UriFactoryDiscovery::find();
  267.         $this->streamFactory $this->streamFactory ?? StreamFactoryDiscovery::find();
  268.         $httpClientFactory = new HttpClientFactory(
  269.             $this->uriFactory,
  270.             $this->messageFactory,
  271.             $this->streamFactory,
  272.             $this->httpClient,
  273.             $this->sdkIdentifier,
  274.             $this->sdkVersion
  275.         );
  276.         if (!empty($this->httpClientPlugins)) {
  277.             $httpClientFactory = new PluggableHttpClientFactory($httpClientFactory$this->httpClientPlugins);
  278.         }
  279.         return new DefaultTransportFactory($this->messageFactory$httpClientFactory$this->logger);
  280.     }
  281. }