diff --git a/Collector/Formatter.php b/Collector/Formatter.php index a77632fe..de648c83 100644 --- a/Collector/Formatter.php +++ b/Collector/Formatter.php @@ -7,6 +7,8 @@ use Http\Client\Exception\TransferException; use Http\Message\Formatter as MessageFormatter; use Http\Message\Formatter\CurlCommandFormatter; +use Psr\Http\Client\NetworkExceptionInterface; +use Psr\Http\Client\RequestExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -49,11 +51,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c */ public function formatException(Exception $exception) { - if ($exception instanceof HttpException) { + if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) { return $this->formatter->formatResponse($exception->getResponse()); } - if ($exception instanceof TransferException) { + if ($exception instanceof TransferException || $exception instanceof NetworkExceptionInterface) { return sprintf('Transfer error: %s', $exception->getMessage()); } diff --git a/Collector/PluginClientFactory.php b/Collector/PluginClientFactory.php index 25cd4896..91c22216 100644 --- a/Collector/PluginClientFactory.php +++ b/Collector/PluginClientFactory.php @@ -6,6 +6,7 @@ use Http\Client\Common\PluginClient; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; +use Psr\Http\Client\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -46,9 +47,9 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc } /** - * @param HttpClient|HttpAsyncClient $client - * @param Plugin[] $plugins - * @param array $options { + * @param HttpClient|ClientInterface|HttpAsyncClient $client + * @param Plugin[] $plugins + * @param array $options { * * @var string $client_name to give client a name which may be used when displaying client information like in * the HTTPlugBundle profiler. diff --git a/Collector/ProfileClient.php b/Collector/ProfileClient.php index b79f2e63..213d727f 100644 --- a/Collector/ProfileClient.php +++ b/Collector/ProfileClient.php @@ -7,6 +7,8 @@ use Http\Client\Exception\HttpException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Client\RequestExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\Stopwatch\Stopwatch; @@ -58,7 +60,7 @@ class ProfileClient implements HttpClient, HttpAsyncClient */ public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) { - if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { + if (!(($client instanceof ClientInterface || $client instanceof HttpClient) && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } @@ -181,7 +183,7 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa */ private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) { - if ($exception instanceof HttpException) { + if ($exception instanceof HttpException || $exception instanceof RequestExceptionInterface) { $this->collectResponseInformations($exception->getResponse(), $event, $stack); } diff --git a/Collector/ProfileClientFactory.php b/Collector/ProfileClientFactory.php index 613243ea..e2adffa4 100644 --- a/Collector/ProfileClientFactory.php +++ b/Collector/ProfileClientFactory.php @@ -6,6 +6,7 @@ use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use Http\HttplugBundle\ClientFactory\ClientFactory; +use Psr\Http\Client\ClientInterface; use Symfony\Component\Stopwatch\Stopwatch; /** @@ -61,7 +62,7 @@ public function createClient(array $config = []) { $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); - if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { + if (!(($client instanceof HttpClient || $client instanceof ClientInterface) && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } diff --git a/Tests/Functional/ServiceInstantiationTest.php b/Tests/Functional/ServiceInstantiationTest.php index 817d8aa4..71852e04 100644 --- a/Tests/Functional/ServiceInstantiationTest.php +++ b/Tests/Functional/ServiceInstantiationTest.php @@ -10,6 +10,8 @@ use Http\HttplugBundle\Collector\ProfilePlugin; use Http\HttplugBundle\Collector\StackPlugin; use Nyholm\NSA; +use Psr\Http\Client\ClientInterface; +use Psr\Http\Message\ResponseInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; @@ -77,6 +79,28 @@ public function testProfilingDecoration() $this->assertInstanceOf(ProfilePlugin::class, $plugins[4]); } + public function testProfilingPsr18Decoration() + { + if (!interface_exists(ClientInterface::class)) { + $this->markTestSkipped('PSR-18 is not installed'); + } + + static::bootKernel(['debug' => true, 'environment' => 'psr18']); + $container = static::$kernel->getContainer(); + + $client = $container->get('httplug.client.my_psr18'); + $this->assertInstanceOf(PluginClient::class, $client); + $profileClient = NSA::getProperty($client, 'client'); + $this->assertInstanceOf(ProfileClient::class, $profileClient); + + $flexibleClient = NSA::getProperty($profileClient, 'client'); + $psr18Client = NSA::getProperty($flexibleClient, 'httpClient'); + $this->assertInstanceOf(ClientInterface::class, $psr18Client); + + $response = $client->sendRequest(new \GuzzleHttp\Psr7\Request('GET', 'https://example.com')); + $this->assertInstanceOf(ResponseInterface::class, $response); + } + /** * {@inheritdoc} */ diff --git a/Tests/Resources/MyPsr18TestClient.php b/Tests/Resources/MyPsr18TestClient.php new file mode 100644 index 00000000..e4c0df00 --- /dev/null +++ b/Tests/Resources/MyPsr18TestClient.php @@ -0,0 +1,17 @@ +getEnvironment(), array('dev', 'test'))) { + if (in_array($this->getEnvironment(), array('dev', 'test', 'psr18'))) { $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); } diff --git a/Tests/Resources/app/config/config_psr18.yml b/Tests/Resources/app/config/config_psr18.yml new file mode 100644 index 00000000..f3aad063 --- /dev/null +++ b/Tests/Resources/app/config/config_psr18.yml @@ -0,0 +1,28 @@ +imports: + - { resource: config_test.yml } + +httplug: + discovery: + async_client: auto + clients: + my_psr18: + service: 'my_psr18_client' + public: true + plugins: + - + decoder: + use_content_encoding: false + - app.http.plugin.custom + - + add_host: + host: "http://localhost:8000" + - + authentication: + my_basic: + type: basic + username: foo + password: bar + +services: + my_psr18_client: + class: Http\HttplugBundle\Tests\Resources\MyPsr18TestClient diff --git a/composer.json b/composer.json index a0a4b834..469f59e5 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,10 @@ "autoload": { "psr-4": { "Http\\HttplugBundle\\": "" - } + }, + "exclude-from-classmap": [ + "/Tests/Resources/MyPsr18TestClient.php" + ] }, "autoload-dev": { "classmap": [