Skip to content

Commit

Permalink
Improve getting request url without query (#503)
Browse files Browse the repository at this point in the history
* Improve getting request url without query

* Adds TelescopeConfig

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
guandeng and huangdijia authored Dec 21, 2023
1 parent 1c09974 commit 11c8e08
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Listener/RequestHandledListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ protected function incomingRequest(ServerRequestInterface $psr7Request): bool
return ! $this->is(
$psr7Request,
collect([
'*favicon.ico',
'telescope-api*',
'vendor/telescope*',
])
Expand All @@ -154,8 +155,9 @@ protected function incomingRequest(ServerRequestInterface $psr7Request): bool

protected function is($psr7Request, $patterns)
{
$path = $psr7Request->getRequestTarget();
$path = ltrim($path, '/');
$uri = $psr7Request->getUri();
$path = $uri->getPath();
$path = rawurldecode(trim($path, '/'));

return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path));
}
Expand Down
6 changes: 4 additions & 2 deletions src/Middleware/TelescopeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ protected function incomingRequest(ServerRequestInterface $psr7Request): bool
return ! $this->is(
$psr7Request,
collect([
'*favicon.ico',
'telescope-api*',
'vendor/telescope*',
])
Expand All @@ -138,8 +139,9 @@ protected function incomingRequest(ServerRequestInterface $psr7Request): bool

protected function is($psr7Request, $patterns)
{
$path = $psr7Request->getRequestTarget();
$path = ltrim($path, '/');
$uri = $psr7Request->getUri();
$path = $uri->getPath();
$path = rawurldecode(trim($path, '/'));

return collect($patterns)->contains(fn ($pattern) => Str::is($pattern, $path));
}
Expand Down
154 changes: 154 additions & 0 deletions src/TelescopeConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?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;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Stringable\Str;

class TelescopeConfig
{
public function __construct(private ConfigInterface $config)
{
}

public function get(string $key, $default = null): mixed
{
return $this->config->get('telescope.' . $key, $default);
}

/**
* @return array{enable: bool, host: string, port: int}
*/
public function getServerOptions(): array
{
return array_replace([
'enable' => false,
'host' => '0.0.0.0',
'port' => 9509,
], (array) $this->get('server', []));
}

public function isServerEnable(): bool
{
return (bool) $this->getServerOptions()['enable'];
}

public function getServerHost(): string
{
return (string) ($this->getServerOptions()['host'] ?: '0.0.0.0');
}

public function getServerPort(): int
{
return (int) ($this->getServerOptions()['port'] ?: 9509);
}

/**
* @return array{connection: string, query_slow: int}
*/
public function getDatabaseOptions(): array
{
return array_replace([
'connection' => 'default',
'query_slow' => 50,
], (array) $this->get('database', []));
}

public function getDatabaseConnection(): string
{
return (string) ($this->getDatabaseOptions()['connection'] ?: 'default');
}

public function getDatabaseQuerySlow(): int
{
return (int) $this->getDatabaseOptions()['query_slow'];
}

public function isEnable(string $key): bool
{
return (bool) $this->get('enable.' . $key, false);
}

public function getTimezone(): string
{
return (string) $this->get('timezone', 'Asia/Shanghai');
}

public function getSaveMode(): int
{
return match ($this->get('save_mode', Telescope::SYNC)) {
Telescope::ASYNC => Telescope::ASYNC,
Telescope::SYNC => Telescope::SYNC,
default => Telescope::SYNC,
};
}

public function getPath(): string
{
return (string) $this->get('path', 'telescope');
}

/**
* @return string[]
*/
public function getIgnoreLogs(): array
{
return (array) $this->get('ignore_logs', []);
}

public function isLogIgnored(string $name): bool
{
if (empty($this->getIgnoreLogs())) {
return false;
}

return in_array($name, $this->getIgnoreLogs(), true);
}

/**
* @return string[]
*/
public function getOnlyPaths(): array
{
return (array) $this->get('only_paths', []);
}

public function isPatchOnly(string $path): bool
{
if (empty($this->getOnlyPaths())) {
return false;
}

return Str::is($this->getOnlyPaths(), $path);
}

/**
* @return string[]
*/
public function getIgnorePaths(): array
{
$defaultIgnorePaths = [
'nova-api*',
'*favicon.ico',
'telescope-api*',
'vendor/telescope*',
];
$ignorePaths = (array) $this->get('ignore_paths', []);

return array_merge($defaultIgnorePaths, $ignorePaths);
}

public function isPathIgnored(string $path): bool
{
return Str::is($this->getIgnorePaths(), $path);
}
}

0 comments on commit 11c8e08

Please sign in to comment.