initial commit
This commit is contained in:
90
exp/Plugin/DebugKit/View/Helper/DebugTimerHelper.php
Normal file
90
exp/Plugin/DebugKit/View/Helper/DebugTimerHelper.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since DebugKit 2.1
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('DebugTimer', 'DebugKit.Lib');
|
||||
App::uses('DebugMemory', 'DebugKit.Lib');
|
||||
App::uses('Helper', 'View');
|
||||
|
||||
/**
|
||||
* Class DebugTimerHelper
|
||||
*
|
||||
* Tracks time and memory usage while rendering view.
|
||||
*
|
||||
*/
|
||||
class DebugTimerHelper extends Helper {
|
||||
|
||||
/**
|
||||
* Set to true when rendering is complete.
|
||||
* Used to not add timers for rendering the toolbar.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_renderComplete = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param View $View
|
||||
* @param array $settings
|
||||
*/
|
||||
public function __construct(View $View, $settings = array()) {
|
||||
parent::__construct($View, $settings);
|
||||
DebugTimer::start(
|
||||
'viewRender',
|
||||
__d('debug_kit', 'Rendering View')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a timer point before rendering a file.
|
||||
*
|
||||
* @param string $viewFile The view being rendered
|
||||
*/
|
||||
public function beforeRenderFile($viewFile) {
|
||||
if ($this->_renderComplete) {
|
||||
return;
|
||||
}
|
||||
DebugTimer::start(
|
||||
'render_' . basename($viewFile),
|
||||
__d('debug_kit', 'Rendering %s',
|
||||
Debugger::trimPath($viewFile))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the timer point before rendering a file.
|
||||
*
|
||||
* @param string $viewFile The view being rendered
|
||||
* @param string $content The contents of the view.
|
||||
*/
|
||||
public function afterRenderFile($viewFile, $content) {
|
||||
if ($this->_renderComplete) {
|
||||
return;
|
||||
}
|
||||
DebugTimer::stop('render_' . basename($viewFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop timers for rendering.
|
||||
*
|
||||
* @param string $layoutFile
|
||||
*/
|
||||
public function afterLayout($layoutFile) {
|
||||
DebugTimer::stop('viewRender');
|
||||
DebugTimer::stop('controllerRender');
|
||||
DebugMemory::record(__d('debug_kit', 'View render complete'));
|
||||
$this->_renderComplete = true;
|
||||
}
|
||||
|
||||
}
|
||||
106
exp/Plugin/DebugKit/View/Helper/FirePhpToolbarHelper.php
Normal file
106
exp/Plugin/DebugKit/View/Helper/FirePhpToolbarHelper.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since DebugKit 0.1
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('ToolbarHelper', 'DebugKit.View/Helper');
|
||||
App::uses('FireCake', 'DebugKit.Lib');
|
||||
|
||||
/**
|
||||
* FirePHP Toolbar Helper
|
||||
*
|
||||
* Injects the toolbar elements into non-HTML layouts via FireCake.
|
||||
*
|
||||
*/
|
||||
class FirePhpToolbarHelper extends ToolbarHelper {
|
||||
|
||||
/**
|
||||
* settings property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array('format' => 'firePHP', 'forceEnable' => false);
|
||||
|
||||
/**
|
||||
* send method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function send() {
|
||||
$view = $this->_View;
|
||||
$view->element('debug_toolbar', array('disableTimer' => true), array('plugin' => 'DebugKit'));
|
||||
}
|
||||
|
||||
/**
|
||||
* makeNeatArray.
|
||||
*
|
||||
* wraps FireCake::dump() allowing panel elements to continue functioning
|
||||
*
|
||||
* @param string $values
|
||||
* @return void
|
||||
*/
|
||||
public function makeNeatArray($values) {
|
||||
FireCake::info($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a simple message
|
||||
*
|
||||
* @param string $label Label of message
|
||||
* @param string $message Message content
|
||||
* @return void
|
||||
*/
|
||||
public function message($label, $message) {
|
||||
FireCake::log($message, $label);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a table with FireCake
|
||||
*
|
||||
* @param array $rows Rows to print
|
||||
* @param array $headers Headers for table
|
||||
* @param array $options Additional options and params
|
||||
* @return void
|
||||
*/
|
||||
public function table($rows, $headers, $options = array()) {
|
||||
$title = $headers[0];
|
||||
if (isset($options['title'])) {
|
||||
$title = $options['title'];
|
||||
}
|
||||
foreach ($rows as $i => $row) {
|
||||
$rows[$i] = array_values($row);
|
||||
}
|
||||
array_unshift($rows, $headers);
|
||||
FireCake::table($title, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a panel which is a 'Group' in FirePHP
|
||||
*
|
||||
* @param $title
|
||||
* @param $anchor
|
||||
* @return void
|
||||
*/
|
||||
public function panelStart($title, $anchor) {
|
||||
FireCake::group($title);
|
||||
}
|
||||
|
||||
/**
|
||||
* End a panel (Group)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function panelEnd() {
|
||||
FireCake::groupEnd();
|
||||
}
|
||||
|
||||
}
|
||||
238
exp/Plugin/DebugKit/View/Helper/HtmlToolbarHelper.php
Normal file
238
exp/Plugin/DebugKit/View/Helper/HtmlToolbarHelper.php
Normal file
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since DebugKit 0.1
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('ToolbarHelper', 'DebugKit.View/Helper');
|
||||
App::uses('Security', 'Utility');
|
||||
|
||||
/**
|
||||
* Html Toolbar Helper
|
||||
*
|
||||
* Injects the toolbar elements into HTML layouts.
|
||||
* Contains helper methods for
|
||||
*
|
||||
* @since DebugKit 0.1
|
||||
*/
|
||||
class HtmlToolbarHelper extends ToolbarHelper {
|
||||
|
||||
/**
|
||||
* helpers property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $helpers = array('Html', 'Form');
|
||||
|
||||
/**
|
||||
* settings property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array('format' => 'html', 'forceEnable' => false);
|
||||
|
||||
/**
|
||||
* Recursively goes through an array and makes neat HTML out of it.
|
||||
*
|
||||
* @param mixed $values Array to make pretty.
|
||||
* @param integer $openDepth Depth to add open class
|
||||
* @param integer $currentDepth current depth.
|
||||
* @param boolean $doubleEncode
|
||||
* @return string
|
||||
*/
|
||||
public function makeNeatArray($values, $openDepth = 0, $currentDepth = 0, $doubleEncode = false) {
|
||||
static $printedObjects = null;
|
||||
if ($currentDepth === 0) {
|
||||
$printedObjects = new SplObjectStorage();
|
||||
}
|
||||
$className = "neat-array depth-$currentDepth";
|
||||
if ($openDepth > $currentDepth) {
|
||||
$className .= ' expanded';
|
||||
}
|
||||
$nextDepth = $currentDepth + 1;
|
||||
$out = "<ul class=\"$className\">";
|
||||
if (!is_array($values)) {
|
||||
if (is_bool($values)) {
|
||||
$values = array($values);
|
||||
}
|
||||
if ($values === null) {
|
||||
$values = array(null);
|
||||
}
|
||||
}
|
||||
if (empty($values)) {
|
||||
$values[] = '(empty)';
|
||||
}
|
||||
foreach ($values as $key => $value) {
|
||||
$out .= '<li><strong>' . h($key, $doubleEncode) . '</strong>';
|
||||
if (is_array($value) && count($value) > 0) {
|
||||
$out .= '(array)';
|
||||
} elseif (is_object($value)) {
|
||||
$out .= '(object)';
|
||||
}
|
||||
if ($value === null) {
|
||||
$value = '(null)';
|
||||
}
|
||||
if ($value === false) {
|
||||
$value = '(false)';
|
||||
}
|
||||
if ($value === true) {
|
||||
$value = '(true)';
|
||||
}
|
||||
if (empty($value) && $value != 0) {
|
||||
$value = '(empty)';
|
||||
}
|
||||
if ($value instanceof Closure) {
|
||||
$value = 'function';
|
||||
}
|
||||
|
||||
$isObject = is_object($value);
|
||||
if ($isObject && $printedObjects->contains($value)) {
|
||||
$isObject = false;
|
||||
$value = ' - recursion';
|
||||
}
|
||||
|
||||
if ($isObject) {
|
||||
$printedObjects->attach($value);
|
||||
}
|
||||
|
||||
if (
|
||||
(
|
||||
$value instanceof ArrayAccess ||
|
||||
$value instanceof Iterator ||
|
||||
is_array($value) ||
|
||||
$isObject
|
||||
) && !empty($value)
|
||||
) {
|
||||
$out .= $this->makeNeatArray($value, $openDepth, $nextDepth, $doubleEncode);
|
||||
} else {
|
||||
$out .= h($value, $doubleEncode);
|
||||
}
|
||||
$out .= '</li>';
|
||||
}
|
||||
$out .= '</ul>';
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTML message
|
||||
*
|
||||
* @param string $label label content
|
||||
* @param string $message message content
|
||||
* @return string
|
||||
*/
|
||||
public function message($label, $message) {
|
||||
return sprintf('<p><strong>%s</strong> %s</p>', $label, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a panel.
|
||||
* Make a link and anchor.
|
||||
*
|
||||
* @param $title
|
||||
* @param $anchor
|
||||
* @return string
|
||||
*/
|
||||
public function panelStart($title, $anchor) {
|
||||
$link = $this->Html->link($title, '#' . $anchor);
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a table.
|
||||
*
|
||||
* @param array $rows Rows to make.
|
||||
* @param array $headers Optional header row.
|
||||
* @return string
|
||||
*/
|
||||
public function table($rows, $headers = array()) {
|
||||
$out = '<table class="debug-table">';
|
||||
if (!empty($headers)) {
|
||||
$out .= $this->Html->tableHeaders($headers);
|
||||
}
|
||||
$out .= $this->Html->tableCells($rows, array('class' => 'odd'), array('class' => 'even'), false, false);
|
||||
$out .= '</table>';
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function send() {
|
||||
if (!$this->settings['forceEnable'] && Configure::read('debug') == 0) {
|
||||
return;
|
||||
}
|
||||
$view = $this->_View;
|
||||
$head = '';
|
||||
if (isset($view->viewVars['debugToolbarCss']) && !empty($view->viewVars['debugToolbarCss'])) {
|
||||
$head .= $this->Html->css($view->viewVars['debugToolbarCss']);
|
||||
}
|
||||
|
||||
$js = sprintf('window.DEBUGKIT_JQUERY_URL = "%s";', $this->webroot('/debug_kit/js/jquery.js'));
|
||||
$head .= $this->Html->scriptBlock($js);
|
||||
|
||||
if (isset($view->viewVars['debugToolbarJavascript'])) {
|
||||
foreach ($view->viewVars['debugToolbarJavascript'] as $script) {
|
||||
if ($script) {
|
||||
$head .= $this->Html->script($script);
|
||||
}
|
||||
}
|
||||
}
|
||||
$search = '</head>';
|
||||
$pos = strpos($view->output, $search);
|
||||
if ($pos !== false) {
|
||||
$view->output = substr_replace($view->output, $head . "\n</head>", $pos, strlen($search));
|
||||
}
|
||||
$toolbar = $view->element('debug_toolbar', array('disableTimer' => true), array('plugin' => 'DebugKit'));
|
||||
$search = '</body>';
|
||||
$pos = strrpos($view->output, $search);
|
||||
if ($pos !== false) {
|
||||
$view->output = substr_replace($view->output, $toolbar . "\n</body>", $pos, strlen($search));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a SQL explain link for a given query
|
||||
*
|
||||
* @param string $sql SQL query string you want an explain link for.
|
||||
* @param $connection
|
||||
* @return string Rendered Html link or '' if the query is not a select/describe
|
||||
*/
|
||||
public function explainLink($sql, $connection) {
|
||||
if (!preg_match('/^[\s()]*SELECT/i', $sql)) {
|
||||
return '';
|
||||
}
|
||||
$sql = str_replace(array("\n", "\t"), ' ', $sql);
|
||||
$hash = Security::hash($sql . $connection, 'sha1', true);
|
||||
$url = array(
|
||||
'plugin' => 'debug_kit',
|
||||
'controller' => 'toolbar_access',
|
||||
'action' => 'sql_explain'
|
||||
);
|
||||
foreach (Router::prefixes() as $prefix) {
|
||||
$url[$prefix] = false;
|
||||
}
|
||||
$this->explainLinkUid = (isset($this->explainLinkUid) ? $this->explainLinkUid + 1 : 0);
|
||||
$uid = $this->explainLinkUid . '_' . rand(0, 10000);
|
||||
$form = $this->Form->create('log', array('url' => $url, 'id' => "logForm{$uid}"));
|
||||
$form .= $this->Form->hidden('log.ds', array('id' => "logDs{$uid}", 'value' => $connection));
|
||||
$form .= $this->Form->hidden('log.sql', array('id' => "logSql{$uid}", 'value' => $sql));
|
||||
$form .= $this->Form->hidden('log.hash', array('id' => "logHash{$uid}", 'value' => $hash));
|
||||
$form .= $this->Form->submit(__d('debug_kit', 'Explain'), array(
|
||||
'div' => false,
|
||||
'class' => 'sql-explain-link'
|
||||
));
|
||||
$form .= $this->Form->end();
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
88
exp/Plugin/DebugKit/View/Helper/SimpleGraphHelper.php
Normal file
88
exp/Plugin/DebugKit/View/Helper/SimpleGraphHelper.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since DebugKit 1.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('AppHelper', 'View/Helper');
|
||||
App::uses('HtmlHelper', 'View/Helper');
|
||||
|
||||
/**
|
||||
* Class SimpleGraphHelper
|
||||
*
|
||||
* Allows creation and display of extremely simple graphing elements
|
||||
*
|
||||
* @since DebugKit 1.0
|
||||
*/
|
||||
class SimpleGraphHelper extends AppHelper {
|
||||
|
||||
/**
|
||||
* Helpers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $helpers = array('Html');
|
||||
|
||||
/**
|
||||
* Default settings to be applied to each Simple Graph
|
||||
*
|
||||
* Allowed options:
|
||||
*
|
||||
* - max => (int) Maximum value in the graphs
|
||||
* - width => (int)
|
||||
* - valueType => string (value, percentage)
|
||||
* - style => array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_defaultSettings = array(
|
||||
'max' => 100,
|
||||
'width' => 350,
|
||||
'valueType' => 'value',
|
||||
);
|
||||
|
||||
/**
|
||||
* bar method
|
||||
*
|
||||
* @param $value Value to be graphed
|
||||
* @param $offset how much indentation
|
||||
* @param array|\Graph $options Graph options
|
||||
* @return string Html graph
|
||||
*/
|
||||
public function bar($value, $offset, $options = array()) {
|
||||
$settings = array_merge($this->_defaultSettings, $options);
|
||||
extract($settings);
|
||||
|
||||
$graphValue = ($value / $max) * $width;
|
||||
$graphValue = max(round($graphValue), 1);
|
||||
|
||||
if ($valueType === 'percentage') {
|
||||
$graphOffset = 0;
|
||||
} else {
|
||||
$graphOffset = ($offset / $max) * $width;
|
||||
$graphOffset = round($graphOffset);
|
||||
}
|
||||
return $this->Html->div(
|
||||
'debug-kit-graph-bar',
|
||||
$this->Html->div(
|
||||
'debug-kit-graph-bar-value',
|
||||
' ',
|
||||
array(
|
||||
'style' => "margin-left: {$graphOffset}px; width: {$graphValue}px",
|
||||
'title' => __d('debug_kit', "Starting %sms into the request, taking %sms", $offset, $value),
|
||||
)
|
||||
),
|
||||
array('style' => "width: {$width}px;"),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
171
exp/Plugin/DebugKit/View/Helper/TidyHelper.php
Normal file
171
exp/Plugin/DebugKit/View/Helper/TidyHelper.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since v 1.0 (22-Jun-2009)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('File', 'Utility');
|
||||
|
||||
/**
|
||||
* TidyHelper class
|
||||
*
|
||||
* Passes html through tidy on the command line, and reports markup errors
|
||||
*
|
||||
* @uses AppHelper
|
||||
* @since v 1.0 (22-Jun-2009)
|
||||
*/
|
||||
class TidyHelper extends AppHelper {
|
||||
|
||||
/**
|
||||
* helpers property
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $helpers = array('DebugKit.Toolbar');
|
||||
|
||||
/**
|
||||
* results property
|
||||
*
|
||||
* @var mixed null
|
||||
*/
|
||||
public $results = null;
|
||||
|
||||
/**
|
||||
* Return a nested array of errors for the passed html string
|
||||
* Fudge the markup slightly so that the tag which is invalid is highlighted
|
||||
*
|
||||
* @param string $html ''
|
||||
* @param string $out ''
|
||||
* @return array
|
||||
*/
|
||||
public function process($html = '', &$out = '') {
|
||||
$errors = $this->tidyErrors($html, $out);
|
||||
|
||||
if (!$errors) {
|
||||
return array();
|
||||
}
|
||||
$result = array('Error' => array(), 'Warning' => array(), 'Misc' => array());
|
||||
$errors = explode("\n", $errors);
|
||||
$markup = explode("\n", $out);
|
||||
foreach ($errors as $error) {
|
||||
preg_match('@line (\d+) column (\d+) - (\w+): (.*)@', $error, $matches);
|
||||
if ($matches) {
|
||||
list($original, $line, $column, $type, $message) = $matches;
|
||||
$line = $line - 1;
|
||||
|
||||
$string = '</strong>';
|
||||
if (isset($markup[$line - 1])) {
|
||||
$string .= h($markup[$line - 1]);
|
||||
}
|
||||
$string .= '<strong>' . h(@$markup[$line]) . '</strong>';
|
||||
if (isset($markup[$line + 1])) {
|
||||
$string .= h($markup[$line + 1]);
|
||||
}
|
||||
$string .= '</strong>';
|
||||
|
||||
$result[$type][$string][] = h($message);
|
||||
} elseif ($error) {
|
||||
$message = $error;
|
||||
$result['Misc'][h($message)][] = h($message);
|
||||
}
|
||||
}
|
||||
$this->results = $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* report method
|
||||
*
|
||||
* Call process if a string is passed, or no prior results exist - and return the results using
|
||||
* the toolbar helper to generate a nested navigatable array
|
||||
*
|
||||
* @param mixed $html null
|
||||
* @return string
|
||||
*/
|
||||
public function report($html = null) {
|
||||
if ($html) {
|
||||
$this->process($html);
|
||||
} elseif ($this->results === null) {
|
||||
$this->process($this->_View->output);
|
||||
}
|
||||
if (!$this->results) {
|
||||
return '<p>' . __d('debug_kit', 'No markup errors found') . '</p>';
|
||||
}
|
||||
foreach ($this->results as &$results) {
|
||||
foreach ($results as $type => &$messages) {
|
||||
foreach ($messages as &$message) {
|
||||
$message = html_entity_decode($message, ENT_COMPAT, Configure::read('App.encoding'));
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->Toolbar->makeNeatArray(array_filter($this->results), 0, 0, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the html string through tidy, and return the (raw) errors. pass back a reference to the
|
||||
* normalized string so that the error messages can be linked to the line that caused them.
|
||||
*
|
||||
* @param string $in ''
|
||||
* @param string $out ''
|
||||
* @return string
|
||||
*/
|
||||
public function tidyErrors($in = '', &$out = '') {
|
||||
$out = preg_replace('@>\s*<@s', ">\n<", $in);
|
||||
|
||||
// direct access? windows etc
|
||||
if (function_exists('tidy_parse_string')) {
|
||||
$tidy = tidy_parse_string($out, array(), 'UTF8');
|
||||
$tidy->cleanRepair();
|
||||
$errors = $tidy->errorBuffer . "\n";
|
||||
return $errors;
|
||||
}
|
||||
|
||||
// cli
|
||||
$File = new File(rtrim(TMP, DS) . DS . rand() . '.html', true);
|
||||
$File->write($out);
|
||||
$path = $File->pwd();
|
||||
$errors = $path . '.err';
|
||||
$this->_exec("tidy -eq -utf8 -f $errors $path");
|
||||
$File->delete();
|
||||
|
||||
if (!file_exists($errors)) {
|
||||
return '';
|
||||
}
|
||||
$Error = new File($errors);
|
||||
$errors = $Error->read();
|
||||
$Error->delete();
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* exec method
|
||||
*
|
||||
* @param mixed $cmd
|
||||
* @param mixed $out null
|
||||
* @return boolean True if successful
|
||||
*/
|
||||
protected function _exec($cmd, &$out = null) {
|
||||
if (DS === '/') {
|
||||
$_out = exec($cmd . ' 2>&1', $out, $return);
|
||||
} else {
|
||||
$_out = exec($cmd, $out, $return);
|
||||
}
|
||||
|
||||
if (Configure::read('debug')) {
|
||||
$source = Debugger::trace(array('depth' => 1, 'start' => 2)) . "\n";
|
||||
//CakeLog::write('system_calls_' . date('Y-m-d'), "\n" . $source . Debugger::exportVar(compact('cmd','out','return')));
|
||||
//CakeLog::write('system_calls', "\n" . $source . Debugger::exportVar(compact('cmd','out','return')));
|
||||
}
|
||||
if ($return) {
|
||||
return false;
|
||||
}
|
||||
return $_out ? $_out : true;
|
||||
}
|
||||
}
|
||||
226
exp/Plugin/DebugKit/View/Helper/ToolbarHelper.php
Normal file
226
exp/Plugin/DebugKit/View/Helper/ToolbarHelper.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since DebugKit 0.1
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('DebugKitDebugger', 'DebugKit.Lib');
|
||||
App::uses('AppHelper', 'View/Helper');
|
||||
App::uses('ConnectionManager', 'Model');
|
||||
|
||||
/**
|
||||
* Provides Base methods for content specific debug toolbar helpers.
|
||||
* Acts as a facade for other toolbars helpers as well.
|
||||
*
|
||||
* @since DebugKit 0.1
|
||||
*/
|
||||
class ToolbarHelper extends AppHelper {
|
||||
|
||||
/**
|
||||
* settings property to be overloaded. Subclasses should specify a format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* flag for whether or not cache is enabled.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_cacheEnabled = false;
|
||||
|
||||
/**
|
||||
* Construct the helper and make the backend helper.
|
||||
*
|
||||
* @param $View
|
||||
* @param array|string $options
|
||||
* @return \ToolbarHelper
|
||||
*/
|
||||
public function __construct($View, $options = array()) {
|
||||
$this->_myName = strtolower(get_class($this));
|
||||
$this->settings = array_merge($this->settings, $options);
|
||||
|
||||
if ($this->_myName !== 'toolbarhelper') {
|
||||
parent::__construct($View, $options);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($options['output'])) {
|
||||
$options['output'] = 'DebugKit.HtmlToolbar';
|
||||
}
|
||||
$className = $options['output'];
|
||||
if (strpos($options['output'], '.') !== false) {
|
||||
list($plugin, $className) = explode('.', $options['output']);
|
||||
}
|
||||
$this->_backEndClassName = $className;
|
||||
$this->helpers[$options['output']] = $options;
|
||||
if (isset($options['cacheKey']) && isset($options['cacheConfig'])) {
|
||||
$this->_cacheKey = $options['cacheKey'];
|
||||
$this->_cacheConfig = $options['cacheConfig'];
|
||||
$this->_cacheEnabled = true;
|
||||
}
|
||||
|
||||
parent::__construct($View, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* afterLayout callback
|
||||
*
|
||||
* @param string $layoutFile
|
||||
* @return void
|
||||
*/
|
||||
public function afterLayout($layoutFile) {
|
||||
if (!$this->request->is('requested')) {
|
||||
$this->send();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the backend Helper
|
||||
* used to conditionally trigger toolbar output
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->_backEndClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* call__
|
||||
*
|
||||
* Allows method calls on backend helper
|
||||
*
|
||||
* @param string $method
|
||||
* @param mixed $params
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function __call($method, $params) {
|
||||
if (method_exists($this->{$this->_backEndClassName}, $method)) {
|
||||
return $this->{$this->_backEndClassName}->dispatchMethod($method, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows for writing to panel cache from view.
|
||||
* Some panels generate all variables in the view by
|
||||
* necessity ie. Timer. Using this method, will allow you to replace in full
|
||||
* the content for a panel.
|
||||
*
|
||||
* @param string $name Name of the panel you are replacing.
|
||||
* @param string $content Content to write to the panel.
|
||||
* @return boolean Success of write.
|
||||
*/
|
||||
public function writeCache($name, $content) {
|
||||
if (!$this->_cacheEnabled) {
|
||||
return false;
|
||||
}
|
||||
$existing = (array)Cache::read($this->_cacheKey, $this->_cacheConfig);
|
||||
$existing[0][$name]['content'] = $content;
|
||||
return Cache::write($this->_cacheKey, $existing, $this->_cacheConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the toolbar
|
||||
*
|
||||
* @param string $name Name of the panel you want cached data for
|
||||
* @param integer $index
|
||||
* @return mixed Boolean false on failure, array of data otherwise.
|
||||
*/
|
||||
public function readCache($name, $index = 0) {
|
||||
if (!$this->_cacheEnabled) {
|
||||
return false;
|
||||
}
|
||||
$existing = (array)Cache::read($this->_cacheKey, $this->_cacheConfig);
|
||||
if (!isset($existing[$index][$name]['content'])) {
|
||||
return false;
|
||||
}
|
||||
return $existing[$index][$name]['content'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the query logs for the given connection names.
|
||||
*
|
||||
* ### Options
|
||||
*
|
||||
* - explain - Whether explain links should be generated for this connection.
|
||||
* - cache - Whether the toolbar_state Cache should be updated.
|
||||
* - threshold - The threshold at which a visual 'maybe slow' flag should be added.
|
||||
* results with rows/ms lower than $threshold will be marked.
|
||||
*
|
||||
* @param string $connection Connection name to get logs for.
|
||||
* @param array $options Options for the query log retrieval.
|
||||
* @return array Array of data to be converted into a table.
|
||||
*/
|
||||
public function getQueryLogs($connection, $options = array()) {
|
||||
$options += array('explain' => false, 'cache' => true, 'threshold' => 20);
|
||||
$db = ConnectionManager::getDataSource($connection);
|
||||
|
||||
if (!method_exists($db, 'getLog')) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$log = $db->getLog();
|
||||
|
||||
$out = array(
|
||||
'queries' => array(),
|
||||
'count' => $log['count'],
|
||||
'time' => $log['time']
|
||||
);
|
||||
foreach ($log['log'] as $i => $query) {
|
||||
$query += array('query' => null);
|
||||
$isSlow = (
|
||||
$query['took'] > 0 &&
|
||||
$query['numRows'] / $query['took'] != 1 &&
|
||||
$query['numRows'] / $query['took'] <= $options['threshold']
|
||||
);
|
||||
$query['actions'] = '';
|
||||
$isHtml = ($this->getName() === 'HtmlToolbar');
|
||||
if ($isSlow && $isHtml) {
|
||||
$query['actions'] = sprintf(
|
||||
'<span class="slow-query">%s</span>',
|
||||
__d('debug_kit', 'maybe slow')
|
||||
);
|
||||
} elseif ($isSlow) {
|
||||
$query['actions'] = '*';
|
||||
}
|
||||
if ($options['explain'] && $isHtml) {
|
||||
$query['actions'] .= $this->explainLink($query['query'], $connection);
|
||||
}
|
||||
if ($isHtml) {
|
||||
$query['query'] = h($query['query']);
|
||||
if (!empty($query['params']) && is_array($query['params'])) {
|
||||
$bindParam = $bindType = null;
|
||||
if (preg_match('/.+ :.+/', $query['query'])) {
|
||||
$bindType = true;
|
||||
}
|
||||
foreach ($query['params'] as $bindKey => $bindVal) {
|
||||
if ($bindType === true) {
|
||||
$bindParam .= h($bindKey) . " => " . h($bindVal) . ", ";
|
||||
} else {
|
||||
$bindParam .= h($bindVal) . ", ";
|
||||
}
|
||||
}
|
||||
$query['query'] .= " [ " . rtrim($bindParam, ', ') . " ]";
|
||||
}
|
||||
}
|
||||
unset($query['params']);
|
||||
$out['queries'][] = $query;
|
||||
}
|
||||
if ($options['cache']) {
|
||||
$existing = $this->readCache('sql_log');
|
||||
$existing[$connection] = $out;
|
||||
$this->writeCache('sql_log', $existing);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user