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

[3.x] Add afterStore hooks #894

Merged
merged 4 commits into from
May 11, 2020
Merged
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: 36 additions & 12 deletions src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class Telescope
*/
public static $afterRecordingHook;

/**
* The callbacks executed after storing the entries.
*
* @var \Closure
*/
public static $afterStoreHooks = [];

/**
* The callbacks that add tags to the record.
*
Expand Down Expand Up @@ -534,6 +541,19 @@ public static function afterRecording(Closure $callback)
return new static;
}

/**
* Set the callback that will be executed after an entry is stored.
*
* @param \Closure $callback
* @return static
*/
public static function afterStore(Closure $callback)
{
static::$afterStoreHooks[] = $callback;

return new static;
}

/**
* Add a callback that adds tags to the record.
*
Expand All @@ -559,22 +579,26 @@ public static function store(EntriesRepository $storage)
return;
}

if (! collect(static::$filterBatchUsing)->every->__invoke(collect(static::$entriesQueue))) {
static::flushEntries();
}
static::withoutRecording(function () use ($storage) {
if (! collect(static::$filterBatchUsing)->every->__invoke(collect(static::$entriesQueue))) {
static::flushEntries();
}

try {
$batchId = Str::orderedUuid()->toString();
try {
$batchId = Str::orderedUuid()->toString();

$storage->store(static::collectEntries($batchId));
$storage->update(static::collectUpdates($batchId));

$storage->store(static::collectEntries($batchId));
$storage->update(static::collectUpdates($batchId));
if ($storage instanceof TerminableRepository) {
$storage->terminate();
}

if ($storage instanceof TerminableRepository) {
$storage->terminate();
collect(static::$afterStoreHooks)->every->__invoke(static::$entriesQueue, $batchId);
} catch (Exception $e) {
app(ExceptionHandler::class)->report($e);
}
} catch (Exception $e) {
app(ExceptionHandler::class)->report($e);
}
});

static::$entriesQueue = [];
static::$updatesQueue = [];
Expand Down
34 changes: 32 additions & 2 deletions tests/Telescope/TelescopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Telescope\Contracts\EntriesRepository;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\Tests\FeatureTestCase;
use Laravel\Telescope\Watchers\QueryWatcher;
Expand Down Expand Up @@ -37,7 +38,7 @@ protected function tearDown(): void
*/
public function run_after_recording_callback()
{
Telescope::afterRecording(function () {
Telescope::afterRecording(function (Telescope $telescope, IncomingEntry $entry) {
$this->count++;
});

Expand All @@ -53,7 +54,7 @@ public function run_after_recording_callback()
*/
public function after_recording_callback_can_store_and_flush()
{
Telescope::afterRecording(function (Telescope $telescope) {
Telescope::afterRecording(function (Telescope $telescope, IncomingEntry $entry) {
if (count($telescope::$entriesQueue) > 1) {
$repository = $this->app->make(EntriesRepository::class);
$telescope->store($repository);
Expand All @@ -73,6 +74,35 @@ public function after_recording_callback_can_store_and_flush()
$this->assertCount(1, Telescope::$entriesQueue);
}

/**
* @test
*/
public function run_after_store_callback()
{
$storedEntries = null;
$storedBatchId = null;
Telescope::afterStore(function (array $entries, $batchId) use (&$storedEntries, &$storedBatchId) {
$storedEntries = $entries;
$storedBatchId = $batchId;

$this->count += count($entries);
});

$this->app->get('db')->table('telescope_entries')->count();

$this->app->get('db')->table('telescope_entries')->count();

$this->assertSame(0, $this->count);

$repository = $this->app->make(EntriesRepository::class);
Telescope::store($repository);

$this->assertSame(2, $this->count);
$this->assertSame(2, count($storedEntries));
$this->assertSame(36, strlen($storedBatchId));
$this->assertInstanceOf(IncomingEntry::class, $storedEntries[0]);
}

/**
* @test
*/
Expand Down