Skip to content

Commit

Permalink
Added config options for ignored paths (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
guandeng authored Dec 20, 2023
1 parent a0d7d31 commit 62b3c19
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
7 changes: 7 additions & 0 deletions publish/telescope.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@
'save_mode' => Telescope::SYNC,
'ignore_logs' => [
],
'path' => env('TELESCOPE_PATH', 'telescope'),
'only_paths' => [
// 'api/*'
],
'ignore_paths' => [
'nova-api*',
],
];
29 changes: 22 additions & 7 deletions src/Listener/RequestHandledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,30 @@ public function requestHandled(RequestTerminated|RpcRequestTerminated $event)

protected function incomingRequest(ServerRequestInterface $psr7Request): bool
{
$target = $psr7Request->getRequestTarget();

if (Str::contains($target, ['telescope'])
|| Str::endsWith($target, ['.ico'])
) {
return false;
if (! empty($only = config('telescope.only_paths', []))) {
return $this->is($psr7Request, $only);
}

return true;
return ! $this->is(
$psr7Request,
collect([
'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)
{
$path = $psr7Request->getRequestTarget();
$path = ltrim($path, '/');

return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path));
}

protected function response(ResponseInterface $response): string|array
Expand Down
29 changes: 22 additions & 7 deletions src/Middleware/TelescopeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,30 @@ public function requestHandled($request, $response)

protected function incomingRequest(ServerRequestInterface $psr7Request): bool
{
$target = $psr7Request->getRequestTarget();

if (Str::contains($target, ['telescope'])
|| Str::endsWith($target, ['.ico'])
) {
return false;
if (! empty($only = config('telescope.only_paths', []))) {
return $this->is($psr7Request, $only);
}

return true;
return ! $this->is(
$psr7Request,
collect([
'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)
{
$path = $psr7Request->getRequestTarget();
$path = ltrim($path, '/');

return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path));
}

protected function response(ResponseInterface $response): string|array
Expand Down

0 comments on commit 62b3c19

Please sign in to comment.