Skip to content

Commit

Permalink
Merge pull request tempestphp#413 from tempestphp/discovery-cache-com…
Browse files Browse the repository at this point in the history
…mand

Discovery cache command
  • Loading branch information
brendt authored Sep 17, 2024
2 parents 1b2dfaf + 6e08cf7 commit f160ea0
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Unit tests for the CommandBus `dispatch` method.
- Replaced Ignition with Whoops.
- Properly detect environment from `.env` when present.
- Properly detect environment from `.env` when present.
- `discovery:cache` command that will generate the discovery cache
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ext-pdo": "*",
"ext-readline": "*",
"ext-simplexml": "*",
"filp/whoops": "^2.15",
"giggsey/libphonenumber-for-php": "^8.13.40",
"guzzlehttp/guzzle": "^7.8",
"laminas/laminas-diactoros": "^3.3",
Expand All @@ -28,8 +29,7 @@
"symfony/var-dumper": "^7.1",
"symfony/var-exporter": "^7.1",
"tempest/highlight": "^2.0",
"vlucas/phpdotenv": "^5.6",
"filp/whoops": "^2.15"
"vlucas/phpdotenv": "^5.6"
},
"require-dev": {
"aidan-casey/mock-client": "dev-master",
Expand Down
46 changes: 46 additions & 0 deletions src/Tempest/Console/src/Commands/DiscoveryCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Commands;

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\HasConsole;
use Tempest\Container\Container;
use Tempest\Core\Discovery;
use Tempest\Core\Kernel;

final readonly class DiscoveryCacheCommand
{
use HasConsole;

public function __construct(
private Container $container,
private Console $console,
private Kernel $kernel,
) {
}

#[ConsoleCommand(
name: 'discovery:cache',
description: 'Generate and store the discovery cache',
aliases: ['discovery:store', 'discovery:warm'],
)]
public function __invoke(): void
{
foreach ($this->kernel->discoveryClasses as $discoveryClass) {
/** @var Discovery $discovery */
$discovery = $this->container->get($discoveryClass);

$discovery->storeCache();

$this->writeln(sprintf(
'<em>%s</em> cached successful',
$discoveryClass,
));
}

$this->console->success('Done');
}
}
2 changes: 1 addition & 1 deletion src/Tempest/Core/src/DiscoveryDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final readonly class DiscoveryDiscovery implements Discovery
{
public const string CACHE_PATH = __DIR__ . '/../../../.cache/tempest/discovery-discovery.cache.php';
public const string CACHE_PATH = __DIR__ . '/../../../../.cache/tempest/discovery-discovery.cache.php';

public function __construct(
private Kernel $kernel,
Expand Down
4 changes: 2 additions & 2 deletions src/Tempest/Core/src/GenericExceptionHandlerSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function setup(AppConfig $appConfig): void
}

// Local web
$whoops = new Run;
$whoops->pushHandler(new PrettyPageHandler);
$whoops = new Run();
$whoops->pushHandler(new PrettyPageHandler());
$whoops->register();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Core/src/HandlesDiscoveryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getCachePath(): string

$name = array_pop($parts) . '.cache.php';

return __DIR__ . '/../../../.cache/tempest/' . $name;
return __DIR__ . '/../../../../.cache/tempest/' . $name;
}

abstract public function createCachePayload(): string;
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Http/src/RouteDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final readonly class RouteDiscovery implements Discovery
{
private const string CACHE_PATH = __DIR__ . '/../../../.cache/tempest/route-discovery.cache.php';
private const string CACHE_PATH = __DIR__ . '/../../../../.cache/tempest/route-discovery.cache.php';

/** @var VarExportPhpFile<RouteConfig> */
private VarExportPhpFile $routeCacheFile;
Expand Down
4 changes: 3 additions & 1 deletion tests/Integration/Console/Commands/MyDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class MyDiscovery implements Discovery
{
public static bool $cacheCleared = false;

public static bool $cached = false;

public function discover(ClassReflector $class): void
{
// TODO: Implement discover() method.
Expand All @@ -24,7 +26,7 @@ public function hasCache(): bool

public function storeCache(): void
{
// TODO: Implement storeCache() method.
self::$cached = true;
}

public function restoreCache(Container $container): void
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/Framework/Commands/DiscoveryCacheCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Tempest\Integration\Framework\Commands;

use Tests\Tempest\Integration\Console\Commands\MyDiscovery;
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;

/**
* @internal
* @small
*/
final class DiscoveryCacheCommandTest extends FrameworkIntegrationTestCase
{
public function test_it_clears_discovery_cache(): void
{
MyDiscovery::$cached = false;

$this->kernel->discoveryClasses = [MyDiscovery::class];

$this->console->call('discovery:cache');

$this->assertTrue(MyDiscovery::$cached);
}
}

0 comments on commit f160ea0

Please sign in to comment.