From 04ec683021d87e72d0821d11269abf73a9b6f7a7 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Sat, 7 Sep 2013 17:37:24 +0200 Subject: [PATCH] add PropelDataCollector to debugbar, still missing some informations --- core/lib/Thelia/Core/Thelia.php | 3 +- .../DataCollector/PropelCollector.php | 236 ++++++++++++++++++ .../DebugBar/Listeners/DebugBarListeners.php | 10 +- 3 files changed, 239 insertions(+), 10 deletions(-) create mode 100644 local/modules/DebugBar/DataCollector/PropelCollector.php diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index 759f1108b..64f8e9a61 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -84,8 +84,7 @@ class Thelia extends Kernel $serviceContainer->setConnectionManager('thelia', $manager); if ($this->isDebug()) { - $serviceContainer->setLogger('defaultLogger', Tlog::getInstance()); - + //$serviceContainer->setLogger('defaultLogger', Tlog::getInstance()); $con = Propel::getConnection(\Thelia\Model\Map\ProductTableMap::DATABASE_NAME); $con->useDebug(true); } diff --git a/local/modules/DebugBar/DataCollector/PropelCollector.php b/local/modules/DebugBar/DataCollector/PropelCollector.php new file mode 100644 index 000000000..881b1b64a --- /dev/null +++ b/local/modules/DebugBar/DataCollector/PropelCollector.php @@ -0,0 +1,236 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace DebugBar\DataCollector; +use Propel\Runtime\Propel; +use Psr\Log\LoggerInterface; + + +/** + * Class PropelCollector + * @package DebugBar\DataCollector + * @author Manuel Raynaud + */ +class PropelCollector extends DataCollector implements Renderable, LoggerInterface +{ + + protected $statements = array(); + + protected $accumulatedTime = 0; + + protected $peakMemory = 0; + + public function __construct() + { + $serviceContainer = Propel::getServiceContainer(); + $serviceContainer->setLogger('defaultLogger', $this); + + $con = Propel::getConnection(\Thelia\Model\Map\ProductTableMap::DATABASE_NAME); + $con->setLogMethods(array( + 'exec', + 'query', + 'execute', // these first three are the default + 'beginTransaction', + 'commit', + 'rollBack', + )); + } + + /** + * Called by the DebugBar when data needs to be collected + * + * @return array Collected data + */ + function collect() + { + return array( + 'nb_statements' => count($this->statements), + 'nb_failed_statements' => 0, + 'accumulated_duration' => '10', + 'accumulated_duration_str' => $this->formatDuration(1), + 'peak_memory_usage' => $this->peakMemory, + 'peak_memory_usage_str' => $this->formatBytes($this->peakMemory), + 'statements' => $this->statements + ); + } + + /** + * Returns the unique name of the collector + * + * @return string + */ + public function getName() + { + return 'propel'; + } + + /** + * Returns a hash where keys are control names and their values + * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()} + * + * @return array + */ + public function getWidgets() + { + return array( + "propel" => array( + "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget", + "map" => "propel", + "default" => "[]" + ), + "propel:badge" => array( + "map" => "propel.nb_statements", + "default" => 0 + ) + ); + } + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * @return null + */ + public function log($level, $message, array $context = array()) + { + $this->statements[] = array( + 'sql' => $message, + 'is_success' => true, + 'duration' => 0, + 'duration_str' => $this->formatDuration(1), + 'memory' => 1, + 'memory_str' => $this->formatBytes(1) + ); + } + + /** + * System is unusable. + * + * @param string $message + * @param array $context + * @return null + */ + public function emergency($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Action must be taken immediately. + * + * Example: Entire website down, database unavailable, etc. This should + * trigger the SMS alerts and wake you up. + * + * @param string $message + * @param array $context + * @return null + */ + public function alert($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * @return null + */ + public function critical($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * @return null + */ + public function error($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Exceptional occurrences that are not errors. + * + * Example: Use of deprecated APIs, poor use of an API, undesirable things + * that are not necessarily wrong. + * + * @param string $message + * @param array $context + * @return null + */ + public function warning($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * @return null + */ + public function notice($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * @return null + */ + public function info($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * @return null + */ + public function debug($message, array $context = array()) + { + $this->log(null, $message, $context); + } + + +} \ No newline at end of file diff --git a/local/modules/DebugBar/Listeners/DebugBarListeners.php b/local/modules/DebugBar/Listeners/DebugBarListeners.php index 2c2a1a08c..7d0981d5d 100644 --- a/local/modules/DebugBar/Listeners/DebugBarListeners.php +++ b/local/modules/DebugBar/Listeners/DebugBarListeners.php @@ -22,9 +22,7 @@ /*************************************************************************************/ namespace DebugBar\Listeners; -use DebugBar\DataCollector\PDO\PDOCollector; -use DebugBar\DataCollector\PDO\TraceablePDO; -use Propel\Runtime\Propel; +use DebugBar\DataCollector\PropelCollector; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Thelia\Action\BaseAction; @@ -42,11 +40,7 @@ class DebugBarListeners extends BaseAction implements EventSubscriberInterface { { $debugBar = $this->container->get("debugBar"); - $connection = Propel::getConnection(\Thelia\Model\Map\ProductTableMap::DATABASE_NAME); - $connection = $connection->getWrappedConnection(); - - $pdo = new TraceablePDO($connection); - $debugBar->addCollector(new PDOCollector($pdo)); + $debugBar->addCollector(new PropelCollector()); } /**