function Log::findCaller

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Log.php \Drupal\Core\Database\Log::findCaller()
  2. 8.9.x 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

array|null 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.

Deprecated

in drupal:10.1.0 and is removed from drupal:11.0.0. Use Connection::findCallerFromDebugBacktrace().

See also

https://www.drupal.org/node/3328053

1 call to Log::findCaller()
Log::log in core/lib/Drupal/Core/Database/Log.php
Log a query to all active logging keys.

File

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

Class

Log
Database query logger.

Namespace

Drupal\Core\Database

Code

public function findCaller() {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use Connection::findCallerFromDebugBacktrace(). See https://www.drupal.org/node/3328053', E_USER_DEPRECATED);
    $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'] ?? [],
            ];
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.