1: <?php
2:
3: namespace OpenSearch;
4:
5: use Http\Discovery\Psr17FactoryDiscovery;
6: use Http\Discovery\Psr18ClientDiscovery;
7: use OpenSearch\Serializers\SerializerInterface;
8: use OpenSearch\Serializers\SmartSerializer;
9: use Psr\Http\Client\ClientInterface;
10: use Psr\Http\Message\RequestFactoryInterface as PsrRequestFactoryInterface;
11: use Psr\Http\Message\StreamFactoryInterface;
12: use Psr\Http\Message\UriFactoryInterface;
13:
14: /**
15: * Creates a PSR transport falling back to a discovery mechanism if properties are not specified.
16: */
17: class TransportFactory
18: {
19: private ?PsrRequestFactoryInterface $psrRequestFactory = null;
20:
21: private ?StreamFactoryInterface $streamFactory = null;
22:
23: private ?UriFactoryInterface $uriFactory = null;
24:
25: private ?SerializerInterface $serializer = null;
26:
27: private ?RequestFactoryInterface $requestFactory = null;
28:
29: private ?ClientInterface $httpClient = null;
30:
31: protected function getHttpClient(): ?ClientInterface
32: {
33: return $this->httpClient;
34: }
35:
36: public function setHttpClient(?ClientInterface $httpClient): static
37: {
38: $this->httpClient = $httpClient;
39: return $this;
40: }
41:
42: protected function getRequestFactory(): ?RequestFactoryInterface
43: {
44: return $this->requestFactory;
45: }
46:
47: public function setRequestFactory(?RequestFactoryInterface $requestFactory): static
48: {
49: $this->requestFactory = $requestFactory;
50: return $this;
51: }
52:
53: protected function getPsrRequestFactory(): PsrRequestFactoryInterface
54: {
55: if ($this->psrRequestFactory === null) {
56: $this->psrRequestFactory = Psr17FactoryDiscovery::findRequestFactory();
57: }
58: return $this->psrRequestFactory;
59: }
60:
61: public function setPsrRequestFactory(PsrRequestFactoryInterface $psrRequestFactory): static
62: {
63: $this->psrRequestFactory = $psrRequestFactory;
64: return $this;
65: }
66:
67: protected function getStreamFactory(): StreamFactoryInterface
68: {
69: if ($this->streamFactory === null) {
70: $this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
71: }
72: return $this->streamFactory;
73: }
74:
75: public function setStreamFactory(StreamFactoryInterface $streamFactory): static
76: {
77: $this->streamFactory = $streamFactory;
78: return $this;
79: }
80:
81: protected function getUriFactory(): UriFactoryInterface
82: {
83: if ($this->uriFactory === null) {
84: $this->uriFactory = Psr17FactoryDiscovery::findUriFactory();
85: }
86: return $this->uriFactory;
87: }
88:
89: public function setUriFactory(UriFactoryInterface $uriFactory): static
90: {
91: $this->uriFactory = $uriFactory;
92: return $this;
93: }
94:
95: protected function getSerializer(): SerializerInterface
96: {
97: if ($this->serializer === null) {
98: $this->serializer = new SmartSerializer();
99: }
100: return $this->serializer;
101: }
102:
103: public function setSerializer(SerializerInterface $serializer): static
104: {
105: $this->serializer = $serializer;
106: return $this;
107: }
108:
109: /**
110: * Creates a new transport.
111: */
112: public function create(): HttpTransport
113: {
114: if ($this->requestFactory === null) {
115: $psrRequestFactory = $this->getPsrRequestFactory();
116: $streamFactory = $this->getStreamFactory();
117: $uriFactory = $this->getUriFactory();
118: $serializer = $this->getSerializer();
119:
120: $this->requestFactory = new RequestFactory(
121: $psrRequestFactory,
122: $streamFactory,
123: $uriFactory,
124: $serializer
125: );
126: }
127: if ($this->httpClient === null) {
128: $this->httpClient = Psr18ClientDiscovery::find();
129: }
130:
131: return new HttpTransport($this->httpClient, $this->requestFactory, $this->getSerializer());
132: }
133:
134: }
135: