Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recording control middleware #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Http/Middleware/PulseRecording.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Laravel\Pulse\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Pulse\Pulse;

class PulseRecording
{
/**
* Create a new middleware instance.
*/
public function __construct(protected Pulse $pulse)
{
//
}

/**
* Start recording.
*/
public static function start(): string
{
return static::class.':start,'.Str::random();
}

/**
* Stop recording.
*/
public static function stop(): string
{
return static::class.':stop,'.Str::random();
}

/**
* Handle the incoming request.
*/
public function handle(Request $request, Closure $next, string $action): mixed
{
match ($action) {
'start' => $this->pulse->startRecording(),
'stop' => $this->pulse->stopRecording(),
};

return $next($request);
}
}
6 changes: 0 additions & 6 deletions src/Pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,6 @@ public function filter(callable $filter): self
*/
public function store(Ingest $ingest): self
{
if (! $this->shouldRecord) {
$this->rememberedUserId = null;

return $this->flushEntries();
}

$this->rescue(fn () => $ingest->ingest(
$this->entries->map->resolve()->filter($this->shouldRecord(...)),
));
Expand Down
43 changes: 43 additions & 0 deletions tests/Feature/RecordingControlMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Laravel\Pulse\Http\Middleware\PulseRecording;

use function Pest\Laravel\get;

it('can stop control recording via middleware', function () {
Route::get('test-route', fn () => 'ok')->middleware([
MyMiddleware::class.':first',
PulseRecording::stop(),
MyMiddleware::class.':second',
PulseRecording::start(),
MyMiddleware::class.':third',
PulseRecording::stop(),
MyMiddleware::class.':fourth',
PulseRecording::start(),
MyMiddleware::class.':fifth',
PulseRecording::stop(),
MyMiddleware::class.':sixth',
]);

$response = get('test-route');

$response->assertOk();
$interactions = Pulse::ignore(fn () => DB::table('pulse_cache_interactions')->get());
expect($interactions->pluck('key')->all())->toBe([
'first',
'third',
'fifth',
]);
});

class MyMiddleware
{
public function handle($request, $next, $key)
{
Cache::get($key);

return $next($request);
}
}