Skip to content
This repository has been archived by the owner on Nov 22, 2020. It is now read-only.

Commit

Permalink
Big refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
perevoshchikov committed Apr 14, 2019
1 parent e8914ea commit f351c10
Show file tree
Hide file tree
Showing 36 changed files with 142 additions and 2,411 deletions.
7 changes: 0 additions & 7 deletions CHANGELOG.md

This file was deleted.

31 changes: 2 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[Predis](https://github.com/nrk/predis) collector for [PHP Debugbar](https://github.com/maximebf/php-debugbar).

![Screenshot](https://raw.githubusercontent.com/perevoshchikov/predis-collector/master/screenshot.png)
![Screenshot](screenshot.png)

## Install

Expand All @@ -18,50 +18,23 @@ $ composer require anper/predis-collector
## Usage

``` php
use Anper\PredisCollector\PredisAdapter;
use Anper\PredisCollector\PredisCollector;
use Predis\Client;

$client = new Client(...);

$collector = new PredisCollector();

$adapter = new PredisAdapter($collector);
$adapter->addClient($client);
$collector->addClient($client);

$debugbar->addCollector($collector);
```

## Formatters

* response formatters
```php
use Anper\PredisCollector\Format\Respose\ArrayFormatter;
use Anper\PredisCollector\Format\Respose\StringFormatter;
use Anper\PredisCollector\Format\Response\StatusFormatter;

$collector->addResponseFormatter(new ArrayFormatter());
$collector->addResponseFormatter(new StringFormatter());
$collector->addResponseFormatter(new StatusFormatter());
```

* command formatters
```php
use Anper\PredisCollector\Format\Command\HighlightFormatter;

$collector->addCommandFormatter(new HighlightFormatter());
```

## Test

``` bash
$ composer test
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"require": {
"php": "~7.1",
"anper/predis-command-collector": "^0.1",
"maximebf/debugbar": "^1.15",
"predis/predis": "^1.1"
},
Expand Down
52 changes: 8 additions & 44 deletions resources/css/widget.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
div.phpdebugbar-widgets-redis .phpdebugbar-widgets-status {
div.phpdebugbar-widgets-predis .phpdebugbar-widgets-status {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
padding: 6px 6px;
border-bottom: 1px solid #ddd;
Expand All @@ -7,65 +7,29 @@ div.phpdebugbar-widgets-redis .phpdebugbar-widgets-status {
background: #fafafa;
}

div.phpdebugbar-widgets-redis li.phpdebugbar-widgets-list-item.phpdebugbar-widgets-error {
color: red;
}

div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-database,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-duration,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-memory,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-response,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-connection {
div.phpdebugbar-widgets-predis span.phpdebugbar-widgets-connection {
float: right;
margin-left: 8px;
color: #888;
}
div.phpdebugbar-widgets-redis div.phpdebugbar-widgets-status span.phpdebugbar-widgets-database,
div.phpdebugbar-widgets-redis div.phpdebugbar-widgets-status span.phpdebugbar-widgets-duration,
div.phpdebugbar-widgets-redis div.phpdebugbar-widgets-status span.phpdebugbar-widgets-memory,
div.phpdebugbar-widgets-redis div.phpdebugbar-widgets-status span.phpdebugbar-widgets-response,
div.phpdebugbar-widgets-redis div.phpdebugbar-widgets-status span.phpdebugbar-widgets-connection {
color: #555;
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-database:before,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-duration:before,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-memory:before,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-response:before,
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-connection:before {

div.phpdebugbar-widgets-predis span.phpdebugbar-widgets-connection:before {
font-family: PhpDebugbarFontAwesome;
margin-right: 4px;
font-size: 12px;
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-database:before {
content: "\f1c0";
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-duration:before {
content: "\f017";
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-memory:before {
content: "\f085";
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-response:before {
content: "\f063";
}
div.phpdebugbar-widgets-redis span.phpdebugbar-widgets-connection:before {
content: "\f233";
}

div.phpdebugbar-widgets-redis li.phpdebugbar-widgets-list-item span.phpdebugbar-widgets-error {
display: block;
font-weight: bold;
}

div.phpdebugbar-widgets-redis code {
div.phpdebugbar-widgets-predis code {
white-space: pre-wrap;
overflow-wrap: break-word;
word-wrap: break-word;
}
div.phpdebugbar-widgets-redis li.phpdebugbar-widgets-list-item.phpdebugbar-widgets-duplicate {

div.phpdebugbar-widgets-predis li.phpdebugbar-widgets-list-item.phpdebugbar-widgets-duplicate {
background-color: #edeff0;
}

div.phpdebugbar-widgets-redis li.phpdebugbar-widgets-list-item.phpdebugbar-widgets-duplicate:hover {
div.phpdebugbar-widgets-predis li.phpdebugbar-widgets-list-item.phpdebugbar-widgets-duplicate:hover {
background-color: #ffc;
}
111 changes: 54 additions & 57 deletions resources/js/widget.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
(function($) {

var csscls = PhpDebugBar.utils.makecsscls('phpdebugbar-widgets-');

var NBSP = ' '; //  
var THINSP = ' '; //  

if (typeof(hljs) === 'object') {
hljs.registerLanguage('redis', function () {
// I'm sorry for this hack
return {
c: [
{
cN: 'number',
b: '\\b\\d+(\\.\\d+)?',
},
{
cN: 'keyword',
b: '[A-Z]+\\b',
},
{
cN: 'deletion',
b: '[' + NBSP + THINSP + ']+',
},
{
cN: 'string',
b: '\\b[\\w\\-\\:\\_]+\\b',
},
]
};
});
}

/**
* Widget for the displaying redis commands
*
* Options:
* - data
*/
var RedisCommandsWidget = PhpDebugBar.Widgets.RedisCommandsWidget = PhpDebugBar.Widget.extend({
var PredisCommandsWidget = PhpDebugBar.Widgets.PredisCommandsWidget = PhpDebugBar.Widget.extend({

className: csscls('redis'),
className: csscls('predis'),

render: function() {
this.$status = $('<div />')
.addClass(csscls('status'))
.appendTo(this.$el);

this.$list = new PhpDebugBar.Widgets.ListWidget({ itemRenderer: function(li, stmt) {
$('<code />')
.html(stmt.prepared_profile)
.appendTo(li);
var code = stmt.method;

if (stmt.duration_str) {
$('<span title="Duration" />')
.addClass(csscls('duration'))
.text(stmt.duration_str)
.appendTo(li);
}
for (var i = 0; i < stmt.arguments.length; i++) {
var arg = stmt.arguments[i];

if (stmt.memory_str) {
$('<span title="Memory usage" />')
.addClass(csscls('memory'))
.text(stmt.memory_str)
.appendTo(li);
if (arg === '') {
code += ' '+ THINSP;
} else if (/^\s+$/.test(arg)) {
code += ' ' + arg.replace(/\s/g, NBSP);
} else {
code += ' ' + arg;
}
}

if (typeof(stmt.connection_id) != 'undefined' && stmt.connection_id) {
$('<code />')
.html(PhpDebugBar.Widgets.highlight(code, 'redis'))
.appendTo(li);

if (typeof(stmt.connection) != 'undefined' && stmt.connection) {
$('<span title="Connection" />')
.addClass(csscls('connection'))
.text(stmt.connection_id)
.text(stmt.connection)
.appendTo(li);
}

if (stmt.prepared_response) {
$('<span title="Response" />')
.addClass(csscls('response'))
.text(stmt.prepared_response)
.appendTo(li);
}
li
.data('method', stmt.method)
.data('arguments', stmt.arguments)
.data('connecton', stmt.connection);

if (typeof(stmt.is_success) != 'undefined' && !stmt.is_success) {
li.addClass(csscls('error'));
li.append($('<span />')
.addClass(csscls('error'))
.text(stmt.error_message));
}
li.trigger('prediscommandswidgetitem');
}});

this.$list.$el.appendTo(this.$el);
Expand All @@ -71,13 +92,9 @@

// Search for duplicate and failed profiles.
for (var map = {}, unique = 0, duplicate = 0, failed = 0, i = 0; i < data.profiles.length; i++) {
var stmt = data.profiles[i].prepared_profile;
var stmt = data.profiles[i].method + data.profiles[i].arguments.join();
map[stmt] = map[stmt] || { keys: [] };
map[stmt].keys.push(i);

if (data.profiles[i].is_success === false) {
failed++;
}
}

// Add classes to all duplicate profiles.
Expand All @@ -101,32 +118,12 @@
.text(data.nb_profiles + " commands were executed")
.appendTo(this.$status);

if (failed) {
t.append(", " + failed + " of which failed");
}

if (duplicate) {
t.append(", " + duplicate + " of which were duplicates");
t.append(", " + unique + " unique");
}

if (data.duration_str) {
this.$status
.append(
$('<span title="Accumulated duration" />')
.addClass(csscls('duration'))
.text(data.duration_str)
);
}

if (data.memory_str) {
this.$status
.append(
$('<span title="Memory usage" />')
.addClass(csscls('memory'))
.text(data.memory_str)
);
}
this.$el.trigger('prediscommandswidgetready');
});
}
});
Expand Down
16 changes: 10 additions & 6 deletions sandbox/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
require_once __DIR__ . '/../vendor/autoload.php';

$redis = new \Predis\Client([
'host' => '127.0.0.1',
'port' => 6379,
'host' => '127.0.0.1',
'port' => 6379,
]);

try {
Expand All @@ -24,16 +24,14 @@
$debugbar = new \DebugBar\DebugBar();

$collector = new \Anper\PredisCollector\PredisCollector();
$adapter = new \Anper\PredisCollector\PredisAdapter($collector);
$adapter->addClient($redis);
$collector->addClient($redis);

$debugbar->addCollector($collector);

$redis->set('foo', 'bar');
$redis->get('foo');
$redis->get('foo');
$redis->del(['foo']);
$redis->hmset('hash', ['one', 'two', '']);

$debugbar->collect();

$renderer = $debugbar->getJavascriptRenderer();
Expand Down Expand Up @@ -63,6 +61,12 @@

return true;
} elseif (\in_array($uri, $files, true)) {
if (\preg_match('/\.css$/', $uri)) {
\header('Content-Type: text/css');
} elseif (\preg_match('/\.js$/', $uri)) {
\header('Content-Type: text/javascript');
}

echo \file_get_contents($uri);

return true;
Expand Down
Binary file modified screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f351c10

Please sign in to comment.