Skip to content

Commit eb0e789

Browse files
committed
Better logger
1 parent be6f9f5 commit eb0e789

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

ClientFactory/PluginClientFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ final class PluginClientFactory
1616
* @param Plugin[] $plugins
1717
* @param ClientFactory $factory
1818
* @param array $config
19+
* @param array $pluginClientOptions
1920
*
2021
* @return PluginClient
2122
*/
22-
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config)
23+
public static function createPluginClient(array $plugins, ClientFactory $factory, array $config, array $pluginClientOptions = [])
2324
{
24-
return new PluginClient($factory->createClient($config), $plugins);
25+
return new PluginClient($factory->createClient($config), $plugins, $pluginClientOptions);
2526
}
2627
}

Collector/DebugPlugin.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
use Http\Client\Common\Plugin;
6+
use Http\Client\Exception;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
/**
11+
* A plugin used for log requests and responses passing through each plugin.
12+
*
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
class DebugPlugin implements Plugin
16+
{
17+
/**
18+
* @var DebugPluginCollector
19+
*/
20+
private $collector;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $clientName;
26+
27+
/**
28+
* @var int
29+
*/
30+
private $depth = -1;
31+
32+
/**
33+
*
34+
* @param DebugPluginCollector $collector
35+
* @param string $clientName
36+
*/
37+
public function __construct(DebugPluginCollector $collector, $clientName)
38+
{
39+
$this->collector = $collector;
40+
$this->clientName = $clientName;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
47+
{
48+
$collector = $this->collector;
49+
$clientName = $this->clientName;
50+
$depth = &$this->depth;
51+
52+
$collector->addRequest($request, $clientName, ++$depth);
53+
54+
return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) {
55+
$collector->addResponse($response, $clientName, --$depth);
56+
57+
58+
return $response;
59+
}, function (Exception $exception) use ($collector, $clientName, &$depth) {
60+
$collector->addFailure($exception, $clientName, --$depth);
61+
62+
throw $exception;
63+
});
64+
}
65+
}

Collector/DebugPluginCollector.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Collector;
4+
5+
use Http\Client\Exception;
6+
use Http\Message\Formatter;
7+
use Http\Message\Formatter\SimpleFormatter;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13+
14+
/**
15+
* @author Tobias Nyholm <[email protected]>
16+
*/
17+
class DebugPluginCollector extends DataCollector
18+
{
19+
/**
20+
* @var Formatter
21+
*/
22+
private $formatter;
23+
24+
/**
25+
* @param Formatter $formatter
26+
*/
27+
public function __construct(Formatter $formatter = null)
28+
{
29+
$this->formatter = $formatter ?: new SimpleFormatter();
30+
}
31+
32+
/**
33+
* @param RequestInterface $request
34+
*/
35+
public function addRequest(RequestInterface $request, $clientName, $depth)
36+
{
37+
$this->data[$clientName]['requests'][$depth][] = $this->formatter->formatRequest($request);
38+
}
39+
40+
/**
41+
* @param ResponseInterface $response
42+
*/
43+
public function addResponse(ResponseInterface $response, $clientName, $depth)
44+
{
45+
$this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response);
46+
}
47+
48+
/**
49+
* @param Exception $exception
50+
*/
51+
public function addFailure(Exception $exception, $clientName, $depth)
52+
{
53+
if ($exception instanceof Exception\HttpException) {
54+
$formattedResponse = $this->formatter->formatResponse($exception->getResponse());
55+
} elseif ($exception instanceof Exception\TransferException) {
56+
$formattedResponse = $exception->getMessage();
57+
} else {
58+
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception));
59+
}
60+
61+
$this->data[$clientName]['failure'][$depth][] = $formattedResponse;
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function collect(Request $request, Response $response, \Exception $exception = null)
68+
{
69+
// We do not need to collect any data from the Symfony Request and Response
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getName()
76+
{
77+
return 'httplug_debug';
78+
}
79+
}

DependencyInjection/HttplugExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\Client\Common\Plugin\AuthenticationPlugin;
88
use Http\Client\Common\PluginClient;
99
use Http\HttplugBundle\ClientFactory\DummyClient;
10+
use Http\HttplugBundle\Collector\DebugPlugin;
1011
use Http\Message\Authentication\BasicAuth;
1112
use Http\Message\Authentication\Bearer;
1213
use Http\Message\Authentication\Wsse;
@@ -211,6 +212,13 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
211212
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
212213
->addArgument($arguments['config']);
213214
} else {
215+
// Create a new plugin service for this client
216+
$serviceIdDebugPlugin = $serviceId.'.debug_plugin';
217+
$container->register($serviceIdDebugPlugin, DebugPlugin::class)
218+
->addArgument(new Reference('httplug.collector.debug_collector'))
219+
->addArgument($name)
220+
->setPublic(false);
221+
214222
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
215223
->addArgument(
216224
array_map(
@@ -221,7 +229,8 @@ function ($id) {
221229
)
222230
)
223231
->addArgument(new Reference($arguments['factory']))
224-
->addArgument($arguments['config']);
232+
->addArgument($arguments['config'])
233+
->addArgument(['debug_plugins'=>[new Reference($serviceIdDebugPlugin)]]);
225234
}
226235

227236

Resources/config/data-collector.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@
1313
<service id="httplug.collector.history_plugin" class="Http\Client\Common\Plugin\HistoryPlugin" public="false">
1414
<argument type="service" id="httplug.collector.message_journal"/>
1515
</service>
16+
17+
18+
<service id="httplug.collector.debug_collector" class="Http\HttplugBundle\Collector\DebugPluginCollector" public="false">
19+
</service>
20+
21+
1622
</services>
1723
</container>

0 commit comments

Comments
 (0)