Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
 
2
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1" />](https://supportukrainenow.org)
3
 
148 lars 4
# A better PHP backtrace
5
 
6
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/backtrace.svg?style=flat-square)](https://packagist.org/packages/spatie/backtrace)
7
![Tests](https://github.com/spatie/backtrace/workflows/Tests/badge.svg)
8
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/backtrace.svg?style=flat-square)](https://packagist.org/packages/spatie/backtrace)
9
 
10
To get the backtrace in PHP you can use the `debug_backtrace` function. By default, it can be hard to work with. The
11
reported function name for a frame is skewed: it belongs to the previous frame. Also, options need to be passed using a bitmask.
12
 
13
This package provides a better way than `debug_backtrace` to work with a back trace. Here's an example:
14
 
15
```php
16
// returns an array with `Spatie\Backtrace\Frame` instances
17
$frames = Spatie\Backtrace\Backtrace::create()->frames();
18
 
19
$firstFrame = $frames[0];
20
 
21
$firstFrame->file; // returns the file name
22
$firstFrame->lineNumber; // returns the line number
23
$firstFrame->class; // returns the class name
24
```
25
 
26
## Support us
27
 
28
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/backtrace.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/backtrace)
29
 
30
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can
31
support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
32
 
33
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
34
You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards
35
on [our virtual postcard wall](https://spatie.be/open-source/postcards).
36
 
37
## Installation
38
 
39
You can install the package via composer:
40
 
41
```bash
42
composer require spatie/backtrace
43
```
44
 
45
## Usage
46
 
47
This is how you can create a backtrace instance:
48
 
49
```php
50
$backtrace = Spatie\Backtrace\Backtrace::create();
51
```
52
 
53
### Getting the frames
54
 
55
To get all the frames you can call `frames`.
56
 
57
```php
58
$frames = $backtrace->frames(); // contains an array with `Spatie\Backtrace\Frame` instances
59
```
60
 
61
A `Spatie\Backtrace\Frame` has these properties:
62
 
63
- `file`: the name of the file
64
- `lineNumber`: the line number
65
- `arguments`: the arguments used for this frame. Will be `null` if `withArguments` was not used.
66
- `class`: the class name for this frame. Will be `null` if the frame concerns a function.
67
- `method`: the method used in this frame
68
- `applicationFrame`: contains `true` is this frame belongs to your application, and `false` if it belongs to a file in
69
  the vendor directory
70
 
71
### Collecting arguments
72
 
73
For performance reasons, the frames of the back trace will not contain the arguments of the called functions. If you
74
want to add those use the `withArguments` method.
75
 
76
```php
77
$backtrace = Spatie\Backtrace\Backtrace::create()->withArguments();
78
```
79
 
80
### Setting the application path
81
 
82
You can use the `applicationPath` to pass the base path of your app. This value will be used to determine whether a
83
frame is an application frame, or a vendor frame. Here's an example using a Laravel specific function.
84
 
85
```php
86
$backtrace = Spatie\Backtrace\Backtrace::create()->applicationPath(base_path());
87
```
88
 
89
### Getting a certain part of a trace
90
 
91
If you only want to have the frames starting from a particular frame in the backtrace you can use
92
the `startingFromFrame` method:
93
 
94
```php
95
use Spatie\Backtrace\Backtrace;
96
use Spatie\Backtrace\Frame;
97
 
98
$frames = Backtrace::create()
99
    ->startingFromFrame(function (Frame $frame) {
100
        return $frame->class === MyClass::class;
101
    })
102
    ->frames();
103
```
104
 
105
With this code, all frames before the frame that concerns `MyClass` will have been filtered out.
106
 
621 lars 107
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`.
148 lars 108
 
109
```php
110
$frames = Spatie\Backtrace\Backtrace::create()
111
    ->offset(2)
112
    ->frames();
113
```
114
 
115
### Limiting the number of frames
116
 
117
To only get a specific number of frames use the `limit` function. In this example, we'll only get the first two frames.
118
 
119
```php
120
$frames = Spatie\Backtrace\Backtrace::create()
121
    ->limit(2)
122
    ->frames();
123
```
124
 
125
###  Getting a backtrace for a throwable
126
 
127
Here's how you can get a backtrace for a throwable.
128
 
129
```php
130
$frames = Spatie\Backtrace\Backtrace::createForThrowable($throwable)
131
```
132
 
133
Because we will use the backtrace that is already available the throwable, the frames will always contain the arguments used.
134
 
135
## Testing
136
 
137
``` bash
138
composer test
139
```
140
 
141
## Changelog
142
 
143
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
144
 
145
## Contributing
146
 
621 lars 147
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
148 lars 148
 
149
## Security Vulnerabilities
150
 
151
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
152
 
153
## Credits
154
 
155
- [Freek Van de Herten](https://github.com/freekmurze)
156
- [All Contributors](../../contributors)
157
 
158
## License
159
 
160
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.