Skip to content

Commit

Permalink
psr 17 + 18
Browse files Browse the repository at this point in the history
  • Loading branch information
janatjak committed May 13, 2023
1 parent 9314fb8 commit 4775e16
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 64 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
"php": "^8.0",
"ext-json": "*",
"nette/di": "^3.0",
"php-http/client-common": "^2.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.0",
"php-http/message": "^1.4",
"php-http/message-factory": "^1.0"
"php-http/client-common": "^2.6",
"php-http/discovery": "^1.0"
},
"require-dev": {
"freezy-bee/nette-caching-psr6": "^2.0",
Expand Down Expand Up @@ -52,6 +49,9 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
}
}
}
4 changes: 2 additions & 2 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Usage
-----

```php
$messageFactory = $container->getService('httplug.messageFactory');
$requestFactory = $container->getService('httplug.requestFactory');
$client = $container->getService('httplug.client.test');

$request = $messageFactory->createRequest('GET', 'https://google.com');
$request = $requestFactory->createRequest('GET', 'https://google.com');
$response = $client->sendRequest($request);
```

Expand Down
4 changes: 2 additions & 2 deletions src/ClientFactory/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace FreezyBee\Httplug\ClientFactory;

use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -14,5 +14,5 @@ interface ClientFactory
/**
* @param array<mixed> $config
*/
public function createClient(array $config = []): HttpClient;
public function createClient(array $config = []): ClientInterface;
}
4 changes: 2 additions & 2 deletions src/ClientFactory/CurlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace FreezyBee\Httplug\ClientFactory;

use Http\Client\Curl\Client;
use Http\Client\HttpClient;
use LogicException;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
class CurlFactory implements ClientFactory
{
public function createClient(array $config = []): HttpClient
public function createClient(array $config = []): ClientInterface
{
if (!class_exists(Client::class)) {
throw new LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
Expand Down
4 changes: 2 additions & 2 deletions src/ClientFactory/Guzzle7Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace FreezyBee\Httplug\ClientFactory;

use Http\Adapter\Guzzle7\Client;
use Http\Client\HttpClient;
use LogicException;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
class Guzzle7Factory implements ClientFactory
{
public function createClient(array $config = []): HttpClient
public function createClient(array $config = []): ClientInterface
{
if (!class_exists(Client::class)) {
throw new LogicException(
Expand Down
32 changes: 15 additions & 17 deletions src/DI/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
use FreezyBee\Httplug\Tracy\MessagePanel;
use FreezyBee\Httplug\Tracy\PluginClientDecorator;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Discovery\StreamFactoryDiscovery;
use Http\Discovery\UriFactoryDiscovery;
use Http\Message\MessageFactory;
use Http\Message\StreamFactory;
use Http\Message\UriFactory;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use InvalidArgumentException;
use Nette\DI\CompilerExtension;
use Nette\DI\ContainerBuilder;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\DI\Helpers;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;

/**
* @author Jakub Janata <[email protected]>
Expand All @@ -37,7 +35,7 @@ class HttplugExtension extends CompilerExtension
# uses discovery if not specified
'classes' => [
'client' => null,
'messageFactory' => null,
'requestFactory' => null,
'uriFactory' => null,
'streamFactory' => null
],
Expand All @@ -56,18 +54,18 @@ class HttplugExtension extends CompilerExtension

/** @var array<string, string> */
private array $classes = [
'client' => HttpClient::class,
'messageFactory' => MessageFactory::class,
'uriFactory' => UriFactory::class,
'streamFactory' => StreamFactory::class,
'client' => ClientInterface::class,
'requestFactory' => RequestFactoryInterface::class,
'uriFactory' => UriFactoryInterface::class,
'streamFactory' => StreamFactoryInterface::class,
];

/** @var array<string, mixed> */
private array $factoryClasses = [
'client' => [HttpClientDiscovery::class, 'find'],
'messageFactory' => [MessageFactoryDiscovery::class, 'find'],
'uriFactory' => [UriFactoryDiscovery::class, 'find'],
'streamFactory' => [StreamFactoryDiscovery::class, 'find'],
'client' => [Psr18ClientDiscovery::class, 'find'],
'requestFactory' => [Psr17FactoryDiscovery::class, 'findRequestFactory'],
'uriFactory' => [Psr17FactoryDiscovery::class, 'findUriFactory'],
'streamFactory' => [Psr17FactoryDiscovery::class, 'findStreamFactory'],
];

/**
Expand Down
19 changes: 3 additions & 16 deletions src/Tracy/PluginClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
namespace FreezyBee\Httplug\Tracy;

use Http\Client\Common\PluginClient;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Promise\Promise;
use Nette\SmartObject;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @author Jakub Janata <[email protected]>
*/
class PluginClientDecorator implements HttpClient, HttpAsyncClient
class PluginClientDecorator implements ClientInterface
{
use SmartObject;

private PluginClient $pluginClient;
private TracyPlugin $tracyPlugin;

/**
* PluginClientDecorator constructor.
* @param HttpClient|HttpAsyncClient $client
* @param array<mixed> $plugins
* @param array<mixed> $options
*/
public function __construct($client, array $plugins = [], array $options = [])
public function __construct(ClientInterface $client, array $plugins = [], array $options = [])
{
$plugins[] = $this->tracyPlugin = new TracyPlugin();
$this->pluginClient = new PluginClient($client, $plugins, $options);
Expand All @@ -51,13 +47,4 @@ public function sendRequest(RequestInterface $request): ResponseInterface

return $response;
}

/**
* @param RequestInterface $request
* @return Promise
*/
public function sendAsyncRequest(RequestInterface $request): Promise
{
return $this->pluginClient->sendAsyncRequest($request);
}
}
27 changes: 13 additions & 14 deletions tests/Integration/ServiceInstantiationTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require __DIR__ . '/../bootstrap.php';

use FreezyBee\Httplug\Tracy\PluginClientDecorator;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Http\Message\StreamFactory;
use Http\Message\UriFactory;
use Nette\Configurator;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Tester\Assert;
use Tester\TestCase;

Expand All @@ -36,18 +36,18 @@ class ServiceInstantiationTest extends TestCase
Assert::true($client instanceof PluginClient);

$clientBase = $container->getService('httplug.client');
Assert::true($clientBase instanceof HttpClient);
Assert::true($clientBase instanceof ClientInterface);

$messageFactory = $container->getService('httplug.messageFactory');
Assert::true($messageFactory instanceof MessageFactory);
$requestFactory = $container->getService('httplug.requestFactory');
Assert::true($requestFactory instanceof RequestFactoryInterface);

$uriFactory = $container->getService('httplug.uriFactory');
Assert::true($uriFactory instanceof UriFactory);
Assert::true($uriFactory instanceof UriFactoryInterface);

$streamFactory = $container->getService('httplug.streamFactory');
Assert::true($streamFactory instanceof StreamFactory);
Assert::true($streamFactory instanceof StreamFactoryInterface);

$request = $messageFactory->createRequest('GET', 'https://ifire.cz');
$request = $requestFactory->createRequest('GET', 'https://ifire.cz');
$response = $client->sendRequest($request);
Assert::same(200, $response->getStatusCode());
}
Expand All @@ -67,11 +67,10 @@ class ServiceInstantiationTest extends TestCase
$client = $container->getService('httplug.client.test');
Assert::true($client instanceof PluginClientDecorator);

/** @var MessageFactory $messageFactory */
$messageFactory = $container->getService('httplug.messageFactory');
Assert::true($messageFactory instanceof MessageFactory);
$requestFactory = $container->getService('httplug.requestFactory');
Assert::true($requestFactory instanceof RequestFactoryInterface);

$request = $messageFactory->createRequest('GET', 'https://ifire.cz');
$request = $requestFactory->createRequest('GET', 'https://ifire.cz');
$response = $client->sendRequest($request);
Assert::same(200, $response->getStatusCode());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Utils/TestPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace FreezyBee\Httplug\Tests\Utils;

use Http\Client\Common\PluginClient;
use Http\Message\MessageFactory;
use Nette\Application\Responses\TextResponse;
use Nette\Application\UI\Presenter;
use Psr\Http\Message\RequestFactoryInterface;

/**
* @author Jakub Janata <[email protected]>
Expand All @@ -26,8 +26,8 @@ public function actionDefault(): void
/** @var PluginClient $pluginClient */
$pluginClient = $this->context->getService('httplug.client.test5');

/** @var MessageFactory $factory */
$factory = $this->context->getByType(MessageFactory::class);
/** @var RequestFactoryInterface $factory */
$factory = $this->context->getByType(RequestFactoryInterface::class);
$request = $factory->createRequest('GET', '/test');

$pluginClient->sendRequest($request);
Expand Down

0 comments on commit 4775e16

Please sign in to comment.