[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/DataCollector/PDO/ -> PDOCollector.php (source)

   1  <?php
   2  
   3  namespace DebugBar\DataCollector\PDO;
   4  
   5  use DebugBar\DataCollector\AssetProvider;
   6  use DebugBar\DataCollector\DataCollector;
   7  use DebugBar\DataCollector\Renderable;
   8  use DebugBar\DataCollector\TimeDataCollector;
   9  
  10  /**
  11   * Collects data about SQL statements executed with PDO
  12   */
  13  class PDOCollector extends DataCollector implements Renderable, AssetProvider
  14  {
  15      protected $connections = array();
  16  
  17      protected $timeCollector;
  18  
  19      protected $renderSqlWithParams = false;
  20  
  21      protected $sqlQuotationChar = '<>';
  22  
  23      /**
  24       * @param \PDO $pdo
  25       * @param TimeDataCollector $timeCollector
  26       */
  27      public function __construct(\PDO $pdo = null, TimeDataCollector $timeCollector = null)
  28      {
  29          $this->timeCollector = $timeCollector;
  30          if ($pdo !== null) {
  31              $this->addConnection($pdo, 'default');
  32          }
  33      }
  34  
  35      /**
  36       * Renders the SQL of traced statements with params embeded
  37       *
  38       * @param boolean $enabled
  39       */
  40      public function setRenderSqlWithParams($enabled = true, $quotationChar = '<>')
  41      {
  42          $this->renderSqlWithParams = $enabled;
  43          $this->sqlQuotationChar = $quotationChar;
  44      }
  45  
  46      /**
  47       * @return bool
  48       */
  49      public function isSqlRenderedWithParams()
  50      {
  51          return $this->renderSqlWithParams;
  52      }
  53  
  54      /**
  55       * @return string
  56       */
  57      public function getSqlQuotationChar()
  58      {
  59          return $this->sqlQuotationChar;
  60      }
  61  
  62      /**
  63       * Adds a new PDO instance to be collector
  64       *
  65       * @param TraceablePDO $pdo
  66       * @param string $name Optional connection name
  67       */
  68      public function addConnection(\PDO $pdo, $name = null)
  69      {
  70          if ($name === null) {
  71              $name = spl_object_hash($pdo);
  72          }
  73          if (!($pdo instanceof TraceablePDO)) {
  74              $pdo = new TraceablePDO($pdo);
  75          }
  76          $this->connections[$name] = $pdo;
  77      }
  78  
  79      /**
  80       * Returns PDO instances to be collected
  81       *
  82       * @return array
  83       */
  84      public function getConnections()
  85      {
  86          return $this->connections;
  87      }
  88  
  89      /**
  90       * @return array
  91       */
  92      public function collect()
  93      {
  94          $data = array(
  95              'nb_statements' => 0,
  96              'nb_failed_statements' => 0,
  97              'accumulated_duration' => 0,
  98              'memory_usage' => 0,
  99              'peak_memory_usage' => 0,
 100              'statements' => array()
 101          );
 102  
 103          foreach ($this->connections as $name => $pdo) {
 104              $pdodata = $this->collectPDO($pdo, $this->timeCollector, $name);
 105              $data['nb_statements'] += $pdodata['nb_statements'];
 106              $data['nb_failed_statements'] += $pdodata['nb_failed_statements'];
 107              $data['accumulated_duration'] += $pdodata['accumulated_duration'];
 108              $data['memory_usage'] += $pdodata['memory_usage'];
 109              $data['peak_memory_usage'] = max($data['peak_memory_usage'], $pdodata['peak_memory_usage']);
 110              $data['statements'] = array_merge($data['statements'],
 111                  array_map(function ($s) use ($name) { $s['connection'] = $name; return $s; }, $pdodata['statements']));
 112          }
 113  
 114          $data['accumulated_duration_str'] = $this->getDataFormatter()->formatDuration($data['accumulated_duration']);
 115          $data['memory_usage_str'] = $this->getDataFormatter()->formatBytes($data['memory_usage']);
 116          $data['peak_memory_usage_str'] = $this->getDataFormatter()->formatBytes($data['peak_memory_usage']);
 117  
 118          return $data;
 119      }
 120  
 121      /**
 122       * Collects data from a single TraceablePDO instance
 123       *
 124       * @param TraceablePDO $pdo
 125       * @param TimeDataCollector $timeCollector
 126       * @param string|null $connectionName the pdo connection (eg default | read | write)
 127       * @return array
 128       */
 129      protected function collectPDO(TraceablePDO $pdo, TimeDataCollector $timeCollector = null, $connectionName = null)
 130      {
 131          if (empty($connectionName) || $connectionName == 'default') {
 132              $connectionName = 'pdo';
 133          } else {
 134              $connectionName = 'pdo ' . $connectionName;
 135          }
 136          $stmts = array();
 137          foreach ($pdo->getExecutedStatements() as $stmt) {
 138              $stmts[] = array(
 139                  'sql' => $this->renderSqlWithParams ? $stmt->getSqlWithParams($this->sqlQuotationChar) : $stmt->getSql(),
 140                  'row_count' => $stmt->getRowCount(),
 141                  'stmt_id' => $stmt->getPreparedId(),
 142                  'prepared_stmt' => $stmt->getSql(),
 143                  'params' => (object) $stmt->getParameters(),
 144                  'duration' => $stmt->getDuration(),
 145                  'duration_str' => $this->getDataFormatter()->formatDuration($stmt->getDuration()),
 146                  'memory' => $stmt->getMemoryUsage(),
 147                  'memory_str' => $this->getDataFormatter()->formatBytes($stmt->getMemoryUsage()),
 148                  'end_memory' => $stmt->getEndMemory(),
 149                  'end_memory_str' => $this->getDataFormatter()->formatBytes($stmt->getEndMemory()),
 150                  'is_success' => $stmt->isSuccess(),
 151                  'error_code' => $stmt->getErrorCode(),
 152                  'error_message' => $stmt->getErrorMessage()
 153              );
 154              if ($timeCollector !== null) {
 155                  $timeCollector->addMeasure($stmt->getSql(), $stmt->getStartTime(), $stmt->getEndTime(), array(), $connectionName);
 156              }
 157          }
 158  
 159          return array(
 160              'nb_statements' => count($stmts),
 161              'nb_failed_statements' => count($pdo->getFailedExecutedStatements()),
 162              'accumulated_duration' => $pdo->getAccumulatedStatementsDuration(),
 163              'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($pdo->getAccumulatedStatementsDuration()),
 164              'memory_usage' => $pdo->getMemoryUsage(),
 165              'memory_usage_str' => $this->getDataFormatter()->formatBytes($pdo->getPeakMemoryUsage()),
 166              'peak_memory_usage' => $pdo->getPeakMemoryUsage(),
 167              'peak_memory_usage_str' => $this->getDataFormatter()->formatBytes($pdo->getPeakMemoryUsage()),
 168              'statements' => $stmts
 169          );
 170      }
 171  
 172      /**
 173       * @return string
 174       */
 175      public function getName()
 176      {
 177          return 'pdo';
 178      }
 179  
 180      /**
 181       * @return array
 182       */
 183      public function getWidgets()
 184      {
 185          return array(
 186              "database" => array(
 187                  "icon" => "database",
 188                  "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
 189                  "map" => "pdo",
 190                  "default" => "[]"
 191              ),
 192              "database:badge" => array(
 193                  "map" => "pdo.nb_statements",
 194                  "default" => 0
 195              )
 196          );
 197      }
 198  
 199      /**
 200       * @return array
 201       */
 202      public function getAssets()
 203      {
 204          return array(
 205              'css' => 'widgets/sqlqueries/widget.css',
 206              'js' => 'widgets/sqlqueries/widget.js'
 207          );
 208      }
 209  }


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