Last active
October 23, 2019 07:20
-
-
Save 1f7/b2d2846777e71f48e43a2ef7acc0a655 to your computer and use it in GitHub Desktop.
Slim 3 / Eloquent / Tracy Debugger
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
=== composer.json | |
"require": { | |
"tracy/tracy": "^2.3.*", | |
"illuminate/database": "5.2.*", | |
=== index.php | |
<?php | |
error_reporting(E_ALL); | |
ini_set('display_errors', 1); | |
use Tracy\Debugger; | |
/** | |
* Step 1: Require the Slim Framework using Composer's autoloader | |
* | |
* If you are not using Composer, you need to load Slim Framework with your own | |
* PSR-4 autoloader. | |
*/ | |
require __DIR__.'/../vendor/autoload.php'; | |
/** | |
* Step 2: Instantiate a Slim application | |
* | |
* This example instantiates a Slim application using | |
* its default settings. However, you will usually configure | |
* your Slim application now by passing an associative array | |
* of setting names and values into the application constructor. | |
*/ | |
$cfg = require __DIR__ . '/../app/config/Settings.php'; | |
$app = new Slim\App($cfg); | |
//http://discourse.slimframework.com/t/debugbar-for-slim3/158/7 | |
unset($app->getContainer()['errorHandler']); | |
Debugger::enable(Debugger::DEVELOPMENT); | |
//Debugger::enable(Debugger::PRODUCTION); | |
//https://github.com/nette/tracy/issues/154#issuecomment-219694817 | |
Debugger::dispatch();// from ver 2.4 | |
// Set up dependencies | |
require __DIR__ . '/../app/config/Dependencies.php'; | |
// Register the database connection with Eloquent | |
$capsule = new \Illuminate\Database\Capsule\Manager; | |
$capsule->addConnection($cfg['settings']['db']); | |
$capsule->setAsGlobal(); | |
$capsule->connection()->enableQueryLog();// enable log | |
$capsule->bootEloquent(); | |
// Register middleware | |
require __DIR__ . '/../app/config/Middleware.php'; | |
... | |
... | |
=== Middleware.php | |
$app->add(new Src\Middlewares\TracyDBMiddleware($capsule)); |
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
<?php | |
/** | |
* Copyright 2016 1f7@runetcms.ru | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
namespace Src\Middlewares; | |
use Tracy\Debugger; | |
use Src\Helpers\TracyDBpanel; | |
use Src\Helpers\TracyTwigpanel; | |
use Illuminate\Database\Capsule\Manager as DB; | |
class TracyDBMiddleware | |
{ | |
private $app; | |
private $slim_ver; | |
public function __construct($app) | |
{ | |
$this->app = $app; | |
$this->slim_ver = $app::VERSION; | |
} | |
public function __invoke($request, $response, $next) | |
{ | |
$res = $next($request, $response); | |
$d = DB::getQueryLog(); | |
$p = $this->app->getContainer()->get('twig_profile'); | |
$v = [ | |
'slim' => $this->slim_ver, | |
'twig' => \Twig_Environment::VERSION | |
]; | |
// uncomment if you want raw data | |
//Debugger::barDump($d, 'DB log'); | |
//Debugger::barDump($p, 'Twig log'); | |
Debugger::getBar()->addPanel(new TracyDBpanel($d, $v)); | |
Debugger::getBar()->addPanel(new TracyTwigpanel($p, $v)); | |
return $res; | |
} | |
} |
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
<?php | |
/** | |
* Copyright 2016 1f7@runetcms.ru | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
namespace Src\Helpers; | |
use Tracy\IBarPanel; | |
class TracyDBpanel implements IBarPanel | |
{ | |
private $count; | |
private $data; | |
private $time; | |
private $ver; | |
public function __construct($data = [], $ver = []) | |
{ | |
$this->data = $this->parse($data); | |
$this->count = count($data); | |
$this->ver = $ver; | |
} | |
protected function getHeader() | |
{ | |
return '<thead><tr><th><b>Count</b></th><th><b>Time, ms</b></th><th>Query / Bindings</th></tr></thead>'; | |
} | |
protected function getBaseRow() | |
{ | |
return '<tr><td>%s</td><td>%s</td><td>%s</td></tr>'; | |
} | |
public function getTab() | |
{ | |
return '<span title="DB Query Info"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" viewBox="0 0 284.207 284.207" style="enable-background:new 0 0 284.207 284.207;" xml:space="preserve" width="512px" height="512px"><path d="M239.604,45.447c0-25.909-41.916-45.447-97.5-45.447s-97.5,19.538-97.5,45.447v47.882c0,6.217,2.419,12.064,6.854,17.365 c-3.84,0.328-6.854,3.543-6.854,7.468v47.882c0,6.217,2.419,12.065,6.855,17.366c-3.84,0.328-6.855,3.543-6.855,7.468v47.881 c0,25.91,41.916,45.448,97.5,45.448s97.5-19.538,97.5-45.448v-47.881c0-3.925-3.016-7.14-6.855-7.468 c4.437-5.301,6.855-11.149,6.855-17.366v-47.882c0-3.925-3.015-7.14-6.855-7.468c4.436-5.301,6.855-11.148,6.855-17.365V45.447z M224.598,190.952c-0.121,14.354-35.358,30.373-82.494,30.373s-82.373-16.02-82.494-30.373 c16.977,12.544,46.938,20.539,82.494,20.539S207.621,203.496,224.598,190.952z M224.598,118.238 c-0.123,14.354-35.359,30.372-82.494,30.372s-82.371-16.019-82.494-30.372c16.977,12.543,46.938,20.538,82.494,20.538 S207.621,130.781,224.598,118.238z M142.104,15c47.218,0,82.5,16.075,82.5,30.447s-35.282,30.447-82.5,30.447 s-82.5-16.075-82.5-30.447S94.886,15,142.104,15z" fill="#005ccc"/></svg><span class="tracy-label">' . $this->count . ' / ' . $this->time . '</span> ms</span>'; | |
} | |
public function getPanel() | |
{ | |
return '<h1>Slim '.$this->ver['slim'].' / Eloquent ORM</h1><div class="tracy-inner"><p><table width="100%">' . $this->data . '<tr class="yes"><th><b>' . $this->count . '</b></th><th><b>' . $this->time . ' ms</b></th><th>Total</th></tr></table></p></div>'; | |
} | |
private function parse($data) | |
{ | |
$return = $this->getHeader(); | |
$time = $cnt = 0; | |
foreach ($data AS $var) { | |
$time += $var['time']; | |
$row = $this->getBaseRow(); | |
$bind = '<span class="tracy-dump-hash"><hr />'; | |
if (!empty($var['bindings'])) { | |
foreach ($var['bindings'] as $k => $v){ | |
$bind .= "[$k => $v]<br />"; | |
} | |
} | |
$return .= sprintf( | |
$row, | |
++$cnt, | |
$var['time'], | |
$var['query'].$bind.'</span>' | |
); | |
} | |
$this->time = $time; | |
return $return; | |
} | |
} |
Im sorry, I update gist from begin my app.php (index.php)
now i use only dev mode and debugger hardcoded.
maybe later I'll make config, but now do not need it
Thank you for your comments!!!
Gist increased to Slim3 Debug Bar package.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @1f7! – Maybe you have some hints for me for making this work in my app.
I added everything above and Tracy works. – But: I can Tracy see only with "Fatal Errors". Then there is the Tracy page and a/the panel.
→ So how can I see the panel (with the queries) on normal/working pages?
Additionally to your code I only added a config value "debug" with true in my settings.php and enabled Tracy in my public/index.php like this:
Thanks a lot in advance and best regards!
BTW: Came here from Slim forums …