Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbar updates #1571

Merged
merged 11 commits into from
Dec 3, 2018
24 changes: 0 additions & 24 deletions application/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,30 +264,6 @@ class App extends BaseConfig
*/
public $CSPEnabled = false;

/*
|--------------------------------------------------------------------------
| Debug Toolbar
|--------------------------------------------------------------------------
| The Debug Toolbar provides a way to see information about the performance
| and state of your application during that page display. By default it will
| NOT be displayed under production environments, and will only display if
| CI_DEBUG is true, since if it's not, there's not much to display anyway.
|
| toolbarMaxHistory = Number of history files, 0 for none or -1 for unlimited
|
*/
public $toolbarCollectors = [
'CodeIgniter\Debug\Toolbar\Collectors\Timers',
'CodeIgniter\Debug\Toolbar\Collectors\Database',
'CodeIgniter\Debug\Toolbar\Collectors\Logs',
'CodeIgniter\Debug\Toolbar\Collectors\Views',
// 'CodeIgniter\Debug\Toolbar\Collectors\Cache',
'CodeIgniter\Debug\Toolbar\Collectors\Files',
'CodeIgniter\Debug\Toolbar\Collectors\Routes',
'CodeIgniter\Debug\Toolbar\Collectors\Events',
];
public $toolbarMaxHistory = 20;

/*
|--------------------------------------------------------------------------
| Application Salt
Expand Down
5 changes: 3 additions & 2 deletions application/Config/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
{
Events::on('DBQuery', 'CodeIgniter\Debug\Toolbar\Collectors\Database::collect');

// Handles the display of the toolbar itself. MUST remain here for toolbar to be displayed.
Events::on('pre_system', 'CodeIgniter\Debug\Toolbar::eventHandler');
Events::on('pre_system', function () {
Services::toolbar()->respond();
});
}
31 changes: 31 additions & 0 deletions application/Config/Toolbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class Toolbar extends BaseConfig
{
/*
|--------------------------------------------------------------------------
| Debug Toolbar
|--------------------------------------------------------------------------
| The Debug Toolbar provides a way to see information about the performance
| and state of your application during that page display. By default it will
| NOT be displayed under production environments, and will only display if
| CI_DEBUG is true, since if it's not, there's not much to display anyway.
|
| toolbarMaxHistory = Number of history files, 0 for none or -1 for unlimited
|
*/
public $collectors = [
\CodeIgniter\Debug\Toolbar\Collectors\Timers::class,
\CodeIgniter\Debug\Toolbar\Collectors\Database::class,
\CodeIgniter\Debug\Toolbar\Collectors\Logs::class,
\CodeIgniter\Debug\Toolbar\Collectors\Views::class,
// \CodeIgniter\Debug\Toolbar\Collectors\Cache::class,
\CodeIgniter\Debug\Toolbar\Collectors\Files::class,
\CodeIgniter\Debug\Toolbar\Collectors\Routes::class,
\CodeIgniter\Debug\Toolbar\Collectors\Events::class,
];
public $maxHistory = 20;
public $viewsPath = BASEPATH . 'Debug/Toolbar/Views/';
}
55 changes: 1 addition & 54 deletions application/Filters/DebugToolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Config\App;
use Config\Services;

class DebugToolbar implements FilterInterface
Expand Down Expand Up @@ -32,59 +31,7 @@ public function before(RequestInterface $request)
*/
public function after(RequestInterface $request, ResponseInterface $response)
{
if (! is_cli() && CI_DEBUG)
{
global $app;

$toolbar = Services::toolbar(config(App::class));
$stats = $app->getPerformanceStats();
$data = $toolbar->run(
$stats['startTime'],
$stats['totalTime'],
$request,
$response
);

helper('filesystem');

// Updated to time() so we can get history
$time = time();

if (! is_dir(WRITEPATH . 'debugbar'))
{
mkdir(WRITEPATH . 'debugbar', 0777);
}

write_file(WRITEPATH . 'debugbar/' . 'debugbar_' . $time, $data, 'w+');

$format = $response->getHeaderLine('content-type');

// Non-HTML formats should not include the debugbar
// then we send headers saying where to find the debug data
// for this response
if ($request->isAJAX() || strpos($format, 'html') === false)
{
return $response->setHeader('Debugbar-Time', (string)$time)
->setHeader('Debugbar-Link', site_url("?debugbar_time={$time}"))
->getBody();
}

$script = PHP_EOL
. '<script type="text/javascript" {csp-script-nonce} id="debugbar_loader" '
. 'data-time="' . $time . '" '
. 'src="' . rtrim(site_url(), '/') . '?debugbar"></script>'
. '<script type="text/javascript" {csp-script-nonce} id="debugbar_dynamic_script"></script>'
. '<style type="text/css" {csp-style-nonce} id="debugbar_dynamic_style"></style>'
. PHP_EOL;

if (strpos($response->getBody(), '</body>') !== false)
{
return $response->setBody(str_replace('</body>', $script . '</body>',
$response->getBody()));
}

return $response->appendBody($script);
}
Services::toolbar()->prepare();
}

//--------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,12 @@ public static function timer($getShared = true)
//--------------------------------------------------------------------

/**
* @param \Config\App $config
* @param boolean $getShared
* @param \Config\Toolbar $config
* @param boolean $getShared
*
* @return \CodeIgniter\Debug\Toolbar
*/
public static function toolbar(\Config\App $config = null, bool $getShared = true)
public static function toolbar(\Config\Toolbar $config = null, bool $getShared = true)
{
if ($getShared)
{
Expand All @@ -756,7 +756,7 @@ public static function toolbar(\Config\App $config = null, bool $getShared = tru

if (! is_object($config))
{
$config = config(App::class);
$config = config(\Config\Toolbar::class);
}

return new \CodeIgniter\Debug\Toolbar($config);
Expand Down
Loading