File tree Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,10 @@ public function __construct(ServerTiming $timing)
24
24
25
25
public function handle (Request $ request , Closure $ next )
26
26
{
27
+ if (false === config ('timing.enabled ' , true )) {
28
+ return $ next ($ request );
29
+ }
30
+
27
31
$ this ->timing ->setDuration ('Bootstrap ' , $ this ->getElapsedTimeInMs ());
28
32
29
33
$ this ->timing ->start ('App ' );
Original file line number Diff line number Diff line change 6
6
7
7
class ServerTimingServiceProvider extends ServiceProvider
8
8
{
9
+ /**
10
+ * Bootstrap the application services.
11
+ */
12
+ public function boot ()
13
+ {
14
+ if ($ this ->app ->runningInConsole ()) {
15
+ $ this ->registerPublishing ();
16
+ }
17
+ }
18
+
9
19
/**
10
20
* Register the application services.
11
21
*/
@@ -15,4 +25,11 @@ public function register()
15
25
return new ServerTiming (new \Symfony \Component \Stopwatch \Stopwatch ());
16
26
});
17
27
}
28
+
29
+ protected function registerPublishing ()
30
+ {
31
+ $ this ->publishes ([
32
+ __DIR__ .'/../config/config.php ' => config_path ('timing.php ' ),
33
+ ], 'server-timing-config ' );
34
+ }
18
35
}
Original file line number Diff line number Diff line change
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
+ ];
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments