You are here

public function Log::findCaller in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Log.php \Drupal\Core\Database\Log::findCaller()
  2. 9 core/lib/Drupal/Core/Database/Log.php \Drupal\Core\Database\Log::findCaller()

Determine the routine that called this query.

Traversing the call stack from the very first call made during the request, we define "the routine that called this query" as the last entry in the call stack that is not any method called from the namespace of the database driver, is not inside the Drupal\Core\Database namespace and does have a file (which excludes call_user_func_array(), anonymous functions and similar). That makes the climbing logic very simple, and handles the variable stack depth caused by the query builders.

See the debug_backtrace() function.

Return value

This method returns a stack trace entry similar to that generated by debug_backtrace(). However, it flattens the trace entry and the trace entry before it so that we get the function and args of the function that called into the database system, not the function and args of the database call itself.

File

core/lib/Drupal/Core/Database/Log.php, line 151

Class

Log
Database query logger.

Namespace

Drupal\Core\Database

Code

public function findCaller() {
  $driver_namespace = Database::getConnectionInfo($this->connectionKey)['default']['namespace'];
  $stack = static::removeDatabaseEntries($this
    ->getDebugBacktrace(), $driver_namespace);

  // Return the first function call whose stack entry has a 'file' key, that
  // is, it is not a callback or a closure.
  for ($i = 0; $i < count($stack); $i++) {
    if (!empty($stack[$i]['file'])) {
      return [
        'file' => $stack[$i]['file'],
        'line' => $stack[$i]['line'],
        'function' => $stack[$i + 1]['function'],
        'class' => $stack[$i + 1]['class'] ?? NULL,
        'type' => $stack[$i + 1]['type'] ?? NULL,
        'args' => $stack[$i + 1]['args'] ?? [],
      ];
    }
  }
}