DatabaseDataCollector.php in Devel 8
File
webprofiler/src/DataCollector/DatabaseDataCollector.php
View source
<?php
namespace Drupal\webprofiler\DataCollector;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webprofiler\DrupalDataCollectorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
class DatabaseDataCollector extends DataCollector implements DrupalDataCollectorInterface {
use StringTranslationTrait, DrupalDataCollectorTrait;
private $database;
private $configFactory;
public function __construct(Connection $database, ConfigFactoryInterface $configFactory) {
$this->database = $database;
$this->configFactory = $configFactory;
}
public function collect(Request $request, Response $response, \Exception $exception = NULL) {
$connections = [];
foreach (Database::getAllConnectionInfo() as $key => $info) {
try {
$database = Database::getConnection('default', $key);
if ($database
->getLogger()) {
$connections[$key] = $database
->getLogger()
->get('webprofiler');
}
} catch (\Exception $e) {
}
}
$this->data['connections'] = array_keys($connections);
$data = [];
foreach ($connections as $key => $queries) {
foreach ($queries as $query) {
unset($query['caller']['args']);
if (isset($query['args']) && empty($query['args'])) {
unset($query['args']);
}
$query['time'] = $query['time'] * 1000;
$query['database'] = $key;
$data[] = $query;
}
}
$querySort = $this->configFactory
->get('webprofiler.config')
->get('query_sort');
if ('duration' === $querySort) {
usort($data, [
"Drupal\\webprofiler\\DataCollector\\DatabaseDataCollector",
"orderQueryByTime",
]);
}
$this->data['queries'] = $data;
$options = $this->database
->getConnectionOptions();
unset($options['password']);
$this->data['database'] = $options;
}
public function getDatabase() {
return $this->data['database'];
}
public function getQueryCount() {
return count($this->data['queries']);
}
public function getQueries() {
return $this->data['queries'];
}
public function getTime() {
$time = 0;
foreach ($this->data['queries'] as $query) {
$time += $query['time'];
}
return $time;
}
public function getColorCode() {
if ($this
->getQueryCount() < 100) {
return 'green';
}
if ($this
->getQueryCount() < 200) {
return 'yellow';
}
return 'red';
}
public function getQueryHighlightThreshold() {
return \Drupal::config('webprofiler.config')
->get('query_highlight');
}
public function getName() {
return 'database';
}
public function getTitle() {
return $this
->t('Database');
}
public function getPanelSummary() {
return $this
->t('Executed queries: @count', [
'@count' => $this
->getQueryCount(),
]);
}
public function getIcon() {
return 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQRJREFUeNpi/P//PwM1ARMDlcGogZQDlpMnT7pxc3NbA9nhQKxOpL5rQLwJiPeBsI6Ozl+YBOOOHTv+AOllQNwtLS39F2owKYZ/gRq8G4i3ggxEToggWzvc3d2Pk+1lNL4fFAs6ODi8JzdS7mMRVyDVoAMHDsANdAPiOCC+jCQvQKqBQB/BDbwBxK5AHA3E/kB8nKJkA8TMQBwLxaBIKQbi70AvTADSBiSadwFXpCikpKQU8PDwkGTaly9fHFigkaKIJid4584dkiMFFI6jkTJII0WVmpHCAixZQEXWYhDeuXMnyLsVlEQKI45qFBQZ8eRECi4DBaAlDqle/8A48ip6gAADANdQY88Uc0oGAAAAAElFTkSuQmCC';
}
public function getLibraries() {
return [
'webprofiler/database',
];
}
public function getData() {
$data = $this->data;
$conn = Database::getConnection();
foreach ($data['queries'] as &$query) {
$explain = TRUE;
$type = 'select';
if (strpos($query['query'], 'INSERT') !== FALSE) {
$explain = FALSE;
$type = 'insert';
}
if (strpos($query['query'], 'UPDATE') !== FALSE) {
$explain = FALSE;
$type = 'update';
}
if (strpos($query['query'], 'CREATE') !== FALSE) {
$explain = FALSE;
$type = 'create';
}
if (strpos($query['query'], 'DELETE') !== FALSE) {
$explain = FALSE;
$type = 'delete';
}
$query['explain'] = $explain;
$query['type'] = $type;
$quoted = [];
if (isset($query['args'])) {
foreach ((array) $query['args'] as $key => $val) {
$quoted[$key] = is_null($val) ? 'NULL' : $conn
->quote($val);
}
}
$query['query_args'] = strtr($query['query'], $quoted);
}
$data['query_highlight_threshold'] = $this
->getQueryHighlightThreshold();
return $data;
}
private function orderQueryByTime($a, $b) {
$at = $a['time'];
$bt = $b['time'];
if ($at == $bt) {
return 0;
}
return $at < $bt ? 1 : -1;
}
}