Skip to content

Commit

Permalink
Added Telescope recording and pause functions (#678)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jun 23, 2024
1 parent 5d1db60 commit c831b42
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 49 deletions.
10 changes: 8 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@

// Reflect
override(\FriendsOfHyperf\Helpers\app(0), map(['' => '@']));
override(\FriendsOfHyperf\Helpers\di(0), map(['' => '@']));
override(\FriendsOfHyperf\Helpers\di(0), map([
'' => '@',
\Hyperf\Contract\ApplicationInterface::class => \Symfony\Component\Console\Application::class,
]));
override(\Hyperf\Context\Context::get(0), map(['' => '@']));
override(\Hyperf\Support\make(0), map(['' => '@']));
override(\Hyperf\Support\optional(0), type(0));
override(\Hyperf\Tappable\tap(0), type(0));
override(\Psr\Container\ContainerInterface::get(0), map(['' => '@']));
override(\Psr\Container\ContainerInterface::get(0), map([
'' => '@',
\Hyperf\Contract\ApplicationInterface::class => \Symfony\Component\Console\Application::class,
]));
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::mock(0), map(['' => '@']));
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::swap(0), map(['' => '@']));
override(\FriendsOfHyperf\Tests\Concerns\InteractsWithContainer::instance(0), map(['' => '@']));
Expand Down
3 changes: 1 addition & 2 deletions src/telescope/src/Aspect/CacheAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use FriendsOfHyperf\Telescope\Telescope;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use FriendsOfHyperf\Telescope\TelescopeContext;
use Hyperf\Cache\CacheManager;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PackerInterface;
use Hyperf\Di\Aop\AbstractAspect;
Expand All @@ -30,7 +29,7 @@
class CacheAspect extends AbstractAspect
{
public array $classes = [
CacheManager::class . '::getDriver',
'Hyperf\Cache\CacheManager::getDriver',
'Hyperf\Cache\Driver\*Driver::fetch',
'Hyperf\Cache\Driver\*Driver::get',
'Hyperf\Cache\Driver\*Driver::set',
Expand Down
36 changes: 19 additions & 17 deletions src/telescope/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ public function __invoke(): array
defined('BASE_PATH') or define('BASE_PATH', '');

return [
'commands' => [
Command\ClearCommand::class,
Command\InstallCommand::class,
Command\PruneCommand::class,
],
'listeners' => [
Listener\SetRequestLifecycleListener::class,
Listener\CommandListener::class,
Listener\DbQueryListener::class,
Listener\ExceptionHandlerListener::class,
Listener\SetupTelescopeServerListener::class,
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
],
'aspects' => [
Aspect\CoroutineAspect::class,
Expand All @@ -42,12 +37,19 @@ public function __invoke(): array
Aspect\RequestDispatcherAspect::class,
Aspect\GrpcCoreMiddlewareAspect::class,
],
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
'commands' => [
Command\ClearCommand::class,
Command\InstallCommand::class,
Command\PruneCommand::class,
],
'dependencies' => [
],
'listeners' => [
Listener\SetRequestLifecycleListener::class,
Listener\CommandListener::class,
Listener\DbQueryListener::class,
Listener\ExceptionHandlerListener::class,
Listener\SetupTelescopeServerListener::class,
],
'publish' => [
[
Expand Down
18 changes: 18 additions & 0 deletions src/telescope/src/Contract/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Contract;

use Psr\SimpleCache\CacheInterface as PsrCacheInterface;

interface CacheInterface extends PsrCacheInterface
{
}
35 changes: 35 additions & 0 deletions src/telescope/src/Controller/EntriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Controller;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ApplicationInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Symfony\Component\Console\Input\ArrayInput;

#[Controller(server: 'telescope')]
class EntriesController
{
/**
* Delete all of the entries from storage.
*/
#[DeleteMapping(path: '/telescope/telescope-api/entries')]
public function destroy(): void
{
$application = ApplicationContext::getContainer()->get(ApplicationInterface::class);
$application->setAutoExit(false);
$application->run(
new ArrayInput(['command' => 'telescope:clear'])
);
}
}
10 changes: 6 additions & 4 deletions src/telescope/src/Controller/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FriendsOfHyperf\Telescope\EntryType;
use FriendsOfHyperf\Telescope\Model\TelescopeEntryModel;
use FriendsOfHyperf\Telescope\Model\TelescopeEntryTagModel;
use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
Expand All @@ -31,6 +32,9 @@ abstract class EntryController
#[Inject]
protected ResponseInterface $response;

#[Inject]
protected TelescopeConfig $telescopeConfig;

public function index()
{
$before = $this->request->input('before');
Expand Down Expand Up @@ -114,11 +118,9 @@ abstract protected function watcher();

/**
* Determine the watcher recording status.
*
* @return string
*/
protected function status()
protected function status(): string
{
return 'enabled';
return $this->telescopeConfig->isRecording() ? 'enabled' : 'paused';
}
}
33 changes: 33 additions & 0 deletions src/telescope/src/Controller/RecordingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Telescope\Controller;

use FriendsOfHyperf\Telescope\TelescopeConfig;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(server: 'telescope')]
class RecordingController
{
#[Inject()]
protected TelescopeConfig $telescopeConfig;

/**
* Toggle recording.
*/
#[PostMapping(path: '/telescope/telescope-api/toggle-recording')]
public function toggle(): void
{
$this->telescopeConfig->isRecording() ? $this->telescopeConfig->pauseRecording() : $this->telescopeConfig->continueRecording();
}
}
10 changes: 9 additions & 1 deletion src/telescope/src/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FriendsOfHyperf\Telescope\Controller;

use FriendsOfHyperf\Telescope\Telescope;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
Expand Down Expand Up @@ -40,8 +41,15 @@ public function index()
if (! isset($this->caches[$blade])) {
$this->caches[$blade] = file_get_contents($blade);
}
$templateContent = $this->caches[$blade];
$params = [
'$telescopeScriptVariables' => json_encode(Telescope::scriptVariables()),
];
foreach ($params as $key => $value) {
$templateContent = str_replace($key, $value, $templateContent);
}

return $this->response->html($this->caches[$blade]);
return $this->response->html($templateContent);
}

#[GetMapping(path: '/telescope/{view}/{id}')]
Expand Down
2 changes: 1 addition & 1 deletion src/telescope/src/Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('command') === false) {
if (! $this->telescopeConfig->isEnable('command')) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/telescope/src/Listener/DbQueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('db') === false) {
if (! $this->telescopeConfig->isEnable('db')) {
return;
}
if ($event instanceof QueryExecuted) {
Expand Down
2 changes: 1 addition & 1 deletion src/telescope/src/Listener/ExceptionHandlerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->telescopeConfig->isEnable('exception') === false) {
if (! $this->telescopeConfig->isEnable('exception')) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/telescope/src/Listener/RequestHandledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function process(object $event): void
if (! $this->telescopeConfig->isEnable('request')) {
return;
}

match ($event::class) {
RequestReceived::class, RpcRequestReceived::class => $this->requestReceived($event),
RequestTerminated::class, RpcRequestTerminated::class => $this->requestHandled($event),
Expand Down
29 changes: 15 additions & 14 deletions src/telescope/src/Telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Hyperf\Collection\Arr;
use Hyperf\Context\ApplicationContext;

use function Hyperf\Config\config;

class Telescope
{
public const SYNC = 0;
Expand Down Expand Up @@ -54,11 +52,6 @@ class Telescope
*/
public static bool $useDarkTheme = false;

/**
* Indicates if Telescope should record entries.
*/
public static bool $shouldRecord = false;

/**
* Indicates if Telescope migrations will be run.
*/
Expand Down Expand Up @@ -145,10 +138,8 @@ public static function getPath(): string

/**
* Add a callback that adds tags to the record.
*
* @return static
*/
public static function tag(Closure $callback)
public static function tag(Closure $callback): static
{
static::$tagUsing[] = $callback;

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

/**
* Set the callback that filters the entries that should be recorded.
*
* @return static
*/
public static function filter(Closure $callback)
public static function filter(Closure $callback): static
{
static::$filterUsing[] = $callback;

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

/**
* Get the default JavaScript variables for Telescope.
*/
public static function scriptVariables(): array
{
return [
'path' => static::getConfig()->getPath(),
'timezone' => static::getConfig()->getTimezone(),
'recording' => static::getConfig()->isRecording(),
];
}

/**
* Determine if the given entry should be recorded.
*/
Expand Down Expand Up @@ -199,7 +200,7 @@ protected static function record(string $type, IncomingEntry $entry): void
return $tagCallback($entry);
}, static::$tagUsing)));

match (config('telescope.save_mode', 0)) {
match (static::getConfig()->getSaveMode()) {
self::ASYNC => TelescopeContext::addEntry($entry),
self::SYNC => $entry->create(),
default => $entry->create(),
Expand Down
Loading

0 comments on commit c831b42

Please sign in to comment.