Skip to content

Commit a6978bb

Browse files
committed
wip
1 parent a6070f2 commit a6978bb

6 files changed

Lines changed: 78 additions & 17 deletions

File tree

packages/core/src/Commands/DiscoveryClearCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
namespace Tempest\Core\Commands;
66

7-
use Tempest\Console\Console;
87
use Tempest\Console\ConsoleCommand;
8+
use Tempest\Console\HasConsole;
9+
use Tempest\Discovery\ClearDiscoveryCache;
910
use Tempest\Discovery\DiscoveryCache;
1011

1112
if (class_exists(\Tempest\Console\ConsoleCommand::class)) {
1213
final readonly class DiscoveryClearCommand
1314
{
15+
use HasConsole;
16+
1417
public function __construct(
1518
private DiscoveryCache $discoveryCache,
16-
private Console $console,
19+
private ClearDiscoveryCache $clearDiscoveryCache,
1720
) {}
1821

1922
#[ConsoleCommand(
@@ -25,7 +28,7 @@ public function __invoke(): void
2528
{
2629
$this->console->task(
2730
label: 'Clearing discovery cache',
28-
handler: fn () => $this->discoveryCache->clear(),
31+
handler: fn () => ($this->clearDiscoveryCache)($this->discoveryCache),
2932
);
3033
}
3134
}

packages/core/src/Commands/DiscoveryGenerateCommand.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77
use Closure;
88
use Tempest\Console\ConsoleCommand;
99
use Tempest\Console\HasConsole;
10+
use Tempest\Container\Container;
1011
use Tempest\Container\GenericContainer;
1112
use Tempest\Core\FrameworkKernel;
12-
use Tempest\Core\Kernel;
1313
use Tempest\Discovery\BootDiscovery;
14+
use Tempest\Discovery\ClearDiscoveryCache;
1415
use Tempest\Discovery\DiscoveryCache;
1516
use Tempest\Discovery\DiscoveryCacheStrategy;
1617
use Tempest\Discovery\DiscoveryConfig;
18+
use Tempest\Discovery\GenerateDiscoveryCache;
1719

1820
if (class_exists(\Tempest\Console\ConsoleCommand::class)) {
1921
final readonly class DiscoveryGenerateCommand
2022
{
2123
use HasConsole;
2224

2325
public function __construct(
24-
private DiscoveryConfig $discoveryConfig,
2526
private FrameworkKernel $kernel,
27+
private Container $container,
28+
private DiscoveryConfig $discoveryConfig,
2629
private DiscoveryCache $discoveryCache,
30+
private GenerateDiscoveryCache $generateDiscoveryCache,
31+
private ClearDiscoveryCache $clearDiscoveryCache,
2732
) {}
2833

2934
#[ConsoleCommand(
@@ -41,20 +46,26 @@ public function __invoke(): void
4146
return;
4247
}
4348

44-
$this->clearDiscoveryCache();
45-
4649
$this->console->task(
47-
label: "Generating discovery cache using the `{$strategy->value}` strategy",
48-
handler: fn (Closure $log) => $this->generateDiscoveryCache($strategy, $log),
50+
label: 'Clearing discovery cache',
51+
handler: fn () => ($this->clearDiscoveryCache)($this->discoveryCache),
4952
);
50-
}
5153

52-
public function clearDiscoveryCache(): void
53-
{
54-
$this->console->call(DiscoveryClearCommand::class);
54+
$this->console->task(
55+
label: "Generating {$strategy->value} discovery cache",
56+
handler: function () use ($strategy) {
57+
$kernel = $this->resolveKernel();
58+
59+
($this->generateDiscoveryCache)(
60+
$kernel->container,
61+
$kernel->discoveryConfig,
62+
$this->discoveryCache->withStrategy($strategy),
63+
);
64+
},
65+
);
5566
}
5667

57-
public function generateDiscoveryCache(DiscoveryCacheStrategy $strategy, Closure $log): void
68+
private function generateDiscoveryCache(DiscoveryCacheStrategy $strategy, Closure $log): void
5869
{
5970
$kernel = $this->resolveKernel();
6071

@@ -74,7 +85,7 @@ public function generateDiscoveryCache(DiscoveryCacheStrategy $strategy, Closure
7485
$this->discoveryCache->storeStrategy($strategy);
7586
}
7687

77-
public function resolveKernel(): Kernel
88+
private function resolveKernel(): FrameworkKernel
7889
{
7990
$container = new GenericContainer();
8091
$container->singleton(DiscoveryConfig::class, $this->discoveryConfig);
@@ -86,6 +97,7 @@ public function resolveKernel(): Kernel
8697
)
8798
->registerKernel()
8899
->loadComposer()
100+
->loadDiscoveryConfig()
89101
->loadConfig();
90102
}
91103
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tempest\Discovery;
4+
5+
final class ClearDiscoveryCache
6+
{
7+
public function __invoke(DiscoveryCache $cache): void
8+
{
9+
$cache->clear();
10+
}
11+
}

packages/discovery/src/DiscoveryCache.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ final class DiscoveryCache
2323
}
2424

2525
public function __construct(
26-
private(set) DiscoveryCacheStrategy $strategy,
26+
private(set) readonly DiscoveryCacheStrategy $strategy,
2727
private ?CacheItemPoolInterface $pool = null,
2828
) {
2929
$this->pool = $pool ?? new PhpFilesAdapter(
3030
directory: self::getCachePath(),
3131
);
3232
}
3333

34+
public function withStrategy(DiscoveryCacheStrategy $strategy): self
35+
{
36+
return clone($this, [
37+
'strategy' => $strategy,
38+
]);
39+
}
40+
3441
/**
3542
* @return array<class-string<\Tempest\Discovery\Discovery>, DiscoveryItems>
3643
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tempest\Discovery;
4+
5+
use Psr\Container\ContainerInterface;
6+
7+
final class GenerateDiscoveryCache
8+
{
9+
public function __invoke(
10+
ContainerInterface $container,
11+
DiscoveryConfig $config,
12+
DiscoveryCache $cache,
13+
): void {
14+
$bootDiscovery = new BootDiscovery(
15+
container: $container,
16+
config: $config,
17+
cache: $cache->withStrategy(DiscoveryCacheStrategy::NONE),
18+
);
19+
20+
$discoveries = $bootDiscovery->build();
21+
22+
foreach ($config->locations as $location) {
23+
$cache->store($location, $discoveries);
24+
}
25+
26+
$cache->storeStrategy($cache->strategy);
27+
}
28+
}

packages/discovery/tests/Fixtures/ContainerWithoutAutowiring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class ContainerWithoutAutowiring implements ContainerInterface
88
{
9-
public function get(string $id)
9+
public function get(string $id): null
1010
{
1111
return null;
1212
}

0 commit comments

Comments
 (0)