Skip to content

Commit

Permalink
refactor(telescope): improve storage driver configuration (#794)
Browse files Browse the repository at this point in the history
* refactor(telescope): improve storage driver configuration

- Add NullEntriesRepository as default storage driver
- Rename compatibility method for legacy config
- Update storage driver configuration structure
- Fix storage options merging with default values

* Fix

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Dec 12, 2024
1 parent cb797d4 commit 7c3d244
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/Storage/EntriesRepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public function __invoke(?ContainerInterface $container = null)
// Compatibility with v3.1
$this->compatibilityWithLegacyConfig($container->get(ConfigInterface::class));

$driver = $telescopeConfig->getStorageDriver();
$options = $telescopeConfig->getStorageOptions($driver);

if (! $options || ! isset($options['driver'])) {
throw new InvalidArgumentException(sprintf('The driver [%s] has not been registered.', $driver));
}
$options = array_replace(
[
'driver' => NullEntriesRepository::class,
],
$telescopeConfig->getStorageOptions($telescopeConfig->getStorageDriver())
);

/** @var mixed $driver */
$driver = $options['driver'];
$instance = match (true) {
$driver instanceof Closure => $driver($container, $options),
Expand Down
86 changes: 86 additions & 0 deletions src/Storage/NullEntriesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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 huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Telescope\Storage;

use Carbon\Carbon;
use DateTimeInterface;
use FriendsOfHyperf\Telescope\Contract\ClearableRepository;
use FriendsOfHyperf\Telescope\Contract\EntriesRepository;
use FriendsOfHyperf\Telescope\Contract\PrunableRepository;
use FriendsOfHyperf\Telescope\Contract\TerminableRepository;
use FriendsOfHyperf\Telescope\EntryResult;

use function Hyperf\Collection\collect;

class NullEntriesRepository implements EntriesRepository, ClearableRepository, PrunableRepository, TerminableRepository
{
public function store($entries): void
{
}

public function update($updates)
{
}

public function get(?string $type, EntryQueryOptions $options)
{
return collect();
}

public function find($id): EntryResult
{
return new EntryResult(
id: null,
sequence: null,
batchId: '',
type: '',
familyHash: '',
content: [],
createdAt: Carbon::now(),
);
}

public function loadMonitoredTags(): void
{
}

public function isMonitoring(array $tags): bool
{
return false;
}

public function monitoring(): array
{
return [];
}

public function monitor(array $tags): void
{
}

public function stopMonitoring(array $tags): void
{
}

public function clear(): void
{
}

public function prune(DateTimeInterface $before, bool $keepExceptions): int
{
return 0;
}

public function terminate(): void
{
}
}
6 changes: 3 additions & 3 deletions src/TelescopeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function get(string $key, $default = null): mixed

public function getStorageDriver(string $default = 'database'): string
{
return (string) $this->get('storage.driver', $default);
return (string) $this->get('driver', $default);
}

public function getStorageOptions(string $driver = 'database'): ?array
public function getStorageOptions(string $driver = 'database'): array
{
return $this->get('storage.' . $driver);
return (array) $this->get('storage.' . $driver, []);
}

/**
Expand Down

0 comments on commit 7c3d244

Please sign in to comment.