Skip to content

Commit 8916481

Browse files
authored
Merge pull request #3 from binaryk/config
Adding configuration to enable middleware.
2 parents 56009b5 + 3f44421 commit 8916481

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ ServerTiming::setDuration('Running expensive task', function() {
6161

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

64+
## Publishing configuration file
65+
66+
The configuration file could be published using:
67+
`php artisan vendor:publish --tag=server-timing-config`
68+
69+
You can disable the middleware changing the `timing.enabled` configuration to false.
70+
6471
```php
6572
ServerTiming::addMetric('User: '.$user->id);
6673
```

src/Middleware/ServerTimingMiddleware.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public function __construct(ServerTiming $timing)
2424

2525
public function handle(Request $request, Closure $next)
2626
{
27+
if(false === config('timing.enabled', true)) {
28+
return $next($request);
29+
}
30+
2731
$this->timing->setDuration('Bootstrap', $this->getElapsedTimeInMs());
2832

2933
$this->timing->start('App');

src/ServerTimingServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class ServerTimingServiceProvider extends ServiceProvider
88
{
9+
/**
10+
* Bootstrap the application services.
11+
*/
12+
public function boot()
13+
{
14+
if ($this->app->runningInConsole()) {
15+
$this->registerPublishing();
16+
}
17+
}
18+
919
/**
1020
* Register the application services.
1121
*/
@@ -15,4 +25,11 @@ public function register()
1525
return new ServerTiming(new \Symfony\Component\Stopwatch\Stopwatch());
1626
});
1727
}
28+
29+
protected function registerPublishing()
30+
{
31+
$this->publishes([
32+
__DIR__.'/config/config.php' => config_path('timing.php'),
33+
], 'server-timing-config');
34+
}
1835
}

src/config/config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Laravel Server Timing enabled
7+
|--------------------------------------------------------------------------
8+
|
9+
| This configuration is used to enable the server timing measurement,
10+
| if set to false, the middleware will be bypassed
11+
|
12+
*/
13+
14+
'enabled' => true,
15+
];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace BeyondCode\ServerTiming\Tests\Middleware;
4+
5+
use BeyondCode\ServerTiming\Middleware\ServerTimingMiddleware;
6+
use BeyondCode\ServerTiming\ServerTiming;
7+
use Illuminate\Http\Request;
8+
use Orchestra\Testbench\TestCase;
9+
use Symfony\Component\Stopwatch\Stopwatch;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class ServerTimingMiddlewareTest extends TestCase
13+
{
14+
/** @test */
15+
public function it_add_server_timing_header()
16+
{
17+
$request = new Request;
18+
19+
$timing = new ServerTiming(new Stopwatch());
20+
21+
$middleware = new ServerTimingMiddleware($timing);
22+
23+
$response = $middleware->handle($request, function ($req) {
24+
return new Response();
25+
});
26+
27+
$this->assertArrayHasKey('server-timing', $response->headers->all());
28+
29+
}
30+
31+
/** @test */
32+
public function it_is_bypassed_if_configuration_false()
33+
{
34+
$this->app['config']->set('timing.enabled', false);
35+
36+
$request = new Request;
37+
38+
$timing = new ServerTiming(new Stopwatch());
39+
40+
$middleware = new ServerTimingMiddleware($timing);
41+
42+
$response = $middleware->handle($request, function ($req) {
43+
return new Response();
44+
});
45+
46+
$this->assertArrayNotHasKey('server-timing', $response->headers->all());
47+
}
48+
}

0 commit comments

Comments
 (0)