Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,51 @@ example
```php
<?php
require '/path/to/vendor/autoload.php';
$config = [];

//This is needed to determine the route before middleware.
$config = [
'settings' => [
'determineRouteBeforeAppMiddleware' => true,
]
];
// if you want to capture ajax requests, set instance of StorageInterface implemented.
// $config['debugbar.storage'] = new \DebugBar\Storage\FileStorage('/path/to/storage');
$slim = new \Slim\Slim($config);
$app = new \Slim\App($config);

/*

To use the logger in the debug bar
1) register as a service
$container['log'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], Monolog\Logger::DEBUG));
return $logger;
};
2) Configure a new array $config = ['logger' => 'logger'];
- KEY IS REQUIRED, MUST BE LOGGER
- Value is the service of the logger (Ex.: $container['log'] = log)
3) Change $debugbar = new \Slim\Middleware\DebugBar(null, $config);
*/
$debugbar = new \Slim\Middleware\DebugBar();
// you can add custom collectors
// $debugbar->addCollector(new MyCustomCollector());
// or use custom debugbar
// $debugbar->setDebugBar(new MyCustomDebugBar());
$slim->add($debugbar);
$slim->get('/', function()
$app->add($debugbar);

$routes = new \Slim\Routes\DebugBarRoutes($app);
$routes->registerRoutes();

$app->get('/', function()
{
echo 'Hello world!';
});
$slim->run();
$app->run();
```

### Notice
* Please use real httpd (apache, nginx etc...)
- PHP builtin server does not supported.
* Available storage for ajax capturing
- Filesystem, PDO and Redis
- for more information, please refer to [the official document](http://phpdebugbar.com/docs/storage.html).
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["slim", "middleware", "debugbar"],
"require": {
"php": ">=5.4.0",
"slim/slim": "2.*",
"slim/slim": "3.*",
"maximebf/debugbar": ">=1.0.0"
},
"require-dev": {
Expand All @@ -14,6 +14,7 @@
"psr-0": {
"Slim\\Middleware\\": "src/",
"Slim\\Views\\": "src/",
"Slim\\Routes\\": "src/",
"DebugBar\\": "src/"
}
},
Expand Down
18 changes: 10 additions & 8 deletions src/DebugBar/DataCollector/SlimEnvCollector.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?php namespace DebugBar\DataCollector;
<?php

use Slim\Slim;
namespace DebugBar\DataCollector;

use Slim\App;

class SlimEnvCollector extends DataCollector implements Renderable
{
/**
* @var \Slim\Slim
* @var App $app
*/
protected $slim;
protected $app;

public function __construct(Slim $slim)
public function __construct(App $app)
{
$this->slim = $slim;
$this->app = $app;
}

public function collect()
{
return $this->slim->getMode();
return 'Versions';
}

public function getName()
Expand All @@ -26,7 +28,7 @@ public function getName()

public function getWidgets()
{
$slim_version = Slim::VERSION;
$slim_version = App::VERSION;
$php_version = PHP_VERSION;
return [
'mode' => [
Expand Down
4 changes: 3 additions & 1 deletion src/DebugBar/DataCollector/SlimLogCollector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace DebugBar\DataCollector;
<?php

namespace DebugBar\DataCollector;

use DebugBar\Bridge\SlimCollector;

Expand Down
11 changes: 6 additions & 5 deletions src/DebugBar/DataCollector/SlimResponseCollector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace DebugBar\DataCollector;
<?php

namespace DebugBar\DataCollector;

use Slim\Http\Response;

Expand All @@ -22,10 +24,9 @@ public function __construct(Response $response)
function collect()
{
return [
'content-type' => $this->response->header('Content-Type'),
'status_code' => $this->response->getStatus(),
'headers' => $this->getDataFormatter()->formatVar($this->response->headers->all()),
'cookies' => $this->getDataFormatter()->formatVar($this->response->cookies->all()),
'content-type' => $this->response->getHeader('Content-Type'),
'status_code' => $this->response->getStatusCode(),
'headers' => $this->getDataFormatter()->formatVar($this->response->getHeaders())
];
}

Expand Down
55 changes: 37 additions & 18 deletions src/DebugBar/DataCollector/SlimRouteCollector.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<?php namespace DebugBar\DataCollector;
<?php

use Slim\Slim;
namespace DebugBar\DataCollector;

use Slim\App;
use Slim\Http\Request;
use Slim\Route;

class SlimRouteCollector extends ConfigCollector
{
/**
* @var \Slim\Slim
* @var Request
*/
protected $slim;
protected $request;

/** @var Route $route */
protected $route;

/**
* @param Slim $slim
* SlimRouteCollector constructor.
* @param Request $request
* @param Route|null $route
*/
public function __construct(Slim $slim)
public function __construct(Request $request, Route $route = null)
{
$this->slim = $slim;
$this->request = $request;
$this->route = $route;
$this->setData($this->getRouteInfo());
}

Expand All @@ -25,18 +35,27 @@ public function getName()

public function getRouteInfo()
{
// if slim.after.router fired, route is not null
$route = $this->slim->router->getCurrentRoute();
$method = $this->slim->request->getMethod();
$path = $this->slim->request->getPathInfo();
$route = $this->route;
$method = $this->request->getMethod();
$path = $this->request->getUri()->getPath();
$uri = $method . ' ' . $path;
return [
'uri' => $uri,
'pattern' => $route->getPattern(),
'params' => $route->getParams() ?: '-',
'name' => $route->getName() ?: '-',
'conditions' => $route->getConditions() ?: '-',
];

if ($route) {
$data = [
'id' => $route->getIdentifier() ?: '-',
'uri' => $uri,
'pattern' => $route->getPattern(),
'arguments' => $route->getArguments() ?: '-',
'name' => $route->getName() ?: '-',
'groups' => $route->getGroups() ?: '-',
];
} else {
$data = [
'route' => 'None Found'
];
}

return array_merge($data, ['uri' => $uri]);
}

public function getWidgets()
Expand Down
4 changes: 3 additions & 1 deletion src/DebugBar/DataCollector/SlimViewCollector.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace DebugBar\DataCollector;
<?php

namespace DebugBar\DataCollector;

class SlimViewCollector extends ConfigCollector
{
Expand Down
54 changes: 38 additions & 16 deletions src/DebugBar/SlimDebugBar.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
<?php namespace DebugBar;
<?php

use Slim\Slim;
namespace DebugBar;

use DebugBar\Bridge\MonologCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use Slim\App;
use DebugBar\DataCollector\ConfigCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DataCollector\TimeDataCollector;
use DebugBar\DataCollector\SlimEnvCollector;
use DebugBar\DataCollector\SlimLogCollector;
use DebugBar\DataCollector\SlimResponseCollector;
use DebugBar\DataCollector\SlimRouteCollector;
use DebugBar\DataCollector\SlimViewCollector;

class SlimDebugBar extends DebugBar
{
public function __construct()
{
$this->addCollector(new TimeDataCollector());
$collector = $this->getCollector('time');
$collector->startMeasure('application', 'Application');

$this->addCollector(new MessagesCollector());
$this->addCollector(new RequestDataCollector());
$this->addCollector(new PhpInfoCollector());
$this->addCollector(new MemoryCollector());
}

public function initCollectors(Slim $slim)
public function initCollectorsBeforeRoute(App $app, $config)
{
if ($this->isLoggerEnabled($config)) {
$logger = $app->getContainer()->get($config['logger']);
$this->addCollector(new MonologCollector($logger));
}

$this->addCollector(new SlimEnvCollector($app));
}

public function initCollectorsAfterRoute(App $app, $response, $request, $route = null)
{
$container = $app->getContainer();

$setting = $this->prepareRenderData($container->get('settings')->all());

$this->addCollector(new ConfigCollector($setting));

$this->addCollector(new SlimResponseCollector($response));

$this->addCollector(new SlimRouteCollector($request, $route));

}

protected function isLoggerEnabled($config)
{
$this->addCollector(new SlimLogCollector($slim));
$this->addCollector(new SlimEnvCollector($slim));
$slim->hook('slim.after.router', function() use ($slim)
{
$setting = $this->prepareRenderData($slim->container['settings']);
$data = $this->prepareRenderData($slim->view->all());
$this->addCollector(new SlimResponseCollector($slim->response));
$this->addCollector(new ConfigCollector($setting));
$this->addCollector(new SlimViewCollector($data));
$this->addCollector(new SlimRouteCollector($slim));
});
return array_key_exists('logger', $config);
}

protected function prepareRenderData(array $data = [])
Expand Down
16 changes: 9 additions & 7 deletions src/DebugBar/SlimHttpDriver.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php namespace DebugBar;
<?php

use Slim\Slim;
namespace DebugBar;

use Slim\Http\Response;

class SlimHttpDriver extends PhpHttpDriver
{
/**
* @var Slim
* @var Response
*/
protected $slim;
protected $response;

public function __construct(Slim $slim)
public function __construct(Response $response)
{
$this->slim = $slim;
$this->response = $response;
}

public function setHeaders(array $headers)
{
foreach ($headers as $key => $val) {
$this->slim->response->header($key, $val);
$this->response->withHeader($key, $val);
}
}
}
Loading