Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 621 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

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