function DatabaseDataCollector::collect

File

webprofiler/src/DataCollector/DatabaseDataCollector.php, line 43

Class

DatabaseDataCollector
Class DatabaseDataCollector

Namespace

Drupal\webprofiler\DataCollector

Code

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) {
            // There was some error during database connection, maybe a stale
            // configuration in settings.php or wrong values used for a migration.
        }
    }
    $this->data['connections'] = array_keys($connections);
    $data = [];
    foreach ($connections as $key => $queries) {
        foreach ($queries as $query) {
            // Remove caller args.
            unset($query['caller']['args']);
            // Remove query args element if empty.
            if (isset($query['args']) && empty($query['args'])) {
                unset($query['args']);
            }
            // Save time in milliseconds.
            $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();
    // Remove password for security.
    unset($options['password']);
    $this->data['database'] = $options;
}