Skip to content

Commit ae3d009

Browse files
committed
Add fix for laravel octance
Don't use imports for octane setup Add return types wip wip
1 parent 61a6d0c commit ae3d009

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/Middleware/ServerTimingMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace BeyondCode\ServerTiming\Middleware;
44

55
use Closure;
6-
use Illuminate\Http\Request;
76
use BeyondCode\ServerTiming\ServerTiming;
7+
use Illuminate\Http\Request;
88
use Illuminate\Support\Str;
99
use Symfony\Component\HttpFoundation\Response;
1010

src/ServerTiming.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,11 @@ public function events(): array
104104
return $this->finishedEvents;
105105
}
106106

107+
public function reset(Stopwatch $stopwatch): void
108+
{
109+
$this->finishedEvents = [];
110+
$this->startedEvents = [];
111+
$this->stopwatch = $stopwatch;
112+
}
113+
107114
}

src/ServerTimingServiceProvider.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function boot(): void
1414
if ($this->app->runningInConsole()) {
1515
$this->registerPublishing();
1616
}
17+
18+
if (isset($_SERVER['LARAVEL_OCTANE'])) {
19+
$this->setupOctane();
20+
}
1721
}
1822

1923
/**
@@ -32,4 +36,31 @@ protected function registerPublishing(): void
3236
__DIR__.'/config/config.php' => config_path('timing.php'),
3337
], 'server-timing-config');
3438
}
39+
40+
protected function resetServerTiming(): void
41+
{
42+
/**
43+
* @var ServerTiming $serverTiming
44+
*/
45+
$serverTiming = $this->app->get(ServerTiming::class);
46+
$serverTiming->reset(new \Symfony\Component\Stopwatch\Stopwatch());
47+
}
48+
49+
protected function setupOctane(): void
50+
{
51+
/** @phpstan-ignore-next-line */
52+
$this->app['events']->listen(\Laravel\Octane\Events\RequestReceived::class, function () {
53+
$this->resetServerTiming();
54+
});
55+
56+
/** @phpstan-ignore-next-line */
57+
$this->app['events']->listen(\Laravel\Octane\Events\TaskReceived::class, function () {
58+
$this->resetServerTiming();
59+
});
60+
61+
/** @phpstan-ignore-next-line */
62+
$this->app['events']->listen(\Laravel\Octane\Events\TickReceived::class, function () {
63+
$this->resetServerTiming();
64+
});
65+
}
3566
}

0 commit comments

Comments
 (0)