Code Monkey home page Code Monkey logo

backtrace's Introduction

A better PHP backtrace

Latest Version on Packagist Tests Total Downloads

To get the backtrace in PHP you can use the debug_backtrace function. By default, it can be hard to work with. The reported function name for a frame is skewed: it belongs to the previous frame. Also, options need to be passed using a bitmask.

This package provides a better way than debug_backtrace to work with a back trace. Here's an example:

// returns an array with `Spatie\Backtrace\Frame` instances
$frames = Spatie\Backtrace\Backtrace::create()->frames(); 

$firstFrame = $frames[0];

$firstFrame->file; // returns the file name
$firstFrame->lineNumber; // returns the line number
$firstFrame->class; // returns the class name

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/backtrace

Usage

This is how you can create a backtrace instance:

$backtrace = Spatie\Backtrace\Backtrace::create();

Getting the frames

To get all the frames you can call frames.

$frames = $backtrace->frames(); // contains an array with `Spatie\Backtrace\Frame` instances

A Spatie\Backtrace\Frame has these properties:

  • file: the name of the file
  • lineNumber: the line number
  • arguments: the arguments used for this frame. Will be null if withArguments was not used.
  • class: the class name for this frame. Will be null if the frame concerns a function.
  • method: the method used in this frame
  • applicationFrame: contains true is this frame belongs to your application, and false if it belongs to a file in the vendor directory

Collecting arguments

For performance reasons, the frames of the back trace will not contain the arguments of the called functions. If you want to add those use the withArguments method.

$backtrace = Spatie\Backtrace\Backtrace::create()->withArguments();

Setting the application path

You can use the applicationPath to pass the base path of your app. This value will be used to determine whether a frame is an application frame, or a vendor frame. Here's an example using a Laravel specific function.

$backtrace = Spatie\Backtrace\Backtrace::create()->applicationPath(base_path());

Getting a certain part of a trace

If you only want to have the frames starting from a particular frame in the backtrace you can use the startingFromFrame method:

use Spatie\Backtrace\Backtrace;
use Spatie\Backtrace\Frame;

$frames = Backtrace::create()
    ->startingFromFrame(function (Frame $frame) {
        return $frame->class === MyClass::class;
    })
    ->frames();

With this code, all frames before the frame that concerns MyClass will have been filtered out.

Alternatively, you can use the offSet method, which will skip the given number of frames. In this example the first 2 frames will not end up in $frames.

$frames = Spatie\Backtrace\Backtrace::create()
    ->offset(2)
    ->frames();

Limiting the number of frames

To only get a specific number of frames use the limit function. In this example, we'll only get the first two frames.

$frames = Spatie\Backtrace\Backtrace::create()
    ->limit(2)
    ->frames();

Getting a backtrace for a throwable

Here's how you can get a backtrace for a throwable.

$frames = Spatie\Backtrace\Backtrace::createForThrowable($throwable)

Because we will use the backtrace that is already available the throwable, the frames will always contain the arguments used.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.