Skip to content

Commit 9ed7207

Browse files
authored
Merge branch 'master' into adamhopkinson-use-env
2 parents b6e0ad7 + 79d67e2 commit 9ed7207

File tree

6 files changed

+54
-56
lines changed

6 files changed

+54
-56
lines changed

.scrutinizer.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Laravel Server Timings
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-server-timing.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-server-timing)
4-
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-server-timing/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-server-timing)
5-
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-server-timing.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-server-timing)
64
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-server-timing.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-server-timing)
75

86
Add Server-Timing header information from within your Laravel apps.
@@ -20,6 +18,29 @@ composer require beyondcode/laravel-server-timing
2018
To add server-timing header information, you need to add the `\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class,` middleware to your HTTP Kernel.
2119
In order to get the most accurate results, put the middleware as the first one to load in the middleware stack.
2220

21+
### Laravel 11
22+
`bootstrap/app.php`
23+
```php
24+
return Application::configure(basePath: dirname(__DIR__))
25+
// ...
26+
->withMiddleware(function (Middleware $middleware) {
27+
$middleware->prepend(\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class);
28+
})
29+
// ...
30+
->create();
31+
```
32+
33+
### Laravel 10 and below
34+
`app/Http/Kernel.php`
35+
```php
36+
class Kernel extends HttpKernel
37+
{
38+
protected $middleware = [
39+
\BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware::class,
40+
// ...
41+
];
42+
```
43+
2344
By default, the middleware measures only three things, to keep it as light-weight as possible:
2445

2546
- Bootstrap (time before the middleware gets called)
@@ -35,9 +56,12 @@ Once the package is successfully installed, you can see your timing information
3556
If you want to provide additional measurements, you can use the start and stop methods. If you do not explicitly stop a measured event, the event will automatically be stopped once the middleware receives your response. This can be useful if you want to measure the time your Blade views take to compile.
3657

3758
```php
59+
use BeyondCode\ServerTiming\Facades\ServerTiming;
60+
3861
ServerTiming::start('Running expensive task');
3962

40-
// do something
63+
// Take a nap
64+
sleep(5);
4165

4266
ServerTiming::stop('Running expensive task');
4367
```
@@ -61,19 +85,16 @@ ServerTiming::setDuration('Running expensive task', function() {
6185

6286
You can also use the Server-Timing middleware to only set textual information without providing a duration.
6387

64-
## Disabling
65-
To disable, add `SERVER_TIMING=false` to your `.env` file.
88+
```php
89+
ServerTiming::addMetric('User: '.$user->id);
90+
```
6691

6792
## Publishing configuration file
6893

6994
The configuration file could be published using:
7095
`php artisan vendor:publish --tag=server-timing-config`
7196

72-
You can disable the middleware changing the `timing.enabled` configuration to false.
73-
74-
```php
75-
ServerTiming::addMetric('User: '.$user->id);
76-
```
97+
You can disable the middleware by changing the `timing.enabled` configuration to false or adding `SERVER_TIMING_ENABLED=false` to your `.env` file.
7798

7899
### Testing
79100

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
],
1818
"require": {
1919
"php": "^7.2|^8.0",
20-
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0",
21-
"symfony/stopwatch": "^4.0|^5.0"
20+
"illuminate/support": "5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
21+
"symfony/stopwatch": "^4.0|^5.0|^6.0|^7.0"
2222
},
2323
"require-dev": {
24-
"orchestra/testbench": "^4.6|^5.0|^6.0",
24+
"orchestra/testbench": "^4.6|^5.0|^6.0|^8.0",
2525
"phpunit/phpunit": "^8.0|^9.0"
2626
},
2727
"autoload": {

src/Facades/ServerTiming.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44

55
use Illuminate\Support\Facades\Facade;
66

7+
/**
8+
* @method static \BeyondCode\ServerTiming\ServerTiming start(string $key) Start a unique timed event.
9+
* @method static \BeyondCode\ServerTiming\ServerTiming addMetric(string $metric) Add new event with null duration.
10+
* @method static bool hasStartedEvent(string $key) Check if a event has been created already.
11+
* @method static \BeyondCode\ServerTiming\ServerTiming measure(string $key) Stop existing event and record its duration, else start a new event.
12+
* @method static \BeyondCode\ServerTiming\ServerTiming stop(string $key) Stop a timed event and record its duration.
13+
* @method static void stopAllUnfinishedEvents() Stop all running events.
14+
* @method static \BeyondCode\ServerTiming\ServerTiming setDuration(string $key, float|int|callable $duration) Set the duration for an event if $duration is number, else record elapsed time to run a user function if $duration is callable.
15+
* @method static float|int|null getDuration(string $key) Retrieve the duration an event has taken.
16+
* @method static array events() Get the list of finished events with their associated duration.
17+
*
18+
* @see \BeyondCode\ServerTiming\ServerTiming
19+
*/
720
class ServerTiming extends Facade
821
{
22+
/**
23+
* Get the registered name of the component.
24+
*
25+
* @return string
26+
*/
927
protected static function getFacadeAccessor()
1028
{
1129
return \BeyondCode\ServerTiming\ServerTiming::class;

src/Middleware/ServerTimingMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ protected function generateHeaders(): string
6666
foreach ($this->timing->events() as $eventName => $duration) {
6767
$eventNameSlug = Str::slug($eventName);
6868

69-
$header .= "${eventNameSlug};desc=\"${eventName}\";";
69+
$header .= "{$eventNameSlug};desc=\"{$eventName}\";";
7070

7171
if (!is_null($duration)) {
72-
$header .= "dur=${duration}";
72+
$header .= "dur={$duration}";
7373
}
7474

7575
$header .= ", ";

0 commit comments

Comments
 (0)