From bfbfa99a005d4193ea2dd3af60f3607c8dce8917 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Thu, 21 Dec 2023 22:52:00 +0800 Subject: [PATCH] Refactor Telescope configuration classes (#505) * Refactor Telescope configuration classes * Refactor request handling and server setup in Telescope * Update Telescope configuration and usage * Remove unused import and empty line in LogAspect.php and EntryController.php * Optimize code --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Co-authored-by: guandeng --- .../2020_08_03_064816_telescope_entries.php | 5 ++- publish/telescope.php | 2 +- src/Aspect/CacheAspect.php | 20 ++++++----- src/Aspect/EventAspect.php | 6 ++-- src/Aspect/GrpcClientAspect.php | 6 ++-- src/Aspect/HttpClientAspect.php | 6 ++-- src/Aspect/LogAspect.php | 10 +++--- src/Aspect/RedisAspect.php | 15 ++++---- src/Aspect/RequestDispatcherAspect.php | 6 ++-- src/Aspect/RpcAspect.php | 6 ++-- src/Command/ClearCommand.php | 5 ++- src/Command/PruneCommand.php | 5 ++- src/Controller/EntryController.php | 14 -------- src/IncomingEntry.php | 9 ++--- src/Listener/CommandListener.php | 6 ++-- src/Listener/DbQueryListener.php | 6 ++-- src/Listener/ExceptionHandlerListener.php | 6 ++-- src/Listener/RequestHandledListener.php | 35 ++++--------------- src/Listener/SetRequestLifecycleListener.php | 6 ++-- src/Listener/SetupTelescopeServerListener.php | 14 +++++--- src/Middleware/TelescopeMiddleware.php | 35 ++++--------------- src/Model/Model.php | 4 +-- src/SwitchManager.php | 15 +++----- src/Telescope.php | 11 ++++-- src/TelescopeConfig.php | 23 +++++++++--- 25 files changed, 118 insertions(+), 158 deletions(-) diff --git a/migrations/2020_08_03_064816_telescope_entries.php b/migrations/2020_08_03_064816_telescope_entries.php index 15b1900..ece3678 100644 --- a/migrations/2020_08_03_064816_telescope_entries.php +++ b/migrations/2020_08_03_064816_telescope_entries.php @@ -8,12 +8,11 @@ * @document https://github.com/friendsofhyperf/components/blob/main/README.md * @contact huangdijia@gmail.com */ +use FriendsOfHyperf\Telescope\Telescope; use Hyperf\Database\Migrations\Migration; use Hyperf\Database\Schema\Blueprint; use Hyperf\Database\Schema\Schema; -use function Hyperf\Config\config; - class TelescopeEntries extends Migration { /** @@ -68,6 +67,6 @@ public function down(): void */ public function getConnection(): string { - return config('telescope.database.connection', 'default'); + return Telescope::getConfig()->getDatabaseConnection(); } } diff --git a/publish/telescope.php b/publish/telescope.php index d76d5bd..e82ba99 100644 --- a/publish/telescope.php +++ b/publish/telescope.php @@ -45,6 +45,6 @@ // 'api/*' ], 'ignore_paths' => [ - 'nova-api*', + // 'nova-api*', ], ]; diff --git a/src/Aspect/CacheAspect.php b/src/Aspect/CacheAspect.php index 1648b55..c72c1a4 100644 --- a/src/Aspect/CacheAspect.php +++ b/src/Aspect/CacheAspect.php @@ -12,16 +12,16 @@ namespace FriendsOfHyperf\Telescope\Aspect; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; 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; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Stringable\Str; -use function Hyperf\Config\config; use function Hyperf\Tappable\tap; /** @@ -36,8 +36,10 @@ class CacheAspect extends AbstractAspect 'Hyperf\Cache\Driver\*Driver::set', ]; - public function __construct(protected SwitchManager $switcherManager) - { + public function __construct( + protected ConfigInterface $config, + protected TelescopeConfig $telescopeConfig + ) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) @@ -57,7 +59,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) protected function processGetDriver($proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($driver) use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('redis')) { + if (! $this->telescopeConfig->isEnable('redis')) { return; } @@ -69,7 +71,7 @@ protected function processGetDriver($proceedingJoinPoint) protected function processDriverFetch($proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('cache')) { + if (! $this->telescopeConfig->isEnable('cache')) { return; } @@ -86,7 +88,7 @@ protected function processDriverFetch($proceedingJoinPoint) protected function processDriverGet($proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('cache')) { + if (! $this->telescopeConfig->isEnable('cache')) { return; } @@ -102,7 +104,7 @@ protected function processDriverGet($proceedingJoinPoint) protected function processDriverSet($proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function () use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('cache')) { + if (! $this->telescopeConfig->isEnable('cache')) { return; } @@ -118,6 +120,6 @@ protected function processDriverSet($proceedingJoinPoint) protected function getCacheKey(string $key): string { $driver = TelescopeContext::getCacheDriver(); - return config('cache.' . $driver . '.prefix', '') . $key; + return $this->config->get('cache.' . $driver . '.prefix', '') . $key; } } diff --git a/src/Aspect/EventAspect.php b/src/Aspect/EventAspect.php index b3ac714..e06c293 100644 --- a/src/Aspect/EventAspect.php +++ b/src/Aspect/EventAspect.php @@ -12,8 +12,8 @@ namespace FriendsOfHyperf\Telescope\Aspect; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Event\EventDispatcher; @@ -30,14 +30,14 @@ class EventAspect extends AbstractAspect EventDispatcher::class . '::dispatch', ]; - public function __construct(protected SwitchManager $switcherManager, protected ListenerProviderInterface $listeners) + public function __construct(protected TelescopeConfig $telescopeConfig, protected ListenerProviderInterface $listeners) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('event')) { + if (! $this->telescopeConfig->isEnable('event')) { return; } diff --git a/src/Aspect/GrpcClientAspect.php b/src/Aspect/GrpcClientAspect.php index d82f7e8..cd7ff34 100644 --- a/src/Aspect/GrpcClientAspect.php +++ b/src/Aspect/GrpcClientAspect.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Telescope\Aspect; -use FriendsOfHyperf\Telescope\SwitchManager; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -24,7 +24,7 @@ class GrpcClientAspect extends AbstractAspect GrpcClient::class . '::send', ]; - public function __construct(protected SwitchManager $switcherManager) + public function __construct(protected TelescopeConfig $telescopeConfig) { } @@ -38,7 +38,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) private function processSend(ProceedingJoinPoint $proceedingJoinPoint) { - if ($this->switcherManager->isEnable('grpc')) { + if ($this->telescopeConfig->isEnable('grpc')) { $carrier = []; $carrier['batch-id'] = TelescopeContext::getBatchId(); /** @var Request $request */ diff --git a/src/Aspect/HttpClientAspect.php b/src/Aspect/HttpClientAspect.php index f29f60a..5ffe181 100644 --- a/src/Aspect/HttpClientAspect.php +++ b/src/Aspect/HttpClientAspect.php @@ -12,8 +12,8 @@ namespace FriendsOfHyperf\Telescope\Aspect; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use GuzzleHttp\Client; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -29,13 +29,13 @@ class HttpClientAspect extends AbstractAspect Client::class . '::requestAsync', ]; - public function __construct(protected SwitchManager $switcherManager) + public function __construct(protected TelescopeConfig $telescopeConfig) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('guzzle')) { + if (! $this->telescopeConfig->isEnable('guzzle')) { return $proceedingJoinPoint->process(); } $startTime = microtime(true); diff --git a/src/Aspect/LogAspect.php b/src/Aspect/LogAspect.php index 2aaa37f..c5f161d 100644 --- a/src/Aspect/LogAspect.php +++ b/src/Aspect/LogAspect.php @@ -13,15 +13,14 @@ use FriendsOfHyperf\Telescope\IncomingEntry; use FriendsOfHyperf\Telescope\Severity; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Stringable\Str; use Monolog\Logger; use UnitEnum; -use function Hyperf\Config\config; use function Hyperf\Tappable\tap; class LogAspect extends AbstractAspect @@ -30,14 +29,14 @@ class LogAspect extends AbstractAspect Logger::class . '::addRecord', ]; - public function __construct(protected SwitchManager $switcherManager) + public function __construct(protected TelescopeConfig $telescopeConfig) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('log')) { + if (! $this->telescopeConfig->isEnable('log')) { return; } $level = $proceedingJoinPoint->arguments['keys']['level']; @@ -48,8 +47,7 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint) return; } $name = $proceedingJoinPoint->getInstance()->getName(); - $ignoreLogs = config('telescope.ignore_logs', []); - if ($ignoreLogs && in_array($name, $ignoreLogs)) { + if ($this->telescopeConfig->isLogIgnored($name)) { return; } Telescope::recordLog( diff --git a/src/Aspect/RedisAspect.php b/src/Aspect/RedisAspect.php index e4089cc..43cc549 100644 --- a/src/Aspect/RedisAspect.php +++ b/src/Aspect/RedisAspect.php @@ -12,9 +12,10 @@ namespace FriendsOfHyperf\Telescope\Aspect; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; +use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\PackerInterface; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -23,7 +24,6 @@ use Throwable; use function Hyperf\Collection\collect; -use function Hyperf\Config\config; use function Hyperf\Tappable\tap; /** @@ -36,15 +36,18 @@ class RedisAspect extends AbstractAspect Redis::class . '::__call', ]; - public function __construct(protected SwitchManager $switcherManager, protected ContainerInterface $container) - { + public function __construct( + protected ContainerInterface $container, + protected ConfigInterface $config, + protected TelescopeConfig $telescopeConfig, + ) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { $startTime = microtime(true); return tap($proceedingJoinPoint->process(), function ($result) use ($proceedingJoinPoint, $startTime) { - if (! $this->switcherManager->isEnable('redis')) { + if (! $this->telescopeConfig->isEnable('redis')) { return; } @@ -80,7 +83,7 @@ private function formatCommand(string $command, array $parameters): string && $key == 1 && $driver = TelescopeContext::getCacheDriver() ) { - $packer = config('cache.' . $driver . '.packer', ''); + $packer = $this->config->get('cache.' . $driver . '.packer', ''); $packer = $this->container->get($packer); if ($packer instanceof PackerInterface) { try { diff --git a/src/Aspect/RequestDispatcherAspect.php b/src/Aspect/RequestDispatcherAspect.php index 77bf6f5..5367369 100644 --- a/src/Aspect/RequestDispatcherAspect.php +++ b/src/Aspect/RequestDispatcherAspect.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Telescope\Aspect; -use FriendsOfHyperf\Telescope\SwitchManager; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -27,14 +27,14 @@ class RequestDispatcherAspect extends AbstractAspect RequestDispatcher::class . '::dispatch', ]; - public function __construct(protected SwitchManager $switcherManager) + public function __construct(protected TelescopeConfig $telescopeConfig) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function () use ($proceedingJoinPoint) { - if (! $this->switcherManager->isEnable('request')) { + if (! $this->telescopeConfig->isEnable('request')) { return; } diff --git a/src/Aspect/RpcAspect.php b/src/Aspect/RpcAspect.php index 381229d..4f89b87 100644 --- a/src/Aspect/RpcAspect.php +++ b/src/Aspect/RpcAspect.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Telescope\Aspect; -use FriendsOfHyperf\Telescope\SwitchManager; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; @@ -26,14 +26,14 @@ class RpcAspect extends AbstractAspect AbstractServiceClient::class . '::__generateRpcPath', ]; - public function __construct(protected SwitchManager $switcherManager, protected Context $context) + public function __construct(protected TelescopeConfig $telescopeConfig, protected Context $context) { } public function process(ProceedingJoinPoint $proceedingJoinPoint) { return tap($proceedingJoinPoint->process(), function () { - if (static::class == self::class && $this->switcherManager->isEnable('rpc') === false) { + if (static::class == self::class && $this->telescopeConfig->isEnable('rpc') === false) { return; } diff --git a/src/Command/ClearCommand.php b/src/Command/ClearCommand.php index 42437ed..c25e1c8 100644 --- a/src/Command/ClearCommand.php +++ b/src/Command/ClearCommand.php @@ -11,18 +11,17 @@ namespace FriendsOfHyperf\Telescope\Command; +use FriendsOfHyperf\Telescope\Telescope; use Hyperf\Command\Command; use Hyperf\DbConnection\Db; -use function Hyperf\Config\config; - class ClearCommand extends Command { protected ?string $signature = 'telescope:clear'; public function handle() { - $connection = config('telescope.database.connection'); + $connection = Telescope::getConfig()->getDatabaseConnection(); Db::connection($connection)->table('telescope_entries')->delete(); Db::connection($connection)->table('telescope_entries_tags')->delete(); Db::connection($connection)->table('telescope_monitoring')->delete(); diff --git a/src/Command/PruneCommand.php b/src/Command/PruneCommand.php index 1c07b96..1cf3b54 100644 --- a/src/Command/PruneCommand.php +++ b/src/Command/PruneCommand.php @@ -12,18 +12,17 @@ namespace FriendsOfHyperf\Telescope\Command; use Carbon\Carbon; +use FriendsOfHyperf\Telescope\Telescope; use Hyperf\Command\Command; use Hyperf\DbConnection\Db; -use function Hyperf\Config\config; - class PruneCommand extends Command { protected ?string $signature = 'telescope:prune {--hours=24 : The number of hours to retain Telescope data}'; public function handle() { - $connection = config('telescope.database.connection'); + $connection = Telescope::getConfig()->getDatabaseConnection(); $created_at = Carbon::now()->subHours($this->input->getOption('hours')); Db::connection($connection)->table('telescope_entries') ->where('created_at', '<', $created_at) diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php index f711677..6cfcf2b 100644 --- a/src/Controller/EntryController.php +++ b/src/Controller/EntryController.php @@ -119,20 +119,6 @@ abstract protected function watcher(); */ protected function status() { - // if (! config('telescope.enabled', false)) { - // return 'disabled'; - // } - - // if (cache('telescope:pause-recording', false)) { - // return 'paused'; - // } - - // $watcher = config('telescope.watchers.'.$this->watcher()); - - // if (! $watcher || (isset($watcher['enabled']) && ! $watcher['enabled'])) { - // return 'off'; - // } - return 'enabled'; } } diff --git a/src/IncomingEntry.php b/src/IncomingEntry.php index 49bcf65..8322d32 100644 --- a/src/IncomingEntry.php +++ b/src/IncomingEntry.php @@ -16,8 +16,6 @@ use FriendsOfHyperf\Telescope\Model\TelescopeEntryTagModel; use Hyperf\Stringable\Str; -use function Hyperf\Config\config; - class IncomingEntry { /** @@ -71,10 +69,13 @@ class IncomingEntry public function __construct(array $content) { $this->uuid = (string) Str::orderedUuid()->toString(); - $timezone = config('telescope.timezone') ?: date_default_timezone_get(); + $timezone = Telescope::getConfig()->getTimezone(); $this->recordedAt = Carbon::now()->setTimezone($timezone)->toDateTimeString(); $this->content = array_merge($content, ['hostname' => $hostname = gethostname()]); - $this->tags = ['hostname:' . $hostname, 'app_name:' . config('app_name', '')]; + $this->tags = [ + 'hostname:' . $hostname, + 'app_name:' . Telescope::getConfig()->getAppName(), + ]; } /** diff --git a/src/Listener/CommandListener.php b/src/Listener/CommandListener.php index 87ab4e2..21cd906 100644 --- a/src/Listener/CommandListener.php +++ b/src/Listener/CommandListener.php @@ -12,8 +12,8 @@ namespace FriendsOfHyperf\Telescope\Listener; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Command\Event\AfterExecute; use Hyperf\Event\Contract\ListenerInterface; @@ -23,7 +23,7 @@ */ class CommandListener implements ListenerInterface { - public function __construct(private SwitchManager $switchManager) + public function __construct(private TelescopeConfig $telescopeConfig) { } @@ -39,7 +39,7 @@ public function listen(): array */ public function process(object $event): void { - if ($this->switchManager->isEnable('command') === false) { + if ($this->telescopeConfig->isEnable('command') === false) { return; } diff --git a/src/Listener/DbQueryListener.php b/src/Listener/DbQueryListener.php index fd57760..9cc3348 100644 --- a/src/Listener/DbQueryListener.php +++ b/src/Listener/DbQueryListener.php @@ -12,8 +12,8 @@ namespace FriendsOfHyperf\Telescope\Listener; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Collection\Arr; use Hyperf\Database\Events\QueryExecuted; use Hyperf\Event\Contract\ListenerInterface; @@ -21,7 +21,7 @@ class DbQueryListener implements ListenerInterface { - public function __construct(private SwitchManager $switchManager) + public function __construct(private TelescopeConfig $telescopeConfig) { } @@ -37,7 +37,7 @@ public function listen(): array */ public function process(object $event): void { - if ($this->switchManager->isEnable('db') === false) { + if ($this->telescopeConfig->isEnable('db') === false) { return; } if ($event instanceof QueryExecuted) { diff --git a/src/Listener/ExceptionHandlerListener.php b/src/Listener/ExceptionHandlerListener.php index c4de57c..f760d7b 100644 --- a/src/Listener/ExceptionHandlerListener.php +++ b/src/Listener/ExceptionHandlerListener.php @@ -12,8 +12,8 @@ namespace FriendsOfHyperf\Telescope\Listener; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Collection\Arr; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\HttpServer\Event; @@ -23,7 +23,7 @@ class ExceptionHandlerListener implements ListenerInterface { - public function __construct(private SwitchManager $switchManager) + public function __construct(private TelescopeConfig $telescopeConfig) { } @@ -39,7 +39,7 @@ public function listen(): array */ public function process(object $event): void { - if ($this->switchManager->isEnable('exception') === false) { + if ($this->telescopeConfig->isEnable('exception') === false) { return; } diff --git a/src/Listener/RequestHandledListener.php b/src/Listener/RequestHandledListener.php index 0698088..40123c0 100644 --- a/src/Listener/RequestHandledListener.php +++ b/src/Listener/RequestHandledListener.php @@ -12,11 +12,10 @@ namespace FriendsOfHyperf\Telescope\Listener; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; use Hyperf\Collection\Arr; -use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\HttpServer\Event\RequestReceived; use Hyperf\HttpServer\Event\RequestTerminated; @@ -38,8 +37,7 @@ class RequestHandledListener implements ListenerInterface { public function __construct( protected ContainerInterface $container, - protected ConfigInterface $config, - protected SwitchManager $switchManager + protected TelescopeConfig $telescopeConfig, ) { } @@ -55,7 +53,7 @@ public function listen(): array public function process(object $event): void { - if (! $this->switchManager->isEnable('request')) { + if (! $this->telescopeConfig->isEnable('request')) { return; } match ($event::class) { @@ -134,32 +132,11 @@ public function requestHandled(RequestTerminated|RpcRequestTerminated $event) protected function incomingRequest(ServerRequestInterface $psr7Request): bool { - if (! empty($only = config('telescope.only_paths', []))) { - return $this->is($psr7Request, $only); + if ($this->telescopeConfig->isPatchOnly($psr7Request)) { + return true; } - return ! $this->is( - $psr7Request, - collect([ - '*favicon.ico', - 'telescope-api*', - 'vendor/telescope*', - ]) - ->merge(config('telescope.ignore_paths', [])) - ->unless(is_null(config('telescope.path', 'telescope')), function ($paths) { - return $paths->prepend(config('telescope.path', 'telescope') . '*'); - }) - ->all() - ); - } - - protected function is($psr7Request, $patterns) - { - $uri = $psr7Request->getUri(); - $path = $uri->getPath(); - $path = rawurldecode(trim($path, '/')); - - return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path)); + return ! $this->telescopeConfig->isPathIgnored($psr7Request); } protected function response(ResponseInterface $response): string|array diff --git a/src/Listener/SetRequestLifecycleListener.php b/src/Listener/SetRequestLifecycleListener.php index 3539066..47e05b7 100644 --- a/src/Listener/SetRequestLifecycleListener.php +++ b/src/Listener/SetRequestLifecycleListener.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Telescope\Listener; -use FriendsOfHyperf\Telescope\SwitchManager; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BootApplication; @@ -23,7 +23,7 @@ class SetRequestLifecycleListener implements ListenerInterface { public function __construct( protected ConfigInterface $config, - protected SwitchManager $switchManager, + protected TelescopeConfig $telescopeConfig, ) { } @@ -36,7 +36,7 @@ public function listen(): array public function process(object $event): void { - if (! $this->switchManager->isEnable('request')) { + if (! $this->telescopeConfig->isEnable('request')) { return; } diff --git a/src/Listener/SetupTelescopeServerListener.php b/src/Listener/SetupTelescopeServerListener.php index a895693..8ea0f13 100644 --- a/src/Listener/SetupTelescopeServerListener.php +++ b/src/Listener/SetupTelescopeServerListener.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Telescope\Listener; use FriendsOfHyperf\Telescope\Server\Server; +use FriendsOfHyperf\Telescope\TelescopeConfig; use Hyperf\Contract\ConfigInterface; use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Framework\Event\BootApplication; @@ -20,8 +21,10 @@ class SetupTelescopeServerListener implements ListenerInterface { - public function __construct(private ConfigInterface $config) - { + public function __construct( + private ConfigInterface $config, + private TelescopeConfig $telescopeConfig, + ) { } public function listen(): array @@ -33,12 +36,13 @@ public function listen(): array public function process(object $event): void { - if (! $this->config->get('telescope.server.enable', false)) { + if (! $this->telescopeConfig->isServerEnable()) { return; } - $host = $this->config->get('telescope.server.host', '0.0.0.0'); - $port = (int) $this->config->get('telescope.server.port', 9509); + $host = $this->telescopeConfig->getServerHost(); + $port = $this->telescopeConfig->getServerPort(); + $servers = $this->config->get('server.servers'); $servers[] = [ diff --git a/src/Middleware/TelescopeMiddleware.php b/src/Middleware/TelescopeMiddleware.php index 85b2569..87a28c7 100644 --- a/src/Middleware/TelescopeMiddleware.php +++ b/src/Middleware/TelescopeMiddleware.php @@ -12,11 +12,10 @@ namespace FriendsOfHyperf\Telescope\Middleware; use FriendsOfHyperf\Telescope\IncomingEntry; -use FriendsOfHyperf\Telescope\SwitchManager; use FriendsOfHyperf\Telescope\Telescope; +use FriendsOfHyperf\Telescope\TelescopeConfig; use FriendsOfHyperf\Telescope\TelescopeContext; use Hyperf\Collection\Arr; -use Hyperf\Contract\ConfigInterface; use Hyperf\HttpServer\Router\Dispatched; use Hyperf\Rpc\Context as RpcContext; use Hyperf\Server\Event; @@ -35,14 +34,13 @@ class TelescopeMiddleware implements MiddlewareInterface { public function __construct( protected ContainerInterface $container, - protected ConfigInterface $config, - protected SwitchManager $switchManager + protected TelescopeConfig $telescopeConfig ) { } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - if (! $this->switchManager->isEnable('request')) { + if (! $this->telescopeConfig->isEnable('request')) { return $handler->handle($request); } @@ -118,32 +116,11 @@ public function requestHandled($request, $response) protected function incomingRequest(ServerRequestInterface $psr7Request): bool { - if (! empty($only = config('telescope.only_paths', []))) { - return $this->is($psr7Request, $only); + if ($this->telescopeConfig->isPatchOnly($psr7Request)) { + return true; } - return ! $this->is( - $psr7Request, - collect([ - '*favicon.ico', - 'telescope-api*', - 'vendor/telescope*', - ]) - ->merge(config('telescope.ignore_paths', [])) - ->unless(is_null(config('telescope.path', 'telescope')), function ($paths) { - return $paths->prepend(config('telescope.path', 'telescope') . '*'); - }) - ->all() - ); - } - - protected function is($psr7Request, $patterns) - { - $uri = $psr7Request->getUri(); - $path = $uri->getPath(); - $path = rawurldecode(trim($path, '/')); - - return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path)); + return ! $this->telescopeConfig->isPathIgnored($psr7Request); } protected function response(ResponseInterface $response): string|array diff --git a/src/Model/Model.php b/src/Model/Model.php index 475d9d2..cd7f2a7 100644 --- a/src/Model/Model.php +++ b/src/Model/Model.php @@ -11,12 +11,12 @@ namespace FriendsOfHyperf\Telescope\Model; -use function Hyperf\Config\config; +use FriendsOfHyperf\Telescope\Telescope; abstract class Model extends \Hyperf\DbConnection\Model\Model { public function getConnectionName() { - return config('telescope.database.connection', 'default'); + return Telescope::getConfig()->getDatabaseConnection(); } } diff --git a/src/SwitchManager.php b/src/SwitchManager.php index e9a0797..b08d86d 100644 --- a/src/SwitchManager.php +++ b/src/SwitchManager.php @@ -11,16 +11,9 @@ namespace FriendsOfHyperf\Telescope; -use Hyperf\Contract\ConfigInterface; - -class SwitchManager +/** + * @deprecated use FriendsOfHyperf\Telescope\TelescopeConfig instead + */ +class SwitchManager extends TelescopeConfig { - public function __construct(protected ConfigInterface $config) - { - } - - public function isEnable(string $key): bool - { - return (bool) $this->config->get("telescope.enable.{$key}", false); - } } diff --git a/src/Telescope.php b/src/Telescope.php index 29637fe..da8d915 100644 --- a/src/Telescope.php +++ b/src/Telescope.php @@ -13,6 +13,7 @@ use Closure; use Hyperf\Collection\Arr; +use Hyperf\Context\ApplicationContext; use function Hyperf\Config\config; @@ -128,12 +129,13 @@ public static function recordClientRequest(IncomingEntry $entry): void */ public static function getAppName(): string { - return config('app_name', '') ? '[' . config('app_name', '') . '] ' : ''; + $appName = static::getConfig()->getAppName(); + return $appName ? '[' . $appName . '] ' : ''; } public static function getQuerySlow(): int { - return config('telescope.database.query_slow', 50); + return static::getConfig()->getDatabaseQuerySlow(); } /** @@ -160,6 +162,11 @@ public static function filter(Closure $callback) return new static(); } + public static function getConfig(): TelescopeConfig + { + return ApplicationContext::getContainer()->get(TelescopeConfig::class); + } + /** * Determine if the given entry should be recorded. */ diff --git a/src/TelescopeConfig.php b/src/TelescopeConfig.php index 3c5949e..532bff2 100644 --- a/src/TelescopeConfig.php +++ b/src/TelescopeConfig.php @@ -13,6 +13,7 @@ use Hyperf\Contract\ConfigInterface; use Hyperf\Stringable\Str; +use Psr\Http\Message\ServerRequestInterface; class TelescopeConfig { @@ -78,9 +79,14 @@ public function isEnable(string $key): bool return (bool) $this->get('enable.' . $key, false); } + public function getAppName(): string + { + return (string) $this->config->get('app_name', ''); + } + public function getTimezone(): string { - return (string) $this->get('timezone', 'Asia/Shanghai'); + return (string) $this->get('timezone', date_default_timezone_get()); } public function getSaveMode(): int @@ -122,8 +128,12 @@ public function getOnlyPaths(): array return (array) $this->get('only_paths', []); } - public function isPatchOnly(string $path): bool + public function isPatchOnly(ServerRequestInterface|string $path): bool { + if ($path instanceof ServerRequestInterface) { + $path = $path->getUri()->getPath(); + } + if (empty($this->getOnlyPaths())) { return false; } @@ -141,14 +151,19 @@ public function getIgnorePaths(): array '*favicon.ico', 'telescope-api*', 'vendor/telescope*', + $this->getPath() . '*', ]; $ignorePaths = (array) $this->get('ignore_paths', []); return array_merge($defaultIgnorePaths, $ignorePaths); } - public function isPathIgnored(string $path): bool + public function isPathIgnored(ServerRequestInterface|string $path): bool { - return Str::is($this->getIgnorePaths(), $path); + if ($path instanceof ServerRequestInterface) { + $path = $path->getUri()->getPath(); + } + + return Str::is($this->getIgnorePaths(), rawurldecode(trim($path, '/'))); } }