-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Nyholm/toolbar
Added web profiler and toolbar
- Loading branch information
Showing
11 changed files
with
258 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Http\HttplugBundle\Collector; | ||
|
||
use Http\Client\Exception; | ||
use Http\Client\Plugin\Journal; | ||
use Http\Message\Formatter; | ||
use Http\Message\Formatter\SimpleFormatter; | ||
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; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class MessageJournal extends DataCollector implements Journal | ||
{ | ||
/** | ||
* @var Formatter | ||
*/ | ||
private $formatter; | ||
|
||
/** | ||
* @param Formatter $formatter | ||
*/ | ||
public function __construct(Formatter $formatter = null) | ||
{ | ||
$this->formatter = $formatter ?: new SimpleFormatter(); | ||
$this->data = ['success' => [], 'failure' => []]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addSuccess(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$this->data['success'][] = [ | ||
'request' => $this->formatter->formatRequest($request), | ||
'response' => $this->formatter->formatResponse($response), | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFailure(RequestInterface $request, Exception $exception) | ||
{ | ||
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['failure'][] = [ | ||
'request' => $this->formatter->formatRequest($request), | ||
'response' => $formattedResponse, | ||
]; | ||
} | ||
|
||
/** | ||
* Get the successful request-resonse pairs. | ||
* | ||
* @return array | ||
*/ | ||
public function getSucessfulRequests() | ||
{ | ||
return $this->data['success']; | ||
} | ||
|
||
/** | ||
* Get the failed request-resonse pairs. | ||
* | ||
* @return array | ||
*/ | ||
public function getFailedRequests() | ||
{ | ||
return $this->data['failure']; | ||
} | ||
|
||
/** | ||
* Get the total number of request made. | ||
* | ||
* @return int | ||
*/ | ||
public function getTotalRequests() | ||
{ | ||
return count($this->data['success']) + count($this->data['failure']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
// We do not need to collect any data form the Symfony Request and Response | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'httplug'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="httplug.collector.message_journal" class="Http\HttplugBundle\Collector\MessageJournal" public="false"> | ||
<tag name="data_collector" template="HttplugBundle::webprofiler.html.twig" priority="200" | ||
id="httplug"/> | ||
<argument>null</argument> | ||
</service> | ||
|
||
<service id="httplug.collector.history_plugin" class="Http\Client\Plugin\HistoryPlugin" public="false"> | ||
<argument type="service" id="httplug.collector.message_journal"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{% extends '@WebProfiler/Profiler/layout.html.twig' %} | ||
|
||
{% import _self as macro %} | ||
|
||
{% block toolbar %} | ||
{% if collector.totalRequests > 0 %} | ||
{% set icon %} | ||
{{ include('@WebProfiler/Icon/ajax.svg') }} | ||
<span class="sf-toolbar-status">{{ collector.totalRequests }}</span> | ||
{% endset %} | ||
|
||
{% set text %} | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Successful requests</b> | ||
<span>{{ collector.sucessfulRequests|length }}</span> | ||
</div> | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Faild requests</b> | ||
<span>{{ collector.failedRequests|length }}</span> | ||
</div> | ||
|
||
{% endset %} | ||
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %} | ||
{% endif %} | ||
{% endblock %} | ||
|
||
{% block head %} | ||
{# Optional. Here you can link to or define your own CSS and JS contents. #} | ||
{{ parent() }} | ||
{% endblock %} | ||
|
||
{% block menu %} | ||
{# This left-hand menu appears when using the full-screen profiler. #} | ||
<span class="label {{ collector.totalRequests == 0 ? 'disabled' }}"> | ||
<span class="icon">{{ include('@WebProfiler/Icon/ajax.svg') }}</span> | ||
<strong>Httplug</strong> | ||
</span> | ||
{% endblock %} | ||
|
||
{% block panel %} | ||
<h2>HTTPlug</h2> | ||
{% if (collector.failedRequests|length > 0) %} | ||
<h3>Failed requests</h3> | ||
{{ macro.printMessages(collector.failedRequests) }} | ||
{% endif %} | ||
|
||
{% if (collector.sucessfulRequests|length > 0) %} | ||
<h3>Successful requests</h3> | ||
{{ macro.printMessages(collector.sucessfulRequests) }} | ||
{% endif %} | ||
|
||
{% if collector.totalRequests == 0 %} | ||
|
||
<div class="empty"> | ||
<p>No request were sent.</p> | ||
</div> | ||
{% endif %} | ||
|
||
{% endblock %} | ||
|
||
{% macro printMessages(messages) %} | ||
<table> | ||
<tr> | ||
<th>Request</th> | ||
<th>Response</th> | ||
</tr> | ||
|
||
{% for message in messages %} | ||
<tr> | ||
<td>{{ message['request'] }}</td> | ||
<td>{{ message['response'] }}</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('httplug', array()); | ||
$container->loadFromExtension('httplug', []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('httplug', array( | ||
'main_alias' => array( | ||
'client' => 'my_client', | ||
$container->loadFromExtension('httplug', [ | ||
'main_alias' => [ | ||
'client' => 'my_client', | ||
'message_factory' => 'my_message_factory', | ||
'uri_factory' => 'my_uri_factory', | ||
'stream_factory' => 'my_stream_factory', | ||
), | ||
'classes' => array( | ||
'client' => 'Http\Adapter\Guzzle6\Client', | ||
'uri_factory' => 'my_uri_factory', | ||
'stream_factory' => 'my_stream_factory', | ||
], | ||
'classes' => [ | ||
'client' => 'Http\Adapter\Guzzle6\Client', | ||
'message_factory' => 'Http\Message\MessageFactory\GuzzleMessageFactory', | ||
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory', | ||
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory', | ||
), | ||
)); | ||
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory', | ||
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory', | ||
], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters