mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-06-27 21:23:26 +02:00
added new storage adapters
This commit is contained in:
@ -22,6 +22,7 @@
|
|||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"kriswallsmith/assetic": "The best way to manage assets",
|
"kriswallsmith/assetic": "The best way to manage assets",
|
||||||
"monolog/monolog": "Log using Monolog"
|
"monolog/monolog": "Log using Monolog",
|
||||||
|
"predis/predis": "Redis storage"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,28 @@ Each time `DebugBar::collect()` is called, the data will be persisted.
|
|||||||
|
|
||||||
## Available storage
|
## Available storage
|
||||||
|
|
||||||
Only file storage is provided at the moment. It will collected data as json files
|
### File
|
||||||
under the specified directory (which as to be writable).
|
|
||||||
|
It will collect data as json files under the specified directory
|
||||||
|
(which has to be writable).
|
||||||
|
|
||||||
$storage = new DebugBar\Storage\FileStorage($directory);
|
$storage = new DebugBar\Storage\FileStorage($directory);
|
||||||
|
|
||||||
|
### Redis
|
||||||
|
|
||||||
|
Stores data inside a Redis hash. Uses [Predis](http://github.com/nrk/predis).
|
||||||
|
|
||||||
|
$storage = new DebugBar\Storage\RedisStorage($client);
|
||||||
|
|
||||||
|
### PDO
|
||||||
|
|
||||||
|
Stores data inside a database.
|
||||||
|
|
||||||
|
$storage = new DebugBar\Storage\PdoStorage($pdo);
|
||||||
|
|
||||||
|
The table name can be changed using the second argument and sql queries
|
||||||
|
can be changed using `setSqlQueries()`.
|
||||||
|
|
||||||
## Creating your own storage
|
## Creating your own storage
|
||||||
|
|
||||||
You can easily create your own storage handler by implementing the
|
You can easily create your own storage handler by implementing the
|
||||||
|
129
src/DebugBar/Storage/PdoStorage.php
Normal file
129
src/DebugBar/Storage/PdoStorage.php
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of the DebugBar package.
|
||||||
|
*
|
||||||
|
* (c) 2013 Maxime Bouroumeau-Fuseau
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DebugBar\Storage;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores collected data into a database using PDO
|
||||||
|
*/
|
||||||
|
class PdoStorage implements StorageInterface
|
||||||
|
{
|
||||||
|
protected $pdo;
|
||||||
|
|
||||||
|
protected $tableName;
|
||||||
|
|
||||||
|
protected $sqlQueries = array(
|
||||||
|
'save' => "INSERT INTO %tablename% (id, data, meta_utime, meta_datetime, meta_uri, meta_ip) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
|
'get' => "SELECT data FROM %tablename% WHERE id = ?",
|
||||||
|
'find' => "SELECT data FROM %tablename% %where% LIMIT %limit% OFFSET %offset%",
|
||||||
|
'clear' => "DELETE FROM %tablename%"
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dirname Directories where to store files
|
||||||
|
* @param array $sqlQueries
|
||||||
|
*/
|
||||||
|
public function __construct(PDO $pdo, $tableName = 'phpdebugbar', array $sqlQueries = array())
|
||||||
|
{
|
||||||
|
$this->pdo = $pdo;
|
||||||
|
$this->tableName = $tableName;
|
||||||
|
$this->setSqlQueries($sqlQueries);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the sql queries to be used
|
||||||
|
*
|
||||||
|
* @param array $queries
|
||||||
|
*/
|
||||||
|
public function setSqlQueries(array $queries)
|
||||||
|
{
|
||||||
|
$this->sqlQueries = array_merge($this->sqlQueries, $queries);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function save($id, $data)
|
||||||
|
{
|
||||||
|
$sql = $this->getSqlQuery('save');
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$meta = $data['__meta'];
|
||||||
|
$stmt->execute(array($id, serialize($data), $meta['utime'], $meta['datetime'], $meta['uri'], $meta['ip']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function get($id)
|
||||||
|
{
|
||||||
|
$sql = $this->getSqlQuery('get');
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute(array($id));
|
||||||
|
if (($data = $stmt->fetchColumn(0)) !== false) {
|
||||||
|
return unserialize($data);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function find(array $filters = array(), $max = 20, $offset = 0)
|
||||||
|
{
|
||||||
|
$where = array();
|
||||||
|
$params = array();
|
||||||
|
foreach ($filters as $key => $value) {
|
||||||
|
$where[] = "meta_$key = ?";
|
||||||
|
$params[] = $value;
|
||||||
|
}
|
||||||
|
if (count($where)) {
|
||||||
|
$where = " WHERE " . implode(' AND ', $where);
|
||||||
|
} else {
|
||||||
|
$where = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = $this->getSqlQuery('find', array(
|
||||||
|
'where' => $where,
|
||||||
|
'offset' => $offset,
|
||||||
|
'limit' => $max
|
||||||
|
));
|
||||||
|
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute($params);
|
||||||
|
|
||||||
|
$results = array();
|
||||||
|
foreach ($stmt->fetchAll() as $row) {
|
||||||
|
$data = unserialize($row['data']);
|
||||||
|
$results[] = $data['__meta'];
|
||||||
|
unset($data);
|
||||||
|
}
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->pdo->exec($this->getSqlQuery('clear'));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSqlQuery($name, array $vars = array())
|
||||||
|
{
|
||||||
|
$sql = $this->sqlQueries[$name];
|
||||||
|
$vars = array_merge(array('tablename' => $this->tableName), $vars);
|
||||||
|
foreach ($vars as $k => $v) {
|
||||||
|
$sql = str_replace("%$k%", $v, $sql);
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
}
|
73
src/DebugBar/Storage/RedisStorage.php
Normal file
73
src/DebugBar/Storage/RedisStorage.php
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of the DebugBar package.
|
||||||
|
*
|
||||||
|
* (c) 2013 Maxime Bouroumeau-Fuseau
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace DebugBar\Storage;
|
||||||
|
|
||||||
|
use Predis\Client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores collected data into Redis
|
||||||
|
*/
|
||||||
|
class RedisStorage implements StorageInterface
|
||||||
|
{
|
||||||
|
protected $redis;
|
||||||
|
|
||||||
|
protected $hash;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $dirname Directories where to store files
|
||||||
|
*/
|
||||||
|
public function __construct(Client $redis, $hash = 'phpdebugbar')
|
||||||
|
{
|
||||||
|
$this->redis = $redis;
|
||||||
|
$this->hash = $hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function save($id, $data)
|
||||||
|
{
|
||||||
|
$this->redis->hset($this->hash, $id, serialize($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function get($id)
|
||||||
|
{
|
||||||
|
return unserialize($this->redis->hget($this->hash, $id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function find(array $filters = array(), $max = 20, $offset = 0)
|
||||||
|
{
|
||||||
|
$results = array();
|
||||||
|
foreach ($this->redis->hgetall($this->hash) as $id => $data) {
|
||||||
|
if ($data = unserialize($data)) {
|
||||||
|
$meta = $data['__meta'];
|
||||||
|
if (array_keys(array_intersect($meta, $filters)) == array_keys($filters)) {
|
||||||
|
$results[] = $meta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array_slice($results, $offset, $max);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->redis->del($this->hash);
|
||||||
|
}
|
||||||
|
}
|
15
src/DebugBar/Storage/pdo_storage_schema.sql
Normal file
15
src/DebugBar/Storage/pdo_storage_schema.sql
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
CREATE TABLE phpdebugbar (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
data TEXT,
|
||||||
|
meta_utime TEXT,
|
||||||
|
meta_datetime TEXT,
|
||||||
|
meta_uri TEXT,
|
||||||
|
meta_ip TEXT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_debugbar_id ON phpdebugbar (id);
|
||||||
|
CREATE INDEX idx_debugbar_meta_utime ON phpdebugbar (meta_utime);
|
||||||
|
CREATE INDEX idx_debugbar_meta_datetime ON phpdebugbar (meta_datetime);
|
||||||
|
CREATE INDEX idx_debugbar_meta_uri ON phpdebugbar (meta_uri);
|
||||||
|
CREATE INDEX idx_debugbar_meta_ip ON phpdebugbar (meta_ip);
|
@ -1,4 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
error_reporting(E_ALL | E_STRICT);
|
||||||
|
|
||||||
$loader = require(dirname(__DIR__) . '/vendor/autoload.php');
|
$loader = require(dirname(__DIR__) . '/vendor/autoload.php');
|
||||||
$loader->add('DebugBar\Tests', __DIR__);
|
$loader->add('DebugBar\Tests', __DIR__);
|
||||||
|
Reference in New Issue
Block a user