[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/Bridge/ -> Propel2Collector.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\Bridge;
  12  
  13  use DebugBar\DataCollector\AssetProvider;
  14  use DebugBar\DataCollector\DataCollector;
  15  use DebugBar\DataCollector\Renderable;
  16  use Monolog\Handler\TestHandler;
  17  use Monolog\Logger;
  18  use Propel\Runtime\Connection\ConnectionInterface;
  19  use Propel\Runtime\Connection\ProfilerConnectionWrapper;
  20  use Propel\Runtime\Propel;
  21  use Psr\Log\LogLevel;
  22  use Psr\Log\LoggerInterface;
  23  
  24  /**
  25   * A Propel logger which acts as a data collector
  26   *
  27   * http://propelorm.org/
  28   *
  29   * Will log queries and display them using the SQLQueries widget.
  30   *
  31   * Example:
  32   * <code>
  33   * $debugbar->addCollector(new \DebugBar\Bridge\Propel2Collector(\Propel\Runtime\Propel::getServiceContainer()->getReadConnection()));
  34   * </code>
  35   */
  36  class Propel2Collector extends DataCollector implements Renderable, AssetProvider
  37  {
  38      /**
  39       * @var null|TestHandler
  40       */
  41      protected $handler = null;
  42  
  43      /**
  44       * @var null|Logger
  45       */
  46      protected $logger = null;
  47  
  48      /**
  49       * @var array
  50       */
  51      protected $config = array();
  52  
  53      /**
  54       * @var array
  55       */
  56      protected $errors = array();
  57  
  58      /**
  59       * @var int
  60       */
  61      protected $queryCount = 0;
  62  
  63      /**
  64       * @param ConnectionInterface $connection Propel connection
  65       */
  66      public function __construct(
  67          ConnectionInterface $connection,
  68          array $logMethods = array(
  69              'beginTransaction',
  70              'commit',
  71              'rollBack',
  72              'forceRollBack',
  73              'exec',
  74              'query',
  75              'execute'
  76          )
  77      ) {
  78          if ($connection instanceof ProfilerConnectionWrapper) {
  79              $connection->setLogMethods($logMethods);
  80  
  81              $this->config = $connection->getProfiler()->getConfiguration();
  82  
  83              $this->handler = new TestHandler();
  84  
  85              if ($connection->getLogger() instanceof Logger) {
  86                  $this->logger = $connection->getLogger();
  87                  $this->logger->pushHandler($this->handler);
  88              } else {
  89                  $this->errors[] = 'Supported only monolog logger';
  90              }
  91          } else {
  92              $this->errors[] = 'You need set ProfilerConnectionWrapper';
  93          }
  94      }
  95  
  96      /**
  97       * @return TestHandler|null
  98       */
  99      public function getHandler()
 100      {
 101          return $this->handler;
 102      }
 103  
 104      /**
 105       * @return array
 106       */
 107      public function getConfig()
 108      {
 109          return $this->config;
 110      }
 111  
 112      /**
 113       * @return Logger|null
 114       */
 115      public function getLogger()
 116      {
 117          return $this->logger;
 118      }
 119  
 120      /**
 121       * @return LoggerInterface
 122       */
 123      protected function getDefaultLogger()
 124      {
 125          return Propel::getServiceContainer()->getLogger();
 126      }
 127  
 128      /**
 129       * @return int
 130       */
 131      protected function getQueryCount()
 132      {
 133          return $this->queryCount;
 134      }
 135  
 136      /**
 137       * @param array $records
 138       * @param array $config
 139       * @return array
 140       */
 141      protected function getStatements($records, $config)
 142      {
 143          $statements = array();
 144          foreach ($records as $record) {
 145              $duration = null;
 146              $memory = null;
 147  
 148              $isSuccess = ( LogLevel::INFO === strtolower($record['level_name']) );
 149  
 150              $detailsCount = count($config['details']);
 151              $parameters = explode($config['outerGlue'], $record['message'], $detailsCount + 1);
 152              if (count($parameters) === ($detailsCount + 1)) {
 153                  $parameters = array_map('trim', $parameters);
 154                  $_details = array();
 155                  foreach (array_splice($parameters, 0, $detailsCount) as $string) {
 156                      list($key, $value) = array_map('trim', explode($config['innerGlue'], $string, 2));
 157                      $_details[$key] = $value;
 158                  }
 159  
 160                  $details = array();
 161                  foreach ($config['details'] as $key => $detail) {
 162                      if (isset($_details[$detail['name']])) {
 163                          $value = $_details[$detail['name']];
 164                          if ('time' === $key) {
 165                              if (substr_count($value, 'ms')) {
 166                                  $value = (float)$value / 1000;
 167                              } else {
 168                                  $value = (float)$value;
 169                              }
 170                          } else {
 171                              $suffixes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
 172                              $suffix = substr($value, -2);
 173                              $i = array_search($suffix, $suffixes, true);
 174                              $i = (false === $i) ? 0 : $i;
 175  
 176                              $value = ((float)$value) * pow(1024, $i);
 177                          }
 178                          $details[$key] = $value;
 179                      }
 180                  }
 181  
 182                  if (isset($details['time'])) {
 183                      $duration = $details['time'];
 184                  }
 185                  if (isset($details['memDelta'])) {
 186                      $memory = $details['memDelta'];
 187                  }
 188  
 189                  $message = end($parameters);
 190  
 191                  if ($isSuccess) {
 192                      $this->queryCount++;
 193                  }
 194  
 195              } else {
 196                  $message = $record['message'];
 197              }
 198  
 199              $statement = array(
 200                  'sql' => $message,
 201                  'is_success' => $isSuccess,
 202                  'duration' => $duration,
 203                  'duration_str' => $this->getDataFormatter()->formatDuration($duration),
 204                  'memory' => $memory,
 205                  'memory_str' => $this->getDataFormatter()->formatBytes($memory),
 206              );
 207  
 208              if (false === $isSuccess) {
 209                  $statement['sql'] = '';
 210                  $statement['error_code'] = $record['level'];
 211                  $statement['error_message'] = $message;
 212              }
 213  
 214              $statements[] = $statement;
 215          }
 216          return $statements;
 217      }
 218  
 219      /**
 220       * @return array
 221       */
 222      public function collect()
 223      {
 224          if (count($this->errors)) {
 225              return array(
 226                  'statements' => array_map(function ($message) {
 227                      return array('sql' => '', 'is_success' => false, 'error_code' => 500, 'error_message' => $message);
 228                  }, $this->errors),
 229                  'nb_statements' => 0,
 230                  'nb_failed_statements' => count($this->errors),
 231              );
 232          }
 233  
 234          if ($this->getHandler() === null) {
 235              return array();
 236          }
 237  
 238          $statements = $this->getStatements($this->getHandler()->getRecords(), $this->getConfig());
 239  
 240          $failedStatement = count(array_filter($statements, function ($statement) {
 241              return false === $statement['is_success'];
 242          }));
 243          $accumulatedDuration = array_reduce($statements, function ($accumulatedDuration, $statement) {
 244          
 245              $time = isset($statement['duration']) ? $statement['duration'] : 0;
 246              return $accumulatedDuration += $time;
 247          });
 248          $memoryUsage = array_reduce($statements, function ($memoryUsage, $statement) {
 249          
 250              $time = isset($statement['memory']) ? $statement['memory'] : 0;
 251              return $memoryUsage += $time;
 252          });
 253  
 254          return array(
 255              'nb_statements' => $this->getQueryCount(),
 256              'nb_failed_statements' => $failedStatement,
 257              'accumulated_duration' => $accumulatedDuration,
 258              'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($accumulatedDuration),
 259              'memory_usage' => $memoryUsage,
 260              'memory_usage_str' => $this->getDataFormatter()->formatBytes($memoryUsage),
 261              'statements' => $statements
 262          );
 263      }
 264  
 265      /**
 266       * @return string
 267       */
 268      public function getName()
 269      {
 270          $additionalName  = '';
 271          if ($this->getLogger() !== $this->getDefaultLogger()) {
 272              $additionalName = ' ('.$this->getLogger()->getName().')';
 273          }
 274  
 275          return 'propel2'.$additionalName;
 276      }
 277  
 278      /**
 279       * @return array
 280       */
 281      public function getWidgets()
 282      {
 283          return array(
 284              $this->getName() => array(
 285                  'icon' => 'bolt',
 286                  'widget' => 'PhpDebugBar.Widgets.SQLQueriesWidget',
 287                  'map' => $this->getName(),
 288                  'default' => '[]'
 289              ),
 290              $this->getName().':badge' => array(
 291                  'map' => $this->getName().'.nb_statements',
 292                  'default' => 0
 293              ),
 294          );
 295      }
 296  
 297      /**
 298       * @return array
 299       */
 300      public function getAssets()
 301      {
 302          return array(
 303              'css' => 'widgets/sqlqueries/widget.css',
 304              'js' => 'widgets/sqlqueries/widget.js'
 305          );
 306      }
 307  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer