Skip to content

Commit c831b42

Browse files
guandeng10951huangdijia
authored
Added Telescope recording and pause functions (#678)
* Support telescope ability * Support recording * Optimizing the code * feat: Refactor telescope recording and caching logic * chore: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php * feat: Update EntriesController to use ApplicationInterface for clearing Telescope entries * feat: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php * chore: Update Psr\Container\ContainerInterface::get() override in .phpstorm.meta.php * feat: Update EntriesController to use ApplicationInterface for clearing Telescope entries * Update Telescope::getCache() to return PsrCacheInterface instead of CacheInterface * Added `Telescope::isRecording()` function * Moved `Telescope::isRecording()` condition to `TelescopeConfig isEnable()` * Optimized * Optimized * Update tests * Remove unused codes * Optimize Telescope::isRecording() condition * Optimize Telescope::isRecording() condition * Optimize Telescope::isRecording() condition * Optimize Telescope::isRecording() condition and update caching logic * Optimize Telescope::isRecording() condition and caching logic * Optimize Telescope::isRecording() condition and caching logic * Optimize Telescope::isRecording() condition and update caching logic * chore: Optimize Telescope::isRecording() condition and caching logic * Optimize Telescope::isRecording() condition and caching logic * chore: Optimize Telescope::isRecording() condition and caching logic * Refactor parse recording * Optimize Telescope::isRecording() condition and caching logic * chore: Disable Telescope filter for cache entries with 'telescope:' prefix * Optimized * chore: Optimize caching logic in Telescope::getCache() method * Optimize caching logic in CacheAspect.php * Optimize caching logic in TelescopeConfig.php * chore: Remove SetupTelescopeFilterListener and optimize caching logic in TelescopeConfig.php and CacheAspect.php * Optimize caching logic in CacheAspect.php * Optimize caching logic in CacheAspect.php * Optimize caching logic in CacheAspect.php * Optimize caching logic in CacheAspect.php * chore: Optimize caching logic in TelescopeConfig.php and CacheAspect.php * chore: Optimize caching logic in TelescopeConfig.php and CacheAspect.php * optimize code * optimize code * optimize code --------- Co-authored-by: 10951 <[email protected]> Co-authored-by: Deeka Wong <[email protected]>
1 parent 5d1db60 commit c831b42

16 files changed

+227
-49
lines changed

.phpstorm.meta.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@
1313

1414
// Reflect
1515
override(\FriendsOfHyperf\Helpers\app(0), map(['' => '@']));
16-
override(\FriendsOfHyperf\Helpers\di(0), map(['' => '@']));
16+
override(\FriendsOfHyperf\Helpers\di(0), map([
17+
'' => '@',
18+
\Hyperf\Contract\ApplicationInterface::class => \Symfony\Component\Console\Application::class,
19+
]));
1720
override(\Hyperf\Context\Context::get(0), map(['' => '@']));
1821
override(\Hyperf\Support\make(0), map(['' => '@']));
1922
override(\Hyperf\Support\optional(0), type(0));
2023
override(\Hyperf\Tappable\tap(0), type(0));
21-
override(\Psr\Container\ContainerInterface::get(0), map(['' => '@']));
24+
override(\Psr\Container\ContainerInterface::get(0), map([
25+
'' => '@',
26+
\Hyperf\Contract\ApplicationInterface::class => \Symfony\Component\Console\Application::class,
27+
]));
2228
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::mock(0), map(['' => '@']));
2329
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::swap(0), map(['' => '@']));
2430
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::instance(0), map(['' => '@']));

src/telescope/src/Aspect/CacheAspect.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use FriendsOfHyperf\Telescope\Telescope;
1616
use FriendsOfHyperf\Telescope\TelescopeConfig;
1717
use FriendsOfHyperf\Telescope\TelescopeContext;
18-
use Hyperf\Cache\CacheManager;
1918
use Hyperf\Contract\ConfigInterface;
2019
use Hyperf\Contract\PackerInterface;
2120
use Hyperf\Di\Aop\AbstractAspect;
@@ -30,7 +29,7 @@
3029
class CacheAspect extends AbstractAspect
3130
{
3231
public array $classes = [
33-
CacheManager::class . '::getDriver',
32+
'Hyperf\Cache\CacheManager::getDriver',
3433
'Hyperf\Cache\Driver\*Driver::fetch',
3534
'Hyperf\Cache\Driver\*Driver::get',
3635
'Hyperf\Cache\Driver\*Driver::set',

src/telescope/src/ConfigProvider.php

+19-17
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ public function __invoke(): array
1818
defined('BASE_PATH') or define('BASE_PATH', '');
1919

2020
return [
21-
'commands' => [
22-
Command\ClearCommand::class,
23-
Command\InstallCommand::class,
24-
Command\PruneCommand::class,
25-
],
26-
'listeners' => [
27-
Listener\SetRequestLifecycleListener::class,
28-
Listener\CommandListener::class,
29-
Listener\DbQueryListener::class,
30-
Listener\ExceptionHandlerListener::class,
31-
Listener\SetupTelescopeServerListener::class,
21+
'annotations' => [
22+
'scan' => [
23+
'paths' => [
24+
__DIR__,
25+
],
26+
],
3227
],
3328
'aspects' => [
3429
Aspect\CoroutineAspect::class,
@@ -42,12 +37,19 @@ public function __invoke(): array
4237
Aspect\RequestDispatcherAspect::class,
4338
Aspect\GrpcCoreMiddlewareAspect::class,
4439
],
45-
'annotations' => [
46-
'scan' => [
47-
'paths' => [
48-
__DIR__,
49-
],
50-
],
40+
'commands' => [
41+
Command\ClearCommand::class,
42+
Command\InstallCommand::class,
43+
Command\PruneCommand::class,
44+
],
45+
'dependencies' => [
46+
],
47+
'listeners' => [
48+
Listener\SetRequestLifecycleListener::class,
49+
Listener\CommandListener::class,
50+
Listener\DbQueryListener::class,
51+
Listener\ExceptionHandlerListener::class,
52+
Listener\SetupTelescopeServerListener::class,
5153
],
5254
'publish' => [
5355
[
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of friendsofhyperf/components.
6+
*
7+
* @link https://github.com/friendsofhyperf/components
8+
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
9+
* @contact [email protected]
10+
*/
11+
12+
namespace FriendsOfHyperf\Telescope\Contract;
13+
14+
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
15+
16+
interface CacheInterface extends PsrCacheInterface
17+
{
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of friendsofhyperf/components.
6+
*
7+
* @link https://github.com/friendsofhyperf/components
8+
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
9+
* @contact [email protected]
10+
*/
11+
12+
namespace FriendsOfHyperf\Telescope\Controller;
13+
14+
use Hyperf\Context\ApplicationContext;
15+
use Hyperf\Contract\ApplicationInterface;
16+
use Hyperf\HttpServer\Annotation\Controller;
17+
use Hyperf\HttpServer\Annotation\DeleteMapping;
18+
use Symfony\Component\Console\Input\ArrayInput;
19+
20+
#[Controller(server: 'telescope')]
21+
class EntriesController
22+
{
23+
/**
24+
* Delete all of the entries from storage.
25+
*/
26+
#[DeleteMapping(path: '/telescope/telescope-api/entries')]
27+
public function destroy(): void
28+
{
29+
$application = ApplicationContext::getContainer()->get(ApplicationInterface::class);
30+
$application->setAutoExit(false);
31+
$application->run(
32+
new ArrayInput(['command' => 'telescope:clear'])
33+
);
34+
}
35+
}

src/telescope/src/Controller/EntryController.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use FriendsOfHyperf\Telescope\EntryType;
1515
use FriendsOfHyperf\Telescope\Model\TelescopeEntryModel;
1616
use FriendsOfHyperf\Telescope\Model\TelescopeEntryTagModel;
17+
use FriendsOfHyperf\Telescope\TelescopeConfig;
1718
use Hyperf\Di\Annotation\Inject;
1819
use Hyperf\HttpServer\Contract\RequestInterface;
1920
use Hyperf\HttpServer\Contract\ResponseInterface;
@@ -31,6 +32,9 @@ abstract class EntryController
3132
#[Inject]
3233
protected ResponseInterface $response;
3334

35+
#[Inject]
36+
protected TelescopeConfig $telescopeConfig;
37+
3438
public function index()
3539
{
3640
$before = $this->request->input('before');
@@ -114,11 +118,9 @@ abstract protected function watcher();
114118

115119
/**
116120
* Determine the watcher recording status.
117-
*
118-
* @return string
119121
*/
120-
protected function status()
122+
protected function status(): string
121123
{
122-
return 'enabled';
124+
return $this->telescopeConfig->isRecording() ? 'enabled' : 'paused';
123125
}
124126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of friendsofhyperf/components.
6+
*
7+
* @link https://github.com/friendsofhyperf/components
8+
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
9+
* @contact [email protected]
10+
*/
11+
12+
namespace FriendsOfHyperf\Telescope\Controller;
13+
14+
use FriendsOfHyperf\Telescope\TelescopeConfig;
15+
use Hyperf\Di\Annotation\Inject;
16+
use Hyperf\HttpServer\Annotation\Controller;
17+
use Hyperf\HttpServer\Annotation\PostMapping;
18+
19+
#[Controller(server: 'telescope')]
20+
class RecordingController
21+
{
22+
#[Inject()]
23+
protected TelescopeConfig $telescopeConfig;
24+
25+
/**
26+
* Toggle recording.
27+
*/
28+
#[PostMapping(path: '/telescope/telescope-api/toggle-recording')]
29+
public function toggle(): void
30+
{
31+
$this->telescopeConfig->isRecording() ? $this->telescopeConfig->pauseRecording() : $this->telescopeConfig->continueRecording();
32+
}
33+
}

src/telescope/src/Controller/ViewController.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FriendsOfHyperf\Telescope\Controller;
1313

14+
use FriendsOfHyperf\Telescope\Telescope;
1415
use Hyperf\Di\Annotation\Inject;
1516
use Hyperf\HttpServer\Annotation\Controller;
1617
use Hyperf\HttpServer\Annotation\GetMapping;
@@ -40,8 +41,15 @@ public function index()
4041
if (! isset($this->caches[$blade])) {
4142
$this->caches[$blade] = file_get_contents($blade);
4243
}
44+
$templateContent = $this->caches[$blade];
45+
$params = [
46+
'$telescopeScriptVariables' => json_encode(Telescope::scriptVariables()),
47+
];
48+
foreach ($params as $key => $value) {
49+
$templateContent = str_replace($key, $value, $templateContent);
50+
}
4351

44-
return $this->response->html($this->caches[$blade]);
52+
return $this->response->html($templateContent);
4553
}
4654

4755
#[GetMapping(path: '/telescope/{view}/{id}')]

src/telescope/src/Listener/CommandListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function listen(): array
4040
*/
4141
public function process(object $event): void
4242
{
43-
if ($this->telescopeConfig->isEnable('command') === false) {
43+
if (! $this->telescopeConfig->isEnable('command')) {
4444
return;
4545
}
4646

src/telescope/src/Listener/DbQueryListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function listen(): array
3737
*/
3838
public function process(object $event): void
3939
{
40-
if ($this->telescopeConfig->isEnable('db') === false) {
40+
if (! $this->telescopeConfig->isEnable('db')) {
4141
return;
4242
}
4343
if ($event instanceof QueryExecuted) {

src/telescope/src/Listener/ExceptionHandlerListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function listen(): array
3939
*/
4040
public function process(object $event): void
4141
{
42-
if ($this->telescopeConfig->isEnable('exception') === false) {
42+
if (! $this->telescopeConfig->isEnable('exception')) {
4343
return;
4444
}
4545

src/telescope/src/Listener/RequestHandledListener.php

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function process(object $event): void
5757
if (! $this->telescopeConfig->isEnable('request')) {
5858
return;
5959
}
60+
6061
match ($event::class) {
6162
RequestReceived::class, RpcRequestReceived::class => $this->requestReceived($event),
6263
RequestTerminated::class, RpcRequestTerminated::class => $this->requestHandled($event),

src/telescope/src/Telescope.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Hyperf\Collection\Arr;
1616
use Hyperf\Context\ApplicationContext;
1717

18-
use function Hyperf\Config\config;
19-
2018
class Telescope
2119
{
2220
public const SYNC = 0;
@@ -54,11 +52,6 @@ class Telescope
5452
*/
5553
public static bool $useDarkTheme = false;
5654

57-
/**
58-
* Indicates if Telescope should record entries.
59-
*/
60-
public static bool $shouldRecord = false;
61-
6255
/**
6356
* Indicates if Telescope migrations will be run.
6457
*/
@@ -145,10 +138,8 @@ public static function getPath(): string
145138

146139
/**
147140
* Add a callback that adds tags to the record.
148-
*
149-
* @return static
150141
*/
151-
public static function tag(Closure $callback)
142+
public static function tag(Closure $callback): static
152143
{
153144
static::$tagUsing[] = $callback;
154145

@@ -157,10 +148,8 @@ public static function tag(Closure $callback)
157148

158149
/**
159150
* Set the callback that filters the entries that should be recorded.
160-
*
161-
* @return static
162151
*/
163-
public static function filter(Closure $callback)
152+
public static function filter(Closure $callback): static
164153
{
165154
static::$filterUsing[] = $callback;
166155

@@ -172,6 +161,18 @@ public static function getConfig(): TelescopeConfig
172161
return ApplicationContext::getContainer()->get(TelescopeConfig::class);
173162
}
174163

164+
/**
165+
* Get the default JavaScript variables for Telescope.
166+
*/
167+
public static function scriptVariables(): array
168+
{
169+
return [
170+
'path' => static::getConfig()->getPath(),
171+
'timezone' => static::getConfig()->getTimezone(),
172+
'recording' => static::getConfig()->isRecording(),
173+
];
174+
}
175+
175176
/**
176177
* Determine if the given entry should be recorded.
177178
*/
@@ -199,7 +200,7 @@ protected static function record(string $type, IncomingEntry $entry): void
199200
return $tagCallback($entry);
200201
}, static::$tagUsing)));
201202

202-
match (config('telescope.save_mode', 0)) {
203+
match (static::getConfig()->getSaveMode()) {
203204
self::ASYNC => TelescopeContext::addEntry($entry),
204205
self::SYNC => $entry->create(),
205206
default => $entry->create(),

0 commit comments

Comments
 (0)