-
Notifications
You must be signed in to change notification settings - Fork 50
Improved collector #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Http\HttplugBundle\Collector; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Http\Client\Exception; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* A plugin used for log requests and responses. This plugin is executed between each normal plugin. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
final class DebugPlugin implements Plugin | ||
{ | ||
/** | ||
* @var DebugPluginCollector | ||
*/ | ||
private $collector; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $clientName; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $depth = -1; | ||
|
||
/** | ||
* @param DebugPluginCollector $collector | ||
* @param string $clientName | ||
*/ | ||
public function __construct(DebugPluginCollector $collector, $clientName) | ||
{ | ||
$this->collector = $collector; | ||
$this->clientName = $clientName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
$collector = $this->collector; | ||
$clientName = $this->clientName; | ||
$depth = &$this->depth; | ||
|
||
$collector->addRequest($request, $clientName, ++$depth); | ||
|
||
return $next($request)->then(function (ResponseInterface $response) use ($collector, $clientName, &$depth) { | ||
$collector->addResponse($response, $clientName, $depth--); | ||
|
||
return $response; | ||
}, function (Exception $exception) use ($collector, $clientName, &$depth) { | ||
$collector->addFailure($exception, $clientName, $depth--); | ||
|
||
throw $exception; | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php | ||
|
||
namespace Http\HttplugBundle\Collector; | ||
|
||
use Http\Client\Exception; | ||
use Http\Message\Formatter; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | ||
|
||
/** | ||
* A data collector for the debug plugin. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
final class DebugPluginCollector extends DataCollector | ||
{ | ||
/** | ||
* @var Formatter | ||
*/ | ||
private $formatter; | ||
|
||
/** | ||
* @var PluginJournal | ||
*/ | ||
private $journal; | ||
|
||
/** | ||
* @param Formatter $formatter | ||
* @param PluginJournal $journal | ||
*/ | ||
public function __construct(Formatter $formatter, PluginJournal $journal) | ||
{ | ||
$this->formatter = $formatter; | ||
$this->journal = $journal; | ||
} | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* @param string $clientName | ||
* @param int $depth | ||
*/ | ||
public function addRequest(RequestInterface $request, $clientName, $depth) | ||
{ | ||
$this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing phpdoc or inheritdoc |
||
* @param string $clientName | ||
* @param int $depth | ||
*/ | ||
public function addResponse(ResponseInterface $response, $clientName, $depth) | ||
{ | ||
$this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response); | ||
$this->data[$clientName]['failure'][$depth][] = false; | ||
} | ||
|
||
/** | ||
* @param Exception $exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing phpdoc or inheritdoc |
||
* @param string $clientName | ||
* @param int $depth | ||
*/ | ||
public function addFailure(Exception $exception, $clientName, $depth) | ||
{ | ||
if ($exception instanceof Exception\HttpException) { | ||
$formattedResponse = $this->formatter->formatResponse($exception->getResponse()); | ||
} elseif ($exception instanceof Exception\TransferException) { | ||
$formattedResponse = $exception->getMessage(); | ||
} else { | ||
$formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception)); | ||
} | ||
|
||
$this->data[$clientName]['response'][$depth][] = $formattedResponse; | ||
$this->data[$clientName]['failure'][$depth][] = true; | ||
} | ||
|
||
/** | ||
* Returns the successful request-resonse pairs. | ||
* | ||
* @return array | ||
*/ | ||
public function getSucessfulRequests() | ||
{ | ||
$count = 0; | ||
foreach ($this->data as $client) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline before control structure |
||
if (isset($client['failure'])) { | ||
foreach ($client['failure'][0] as $failure) { | ||
if (!$failure) { | ||
++$count; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
/** | ||
* Returns the failed request-resonse pairs. | ||
* | ||
* @return array | ||
*/ | ||
public function getFailedRequests() | ||
{ | ||
$count = 0; | ||
foreach ($this->data as $client) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline before control structure |
||
if (isset($client['failure'])) { | ||
foreach ($client['failure'][0] as $failure) { | ||
if ($failure) { | ||
++$count; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $count; | ||
} | ||
|
||
/** | ||
* Returns the total number of request made. | ||
* | ||
* @return int | ||
*/ | ||
public function getTotalRequests() | ||
{ | ||
return $this->getSucessfulRequests() + $this->getFailedRequests(); | ||
} | ||
|
||
/** | ||
* Return a RequestStackProvider for each client. | ||
* | ||
* @return RequestStackProvider[] | ||
*/ | ||
public function getClients() | ||
{ | ||
return RequestStackProvider::createFromCollectedData($this->data); | ||
} | ||
|
||
/** | ||
* @return PluginJournal | ||
*/ | ||
public function getJournal() | ||
{ | ||
return $this->journal; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
// We do not need to collect any data from the Symfony Request and Response | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'httplug'; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say we should add inheritdoc here. |
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function serialize() | ||
{ | ||
return serialize([$this->data, $this->journal]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unserialize($data) | ||
{ | ||
list($this->data, $this->journal) = unserialize($data); | ||
} | ||
} |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing phpdoc or inheritdoc