Skip to content

Commit d9f7ce3

Browse files
committed
Improve discovery performance
1 parent 9e16cc3 commit d9f7ce3

1 file changed

Lines changed: 36 additions & 11 deletions

File tree

src/Discovery/Infrastructure/Services/DiscoveryEngine.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Container\Container;
88
use Illuminate\Support\Collection;
9+
use Pollora\Application\Domain\Contracts\DebugDetectorInterface;
910
use Pollora\Discovery\Domain\Contracts\DiscoversPathInterface;
1011
use Pollora\Discovery\Domain\Contracts\DiscoveryEngineInterface;
1112
use Pollora\Discovery\Domain\Contracts\DiscoveryInterface;
@@ -17,8 +18,10 @@
1718
use RecursiveDirectoryIterator;
1819
use RecursiveIteratorIterator;
1920
use Spatie\StructureDiscoverer\Cache\LaravelDiscoverCacheDriver;
21+
use Spatie\StructureDiscoverer\Cache\NullDiscoverCacheDriver;
2022
use Spatie\StructureDiscoverer\Discover;
2123
use SplFileInfo;
24+
use function WP_CLI\Dispatcher\get_path;
2225

2326
/**
2427
* Discovery Engine
@@ -34,6 +37,13 @@
3437
*/
3538
final class DiscoveryEngine implements DiscoveryEngineInterface
3639
{
40+
/**
41+
* Static cache for discovered structures to avoid repeated scans
42+
*
43+
* @var array<string, mixed>
44+
*/
45+
private static array $structuresCache = [];
46+
3747
/**
3848
* Collection of discovery locations
3949
*
@@ -54,7 +64,8 @@ final class DiscoveryEngine implements DiscoveryEngineInterface
5464
* @param Container $container The service container for dependency injection
5565
*/
5666
public function __construct(
57-
private readonly Container $container
67+
private readonly Container $container,
68+
private readonly DebugDetectorInterface $debugDetector
5869
) {
5970
$this->locations = new Collection;
6071
$this->discoveries = new Collection;
@@ -212,13 +223,22 @@ private function discoverStructures(DiscoveryInterface $discovery): void
212223
// Use Spatie's native caching with a cache identifier based on location and discovery type
213224
$cacheId = 'discovery_'.$discovery->getIdentifier().'_'.md5($location->getPath());
214225

215-
$discoveredStructures = Discover::in($location->getPath())
216-
->full()
217-
->withCache(
218-
$cacheId,
219-
new LaravelDiscoverCacheDriver
220-
)
221-
->get();
226+
// Check if we already have the structures cached in memory
227+
if (isset(self::$structuresCache[$cacheId])) {
228+
$discoveredStructures = self::$structuresCache[$cacheId];
229+
} else {
230+
// Discover and cache the structures
231+
$discoveredStructures = Discover::in($location->getPath())
232+
->full()
233+
->withCache(
234+
$cacheId,
235+
$this->debugDetector->isDebugMode() ? new NullDiscoverCacheDriver() : new LaravelDiscoverCacheDriver
236+
)
237+
->get();
238+
239+
// Cache the results in memory for future use
240+
self::$structuresCache[$cacheId] = $discoveredStructures;
241+
}
222242

223243
foreach ($discoveredStructures as $structure) {
224244
$discovery->discover($location, $structure);
@@ -240,9 +260,6 @@ private function discoverPaths(DiscoversPathInterface $discovery): void
240260

241261
/** @var SplFileInfo $file */
242262
foreach ($iterator as $file) {
243-
echo '<pre>';
244-
var_dump($file);
245-
echo '</pre>';
246263
if ($file->isFile()) {
247264
$discovery->discoverPath($location, $file->getPathname());
248265
}
@@ -260,6 +277,14 @@ public function clearLocations(): static
260277
return $this;
261278
}
262279

280+
/**
281+
* Clear the static structures cache
282+
*/
283+
public static function clearStructuresCache(): void
284+
{
285+
self::$structuresCache = [];
286+
}
287+
263288
/**
264289
* Run a specific discovery
265290
*

0 commit comments

Comments
 (0)