Skip to content

Commit

Permalink
Add HTTP Dependency Resolvers (#744)
Browse files Browse the repository at this point in the history
* Add FactoryDecoratorTrait

* Add RequestFactoryDecoratorTrait

* Add RequestFactoryDecorator

* Add ServerRequestFactoryDecorator

* Add ResponseFactoryDecorator

* Add MessageFactory

* Add FactoryResolverInterface

* Add PsrClientResolverInterface

* Add HttpPlugClientResolverInterface

* add DependencyResolverInterface

* Add HttpDiscovery\MessageFactoryResolver

* Add HttpDiscovery\PsrClientResolver

* Add HttpDiscovery\HttpPlugClientResolver

* Add HttpDiscovery\DependencyResolver

* Finalize

* Make Phan happy

* Make Psalm happy

* Make Phpstan happy

* Fix dependency

* Remove decorators for now
  • Loading branch information
tidal authored Jul 2, 2022
1 parent 47ee3e9 commit 5f318cd
Show file tree
Hide file tree
Showing 19 changed files with 811 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Adapter\HttpDiscovery;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use OpenTelemetry\SDK\Common\Http\DependencyResolverInterface;
use OpenTelemetry\SDK\Common\Http\HttpPlug\Client\ResolverInterface as HttpPlugClientResolverInterface;
use OpenTelemetry\SDK\Common\Http\Psr\Client\ResolverInterface as PsrClientResolverInterface;
use OpenTelemetry\SDK\Common\Http\Psr\Message\FactoryResolverInterface as MessageFactoryResolverInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

final class DependencyResolver implements DependencyResolverInterface
{
private MessageFactoryResolverInterface $messageFactoryResolver;
private PsrClientResolverInterface $psrClientResolver;
private HttpPlugClientResolverInterface $httpPlugClientResolver;

public function __construct(
?MessageFactoryResolverInterface $messageFactoryResolver = null,
?PsrClientResolverInterface $psrClientResolver = null,
?HttpPlugClientResolverInterface $httpPlugClientResolver = null
) {
$this->messageFactoryResolver = $messageFactoryResolver ?? MessageFactoryResolver::create();
$this->psrClientResolver = $psrClientResolver ?? PsrClientResolver::create();
$this->httpPlugClientResolver = $httpPlugClientResolver ?? HttpPlugClientResolver::create();
}

public static function create(
?MessageFactoryResolverInterface $messageFactoryResolver = null,
?PsrClientResolverInterface $psrClientResolver = null,
?HttpPlugClientResolverInterface $httpPlugClientResolver = null
): self {
return new self($messageFactoryResolver, $psrClientResolver, $httpPlugClientResolver);
}

public function resolveRequestFactory(): RequestFactoryInterface
{
return $this->messageFactoryResolver->resolveRequestFactory();
}

public function resolveResponseFactory(): ResponseFactoryInterface
{
return $this->messageFactoryResolver->resolveResponseFactory();
}

public function resolveServerRequestFactory(): ServerRequestFactoryInterface
{
return $this->messageFactoryResolver->resolveServerRequestFactory();
}

public function resolveStreamFactory(): StreamFactoryInterface
{
return $this->messageFactoryResolver->resolveStreamFactory();
}

public function resolveUploadedFileFactory(): UploadedFileFactoryInterface
{
return $this->messageFactoryResolver->resolveUploadedFileFactory();
}

public function resolveUriFactory(): UriFactoryInterface
{
return $this->messageFactoryResolver->resolveUriFactory();
}

public function resolveHttpPlugClient(): HttpClient
{
return $this->httpPlugClientResolver->resolveHttpPlugClient();
}

public function resolveHttpPlugAsyncClient(): HttpAsyncClient
{
return $this->httpPlugClientResolver->resolveHttpPlugAsyncClient();
}

public function resolvePsrClient(): ClientInterface
{
return $this->psrClientResolver->resolvePsrClient();
}
}
38 changes: 38 additions & 0 deletions src/SDK/Common/Adapter/HttpDiscovery/HttpPlugClientResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Adapter\HttpDiscovery;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpAsyncClientDiscovery;
use Http\Discovery\HttpClientDiscovery;
use OpenTelemetry\SDK\Common\Http\HttpPlug\Client\ResolverInterface;

final class HttpPlugClientResolver implements ResolverInterface
{
private ?HttpClient $httpClient;
private ?HttpAsyncClient $httpAsyncClient;

public function __construct(?HttpClient $httpClient = null, ?HttpAsyncClient $httpAsyncClient = null)
{
$this->httpClient = $httpClient;
$this->httpAsyncClient = $httpAsyncClient;
}

public static function create(?HttpClient $httpClient = null, ?HttpAsyncClient $httpAsyncClient = null): self
{
return new self($httpClient, $httpAsyncClient);
}

public function resolveHttpPlugClient(): HttpClient
{
return $this->httpClient ??= HttpClientDiscovery::find();
}

public function resolveHttpPlugAsyncClient(): HttpAsyncClient
{
return $this->httpAsyncClient ??= HttpAsyncClientDiscovery::find();
}
}
88 changes: 88 additions & 0 deletions src/SDK/Common/Adapter/HttpDiscovery/MessageFactoryResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Adapter\HttpDiscovery;

use Http\Discovery\Psr17FactoryDiscovery;
use OpenTelemetry\SDK\Common\Http\Psr\Message\FactoryResolverInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

final class MessageFactoryResolver implements FactoryResolverInterface
{
private ?RequestFactoryInterface $requestFactory;
private ?ResponseFactoryInterface $responseFactory;
private ?ServerRequestFactoryInterface $serverRequestFactory;
private ?StreamFactoryInterface $streamFactory;
private ?UploadedFileFactoryInterface $uploadedFileFactory;
private ?UriFactoryInterface $uriFactory;

public function __construct(
?RequestFactoryInterface $requestFactory = null,
?ResponseFactoryInterface $responseFactory = null,
?ServerRequestFactoryInterface $serverRequestFactory = null,
?StreamFactoryInterface $streamFactory = null,
?UploadedFileFactoryInterface $uploadedFileFactory = null,
?UriFactoryInterface $uriFactory = null
) {
$this->requestFactory = $requestFactory;
$this->responseFactory = $responseFactory;
$this->serverRequestFactory = $serverRequestFactory;
$this->streamFactory = $streamFactory;
$this->uploadedFileFactory = $uploadedFileFactory;
$this->uriFactory = $uriFactory;
}

public static function create(
?RequestFactoryInterface $requestFactory = null,
?ResponseFactoryInterface $responseFactory = null,
?ServerRequestFactoryInterface $serverRequestFactory = null,
?StreamFactoryInterface $streamFactory = null,
?UploadedFileFactoryInterface $uploadedFileFactory = null,
?UriFactoryInterface $uriFactory = null
): self {
return new self(
$requestFactory,
$responseFactory,
$serverRequestFactory,
$streamFactory,
$uploadedFileFactory,
$uriFactory
);
}

public function resolveRequestFactory(): RequestFactoryInterface
{
return $this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory();
}

public function resolveResponseFactory(): ResponseFactoryInterface
{
return $this->responseFactory ??= Psr17FactoryDiscovery::findResponseFactory();
}

public function resolveServerRequestFactory(): ServerRequestFactoryInterface
{
return $this->serverRequestFactory ??= Psr17FactoryDiscovery::findServerRequestFactory();
}

public function resolveStreamFactory(): StreamFactoryInterface
{
return $this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory();
}

public function resolveUploadedFileFactory(): UploadedFileFactoryInterface
{
return $this->uploadedFileFactory ??= Psr17FactoryDiscovery::findUploadedFileFactory();
}

public function resolveUriFactory(): UriFactoryInterface
{
return $this->uriFactory ??= Psr17FactoryDiscovery::findUriFactory();
}
}
29 changes: 29 additions & 0 deletions src/SDK/Common/Adapter/HttpDiscovery/PsrClientResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Adapter\HttpDiscovery;

use Http\Discovery\Psr18ClientDiscovery;
use OpenTelemetry\SDK\Common\Http\Psr\Client\ResolverInterface;
use Psr\Http\Client\ClientInterface;

final class PsrClientResolver implements ResolverInterface
{
private ?ClientInterface $client;

public function __construct(?ClientInterface $client = null)
{
$this->client = $client;
}

public static function create(?ClientInterface $client = null): self
{
return new self($client);
}

public function resolvePsrClient(): ClientInterface
{
return $this->client ??= Psr18ClientDiscovery::find();
}
}
13 changes: 13 additions & 0 deletions src/SDK/Common/Http/DependencyResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http;

use OpenTelemetry\SDK\Common\Http\HttpPlug\Client\ResolverInterface as HttpPlugClientResolverInterface;
use OpenTelemetry\SDK\Common\Http\Psr\Client\ResolverInterface as PsrClientResolverInterface;
use OpenTelemetry\SDK\Common\Http\Psr\Message\FactoryResolverInterface;

interface DependencyResolverInterface extends FactoryResolverInterface, PsrClientResolverInterface, HttpPlugClientResolverInterface
{
}
15 changes: 15 additions & 0 deletions src/SDK/Common/Http/HttpPlug/Client/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http\HttpPlug\Client;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;

interface ResolverInterface
{
public function resolveHttpPlugClient(): HttpClient;

public function resolveHttpPlugAsyncClient(): HttpAsyncClient;
}
12 changes: 12 additions & 0 deletions src/SDK/Common/Http/Psr/Client/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http\Psr\Client;

use Psr\Http\Client\ClientInterface;

interface ResolverInterface
{
public function resolvePsrClient(): ClientInterface;
}
22 changes: 22 additions & 0 deletions src/SDK/Common/Http/Psr/Message/FactoryResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http\Psr\Message;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

interface FactoryResolverInterface
{
public function resolveRequestFactory(): RequestFactoryInterface;
public function resolveResponseFactory(): ResponseFactoryInterface;
public function resolveServerRequestFactory(): ServerRequestFactoryInterface;
public function resolveStreamFactory(): StreamFactoryInterface;
public function resolveUploadedFileFactory(): UploadedFileFactoryInterface;
public function resolveUriFactory(): UriFactoryInterface;
}
52 changes: 52 additions & 0 deletions src/SDK/Common/Http/Psr/Message/MessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http\Psr\Message;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;

final class MessageFactory implements MessageFactoryInterface
{
private RequestFactoryInterface $requestFactory;
private ResponseFactoryInterface $responseFactory;
private ServerRequestFactoryInterface $serverRequestFactory;

public function __construct(
RequestFactoryInterface $requestFactory,
ResponseFactoryInterface $responseFactory,
ServerRequestFactoryInterface $serverRequestFactory
) {
$this->requestFactory = $requestFactory;
$this->responseFactory = $responseFactory;
$this->serverRequestFactory = $serverRequestFactory;
}

public static function create(
RequestFactoryInterface $requestFactory,
ResponseFactoryInterface $responseFactory,
ServerRequestFactoryInterface $serverRequestFactory
): self {
return new self($requestFactory, $responseFactory, $serverRequestFactory);
}

public function createRequest(string $method, $uri): RequestInterface
{
return $this->requestFactory->createRequest($method, $uri);
}

public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return $this->responseFactory->createResponse($code, $reasonPhrase);
}

public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return $this->serverRequestFactory->createServerRequest($method, $uri, $serverParams);
}
}
13 changes: 13 additions & 0 deletions src/SDK/Common/Http/Psr/Message/MessageFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Http\Psr\Message;

use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;

interface MessageFactoryInterface extends RequestFactoryInterface, ServerRequestFactoryInterface, ResponseFactoryInterface
{
}
Loading

0 comments on commit 5f318cd

Please sign in to comment.