Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to add a custom stacktrace parsing method #1585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Commits on Mar 22, 2024

  1. Ability to add a custom stacktrace parsing method

    Hello there,
    
    I use https://github.com/yajra/laravel-datatables package in almost all of my projects.
    
    I currently have a problem of seeing the source of the queries inside my DataTable classes.
    
    For example:
    
    I have a query builder setup inside my `UsersDataTable`.
    
    ```php
        public function query()
        {
            return User::query();
        }
    ```
    
    Since this function only starts to bulid the query it doesn't show up in `debug_backtrace()`.
    
    For this I've added a solution which allows me to check the "query sender" object and modify how the `$frame` was filled.
    
    I've added simple static method to add my custom callables to `QueryCollector`, in there I can modify and return the `$frame` as I wish in case the `$trace['object']` is matching my first parameter `$objectType`.
    
    Here is an example `DataTable::class` query tracer.
    
    ```php
    QueryCollector::addCustomFrameParser(DataTable::class, function (object $frame, array $trace) {
        // Remove the 'App\' prefix from the namespace
        $relativeNamespace = str_replace('App\\', '', $trace['object']::class);
    
        // Convert namespace to a directory path
        $directoryPath = str_replace('\\', DIRECTORY_SEPARATOR, $relativeNamespace);
        $filePath = app_path($directoryPath) . '.php';
    
        // Get the file path inside the project from the class object
        $frame->file = $filePath;
    
        return $frame;
    });
    ```
    
    After I add this to my `AppServiceProvider`, I can simply see the change inside my "Query" tab.
    
    Before:
    IMG HERE
    
    After:
    IMG HERE
    OzanKurt committed Mar 22, 2024
    Configuration menu
    Copy the full SHA
    e138039 View commit details
    Browse the repository at this point in the history