Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
* release/1.1.0:
  Raise the version number
  Improve the documentation
  Render the params into the SQL query
  Writes file in typo3conf/ext folder
  Adding jQuery.noConflict() to the header to avoid problems with other jQuery instances
  Extend the JavaScriptRenderer and move all asset related code there
  Rename Fonts folder to lowercase to match the font awesome lib folder naming
  • Loading branch information
Konafets committed Mar 7, 2018
2 parents 208492a + e9f2bbd commit 9ac610d
Show file tree
Hide file tree
Showing 20 changed files with 1,930 additions and 5,737 deletions.
59 changes: 59 additions & 0 deletions Classes/AssetsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php namespace Konafets\TYPO3DebugBar;

use DebugBar\DebugBar;
use DebugBar\JavascriptRenderer;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

class AssetsRenderer extends JavascriptRenderer
{

const PATH_TO_STYLES = 'Resources/Public/Css';
const PATH_TO_JAVASCRIPT = 'Resources/Public/JavaScript';
const CUSTOM_CSS_STYLE_FILENAME = '/typo3_debugbar.css';

/** @var string */
protected $pathToCssAssetFile = '';

/** @var string */
protected $pathToJsAssetFile = '';

/**
* @param DebugBar $debugBar
* @param null $baseUrl
* @param null $basePath
*/
public function __construct(DebugBar $debugBar, $baseUrl = null, $basePath = null)
{
parent::__construct($debugBar, $baseUrl, $basePath);
$extensionPath = ExtensionManagementUtility::extPath(Typo3DebugBar::EXTENSION_KEY);

$this->pathToCssAssetFile = 'typo3temp/tx_typo3_debugbar_styles.css';
$this->pathToJsAssetFile = 'typo3temp/tx_typo3_debugbar_javascript.js';

$this->cssVendors['fontawesome'] = $extensionPath . 'Resources/Public/vendor/font-awesome/style.css';
$this->cssFiles['typo3'] = $extensionPath . self::PATH_TO_STYLES . self::CUSTOM_CSS_STYLE_FILENAME;
}

/**
* Renders the html to include needed assets
*
* Only useful if Assetic is not used
*
* @return string
*/
public function renderHead()
{
$this->dumpCssAssets($this->pathToCssAssetFile);
$this->dumpJsAssets($this->pathToJsAssetFile);

$html = '';
$html .= "<link href='{$this->pathToCssAssetFile}' rel='stylesheet' type='text/css'>\n";
$html .= "<script src='{$this->pathToJsAssetFile}' type='text/javascript'></script>\n";

if ($this->isJqueryNoConflictEnabled()) {
$html .= '<script type="text/javascript">jQuery.noConflict(true);</script>' . "\n";
}

return $html;
}
}
113 changes: 101 additions & 12 deletions Classes/DataCollectors/MySqliCollector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Konafets\TYPO3DebugBar\DataCollectors;

use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\Renderable;
use DebugBar\DataCollector\TimeDataCollector;
Expand All @@ -13,18 +14,20 @@
* @package Konafets\TYPO3DebugBar\DataCollectors
* @author Stefano Kowalke <info@arroba-it.de>
*/
class MySqliCollector extends BaseCollector implements DataCollectorInterface, Renderable
class MySqliCollector extends BaseCollector implements DataCollectorInterface, Renderable, AssetProvider
{

protected $connections = [];
const DEFAULT_CONNECTION = 'Default';

/** @var bool */
protected $renderSqlWithParams = false;

protected $sqlQuotationChar = '<>';

/** @var DebugStack $sqlLogger */
protected $sqlLogger;

/** @var \TYPO3\CMS\Core\Database\Connection */
protected $connection;

/**
* The constructor
*
Expand All @@ -35,9 +38,47 @@ public function __construct($mysqli = null, TimeDataCollector $timeDataCollector
{
parent::__construct();

$this->connection = $this->getDefaultConnection();
$this->sqlLogger = $this->getSqlLoggerFromDatabaseConfiguration();
}

/**
* @return mixed
*/
public static function getDefaultConnection()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connections = $connectionPool->getConnectionNames();
$connectionName = self::DEFAULT_CONNECTION;

if (! in_array($connectionName, $connections)) {
list($connectionName) = $connections;
}

$connection = $connectionPool->getConnectionByName($connectionName);

return $connection;
}

/**
* Renders the SQL of traced statements with params embeded
*
* @param bool $enabled
* @param string $quotationChar
*/
public function setRenderSqlWithParams($enabled = true)
{
$this->renderSqlWithParams = $enabled;
}

/**
* @return bool
*/
public function isSqlRenderedWithParams()
{
return $this->renderSqlWithParams;
}

/**
* Called by the DebugBar when data needs to be collected
*
Expand All @@ -52,12 +93,17 @@ function collect()
foreach ($queries as $query) {
$totalTime += $query['executionMS'];

if ($this->isSqlRenderedWithParams()) {
$this->addParamsToSql($query['sql'], $query['params']);
unset($query['params']);
}

$statements[] = [
'sql' => $query['sql'],
'params' => $query['params'],
'duration' => $query['executionMS'],
'duration_str' => $this->formatDuration($query['executionMS']),
'connection' => key($this->connections),
'connection' => $this->connection->getDatabase(),
];
}

Expand Down Expand Up @@ -106,17 +152,60 @@ function getWidgets()

private function getSqlLoggerFromDatabaseConfiguration()
{
$this->getConnections();
if ($this->connection === null) {
$this->connection = $this->getDefaultConnection();
}

return $this->connections['Default']->getConfiguration()->getSQLLogger();
return $this->connection->getConfiguration()->getSQLLogger();
}

private function getConnections()
/**
* @param string $sql
* @param array $params
*/
private function addParamsToSql(&$sql, array $params)
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);

foreach ($connectionPool->getConnectionNames() as $connectionName) {
$this->connections[$connectionName] = $connectionPool->getConnectionByName($connectionName);
foreach ($params as $key => $param) {
if (is_array($param)) {
$param = $param[0];
}

$regex = is_numeric($key)
? "/\?(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/"
: "/:{$key}(?=(?:[^'\\\']*'[^'\\\']*')*[^'\\\']*$)/";
$sql = preg_replace($regex, $this->connection->quote($param), $sql, 1);
}
}

/**
* Returns an array with the following keys:
* - base_path
* - base_url
* - css: an array of filenames
* - js: an array of filenames
* - inline_css: an array map of content ID to inline CSS content (not including <style> tag)
* - inline_js: an array map of content ID to inline JS content (not including <script> tag)
* - inline_head: an array map of content ID to arbitrary inline HTML content (typically
* <style>/<script> tags); it must be embedded within the <head> element
*
* All keys are optional.
*
* Ideally, you should store static assets in filenames that are returned via the normal css/js
* keys. However, the inline asset elements are useful when integrating with 3rd-party
* libraries that require static assets that are only available in an inline format.
*
* The inline content arrays require special string array keys: the caller of this function
* will use them to deduplicate content. This is particularly useful if multiple instances of
* the same asset provider are used. Inline assets from all collectors are merged together into
* the same array, so these content IDs effectively deduplicate the inline assets.
*
* @return array
*/
function getAssets()
{
return [
'css' => 'widgets/sqlqueries/widget.css',
'js' => 'widgets/sqlqueries/widget.js',
];
}
}
36 changes: 36 additions & 0 deletions Classes/DataCollectors/VarDumpCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\Renderable;
use Konafets\TYPO3DebugBar\AssetsRenderer;
use Konafets\TYPO3DebugBar\Typo3DebugBar;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;

/**
* Class VarDumpCollector
Expand Down Expand Up @@ -72,4 +75,37 @@ function getWidgets()
],
];
}

/**
* Returns an array with the following keys:
* - base_path
* - base_url
* - css: an array of filenames
* - js: an array of filenames
* - inline_css: an array map of content ID to inline CSS content (not including <style> tag)
* - inline_js: an array map of content ID to inline JS content (not including <script> tag)
* - inline_head: an array map of content ID to arbitrary inline HTML content (typically
* <style>/<script> tags); it must be embedded within the <head> element
*
* All keys are optional.
*
* Ideally, you should store static assets in filenames that are returned via the normal css/js
* keys. However, the inline asset elements are useful when integrating with 3rd-party
* libraries that require static assets that are only available in an inline format.
*
* The inline content arrays require special string array keys: the caller of this function
* will use them to deduplicate content. This is particularly useful if multiple instances of
* the same asset provider are used. Inline assets from all collectors are merged together into
* the same array, so these content IDs effectively deduplicate the inline assets.
*
* @return array
*/
public function getAssets()
{
$path = ExtensionManagementUtility::extPath(Typo3DebugBar::EXTENSION_KEY);

return [
'js' => $path . AssetsRenderer::PATH_TO_JAVASCRIPT . '/generic/widget.js',
];
}
}
Loading

0 comments on commit 9ac610d

Please sign in to comment.