Skip to content

Commit a7ee3d4

Browse files
authored
Integration with Laravel Telescope implemented (#54)
1 parent 54d86c4 commit a7ee3d4

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## UNRELEASED
8+
9+
### Added
10+
11+
- Integration with [Laravel Telescope](https://github.com/laravel/telescope/) is supported now (just enable `SetupTelescopeListener` for `BeforeLoopStartedEvent`)
12+
713
## v5.1.0
814

915
### Added

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"laravel/laravel": "~6.0 || ~7.0 || ~8.0",
4747
"laravel/scout": "~8.0 || ~9.0",
4848
"laravel/socialite": "^5.0",
49+
"laravel/telescope": "^4.5",
4950
"mockery/mockery": "^1.3.2",
5051
"phpstan/phpstan": "~0.12.80",
5152
"phpunit/phpunit": "^8.0 || ^9.3"

config/roadrunner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'listeners' => [
3333
Events\BeforeLoopStartedEvent::class => [
3434
...Defaults::beforeLoopStarted(),
35+
// Listeners\SetupTelescopeListener::class, // for <https://github.com/laravel/telescope>
3536
],
3637

3738
Events\BeforeLoopIterationEvent::class => [
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Illuminate\Support\Str;
8+
use Laravel\Telescope\{Telescope, EntryType, IncomingEntry};
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
11+
12+
/**
13+
* Target package: <https://github.com/laravel/telescope>.
14+
*/
15+
class SetupTelescopeListener implements ListenerInterface
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function handle($event): void
21+
{
22+
if ($event instanceof WithApplication) {
23+
/** @var ConfigRepository $config */
24+
$config = $event->application()->make(ConfigRepository::class);
25+
26+
if (!\class_exists(Telescope::class) || !$config->get('telescope.enabled')) {
27+
return;
28+
}
29+
30+
Telescope::filter(static function (IncomingEntry $entry): bool {
31+
switch ($entry->type) {
32+
case EntryType::EVENT:
33+
if (Str::startsWith($entry->content['name'] ?? '', 'Spiral\\RoadRunnerLaravel\\')) {
34+
return false;
35+
}
36+
37+
break;
38+
39+
case EntryType::REQUEST:
40+
if (Str::startsWith($entry->content['controller_action'] ?? '', 'Laravel\\Telescope\\')) {
41+
return false;
42+
}
43+
44+
break;
45+
46+
case EntryType::VIEW:
47+
if (Str::startsWith($entry->content['name'] ?? '', 'telescope::')) {
48+
return false;
49+
}
50+
51+
break;
52+
53+
case EntryType::REDIS:
54+
$cmd = $entry->content['command'] ?? '';
55+
56+
if (Str::contains($cmd, ['telescope:pause-recording', 'telescope:dump-watcher'])) {
57+
return false;
58+
}
59+
60+
break;
61+
}
62+
63+
return true;
64+
});
65+
}
66+
}
67+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Laravel\Telescope\Telescope;
9+
use Laravel\Telescope\IncomingEntry;
10+
use Spiral\RoadRunnerLaravel\Listeners\SetupTelescopeListener;
11+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
12+
13+
/**
14+
* @covers \Spiral\RoadRunnerLaravel\Listeners\SetupTelescopeListener
15+
*/
16+
class SetupTelescopeListenerTest extends AbstractListenerTestCase
17+
{
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function testHandle(): void
32+
{
33+
/** @var m\MockInterface|WithApplication $event_mock */
34+
$event_mock = m::mock(WithApplication::class)
35+
->makePartial()
36+
->expects('application')
37+
->andReturn($this->app)
38+
->getMock();
39+
40+
$this->assertEmpty(Telescope::$entriesQueue);
41+
Telescope::recordEvent($event = new IncomingEntry(['name' => 'Spiral\\RoadRunnerLaravel\\']));
42+
Telescope::recordRequest($request = new IncomingEntry(['controller_action' => 'Laravel\\Telescope\\']));
43+
Telescope::recordView($view = new IncomingEntry(['name' => 'telescope::foo']));
44+
Telescope::recordRedis($redis1 = new IncomingEntry(['command' => 'get cache:telescope:pause-recording']));
45+
Telescope::recordRedis($redis2 = new IncomingEntry(['command' => 'get cache:telescope:dump-watcher']));
46+
$this->assertCount(5, Telescope::$entriesQueue);
47+
48+
Telescope::flushEntries();
49+
50+
$this->listenerFactory()->handle($event_mock);
51+
52+
$this->assertEmpty(Telescope::$entriesQueue);
53+
Telescope::recordEvent($event);
54+
Telescope::recordRequest($request);
55+
Telescope::recordView($view);
56+
Telescope::recordRedis($redis1);
57+
Telescope::recordRedis($redis2);
58+
$this->assertEmpty(Telescope::$entriesQueue);
59+
60+
Telescope::recordBatch($any_another_entry = new IncomingEntry([]));
61+
Telescope::recordCache($any_another_entry);
62+
Telescope::recordCommand($any_another_entry);
63+
Telescope::recordDump($any_another_entry);
64+
Telescope::recordEvent($any_another_entry);
65+
Telescope::recordException($any_another_entry);
66+
Telescope::recordGate($any_another_entry);
67+
Telescope::recordJob($any_another_entry);
68+
Telescope::recordLog($any_another_entry);
69+
Telescope::recordMail($any_another_entry);
70+
Telescope::recordNotification($any_another_entry);
71+
Telescope::recordQuery($any_another_entry);
72+
Telescope::recordModelEvent($any_another_entry);
73+
Telescope::recordRedis($any_another_entry);
74+
Telescope::recordRequest($any_another_entry);
75+
Telescope::recordScheduledCommand($any_another_entry);
76+
Telescope::recordView($any_another_entry);
77+
Telescope::recordClientRequest($any_another_entry);
78+
$this->assertCount(18, Telescope::$entriesQueue);
79+
}
80+
81+
/**
82+
* @return SetupTelescopeListener
83+
*/
84+
protected function listenerFactory(): SetupTelescopeListener
85+
{
86+
return new SetupTelescopeListener();
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
protected function tearDown(): void
93+
{
94+
Telescope::flushEntries();
95+
96+
parent::tearDown();
97+
}
98+
}

0 commit comments

Comments
 (0)