Skip to content

Commit 2bd5814

Browse files
authored
feat(console): add make:discovery command (#1057)
1 parent 0b25975 commit 2bd5814

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Discovery\Commands;
6+
7+
use Tempest\Console\ConsoleArgument;
8+
use Tempest\Console\ConsoleCommand;
9+
use Tempest\Core\PublishesFiles;
10+
use Tempest\Discovery\Stubs\DiscoveryStub;
11+
use Tempest\Generation\DataObjects\StubFile;
12+
13+
final class MakeDiscoveryCommand
14+
{
15+
use PublishesFiles;
16+
17+
#[ConsoleCommand(
18+
name: 'make:discovery',
19+
description: 'Creates a new discovery class',
20+
aliases: ['discovery:make', 'discovery:create', 'create:discovery'],
21+
)]
22+
public function __invoke(
23+
#[ConsoleArgument(description: 'The name of the discovery class to create')]
24+
string $className,
25+
): void {
26+
$suggestedPath = $this->getSuggestedPath($className);
27+
$targetPath = $this->promptTargetPath($suggestedPath);
28+
$shouldOverride = $this->askForOverride($targetPath);
29+
30+
$this->stubFileGenerator->generateClassFile(
31+
stubFile: StubFile::from(DiscoveryStub::class),
32+
targetPath: $targetPath,
33+
shouldOverride: $shouldOverride,
34+
);
35+
36+
$this->console->success(sprintf('File successfully created at <em>%s</em>.', $targetPath));
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Discovery\Stubs;
6+
7+
use Tempest\Discovery\Discovery;
8+
use Tempest\Discovery\DiscoveryLocation;
9+
use Tempest\Discovery\IsDiscovery;
10+
use Tempest\Reflection\ClassReflector;
11+
12+
final class DiscoveryStub implements Discovery
13+
{
14+
use IsDiscovery;
15+
16+
public function discover(DiscoveryLocation $location, ClassReflector $class): void
17+
{
18+
if (! $class->implements('MyClass::class')) {
19+
return;
20+
}
21+
22+
$this->discoveryItems->add($location, $class);
23+
}
24+
25+
public function apply(): void
26+
{
27+
foreach ($this->discoveryItems as $class) {
28+
// Do something with the discovered class
29+
}
30+
}
31+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Discovery\Commands;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Tempest\Core\ComposerNamespace;
10+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
11+
12+
/**
13+
* @internal
14+
*/
15+
class MakeDiscoveryCommandTest extends FrameworkIntegrationTestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->installer->configure(
22+
__DIR__ . '/install',
23+
new ComposerNamespace('App\\', __DIR__ . '/install/App'),
24+
);
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
$this->installer->clean();
30+
31+
parent::tearDown();
32+
}
33+
34+
#[DataProvider('command_input_provider')]
35+
#[Test]
36+
public function make_command(
37+
string $commandArgs,
38+
string $expectedPath,
39+
string $expectedNamespace,
40+
): void {
41+
$this->console
42+
->call("make:discovery {$commandArgs}")
43+
->submit();
44+
45+
$this->installer
46+
->assertFileExists($expectedPath)
47+
->assertFileContains($expectedPath, 'namespace ' . $expectedNamespace . ';')
48+
->assertFileExists($expectedPath, 'implements Discovery')
49+
->assertFileContains($expectedPath, 'public function discover');
50+
}
51+
52+
public static function command_input_provider(): array
53+
{
54+
return [
55+
'make_with_defaults' => [
56+
'commandArgs' => 'CustomDiscovery',
57+
'expectedPath' => 'App/CustomDiscovery.php',
58+
'expectedNamespace' => 'App',
59+
],
60+
'make_with_other_namespace' => [
61+
'commandArgs' => 'CustomDiscoveries\\CustomDiscovery',
62+
'expectedPath' => 'App/CustomDiscoveries/CustomDiscovery.php',
63+
'expectedNamespace' => 'App\\CustomDiscoveries',
64+
],
65+
'make_with_input_path' => [
66+
'commandArgs' => 'CustomDiscoveries/CustomDiscovery',
67+
'expectedPath' => 'App/CustomDiscoveries/CustomDiscovery.php',
68+
'expectedNamespace' => 'App\\CustomDiscoveries',
69+
],
70+
];
71+
}
72+
}

0 commit comments

Comments
 (0)