Skip to content

Commit cf354b7

Browse files
gturpin-devbrendt
andauthored
feat(console): add make:initializer command (#771)
Co-authored-by: Brent Roose <[email protected]>
1 parent acbc6aa commit cf354b7

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Container\Commands;
6+
7+
use Tempest\Console\ConsoleArgument;
8+
use Tempest\Console\ConsoleCommand;
9+
use Tempest\Container\Singleton;
10+
use Tempest\Container\Stubs\InitializerStub;
11+
use Tempest\Core\PublishesFiles;
12+
use Tempest\Generation\ClassManipulator;
13+
use Tempest\Generation\DataObjects\StubFile;
14+
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
15+
use Tempest\Generation\Exceptions\FileGenerationFailedException;
16+
17+
final class MakeInitializerCommand
18+
{
19+
use PublishesFiles;
20+
21+
#[ConsoleCommand(
22+
name: 'make:initializer',
23+
description: 'Creates a new initializer class',
24+
aliases: ['initializer:make', 'initializer:create', 'create:initializer'],
25+
)]
26+
public function __invoke(
27+
#[ConsoleArgument(
28+
help: 'The name of the initializer class to create',
29+
)]
30+
string $className,
31+
#[ConsoleArgument(
32+
name: 'singleton',
33+
help: 'Whether the initializer should be a singleton',
34+
)]
35+
bool $isSingleton = false,
36+
): void {
37+
$suggestedPath = $this->getSuggestedPath($className);
38+
$targetPath = $this->promptTargetPath($suggestedPath);
39+
$shouldOverride = $this->askForOverride($targetPath);
40+
41+
try {
42+
$this->stubFileGenerator->generateClassFile(
43+
stubFile: StubFile::from(InitializerStub::class),
44+
targetPath: $targetPath,
45+
shouldOverride: $shouldOverride,
46+
manipulations: [
47+
function (ClassManipulator $stubClass) use ($isSingleton) {
48+
if ($isSingleton) {
49+
$stubClass->addMethodAttribute('initialize', Singleton::class);
50+
}
51+
52+
return $stubClass;
53+
},
54+
]
55+
);
56+
57+
$this->console->success(sprintf('Initializer successfully created at "%s".', $targetPath));
58+
} catch (FileGenerationAbortedException|FileGenerationFailedException $e) {
59+
$this->console->error($e->getMessage());
60+
}
61+
}
62+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Container\Stubs;
6+
7+
use Tempest\Container\Container;
8+
use Tempest\Container\Initializer;
9+
10+
final class InitializerStub implements Initializer
11+
{
12+
public function initialize(Container $container): mixed
13+
{
14+
return null;
15+
}
16+
}

src/Tempest/Database/src/Commands/MakeModelCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function __invoke(
3232
$shouldOverride = $this->askForOverride($targetPath);
3333

3434
try {
35-
3635
$this->stubFileGenerator->generateClassFile(
3736
stubFile: StubFile::from(DatabaseModelStub::class),
3837
targetPath: $targetPath,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Container\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 MakeInitializerCommandTest 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+
#[Test]
35+
#[DataProvider('command_input_provider')]
36+
public function make_command(
37+
string $commandArgs,
38+
string $expectedPath,
39+
string $expectedNamespace
40+
): void {
41+
$this->console
42+
->call("make:initializer {$commandArgs}")
43+
->submit();
44+
45+
$this->installer
46+
->assertFileExists($expectedPath)
47+
->assertFileContains($expectedPath, 'namespace ' . $expectedNamespace . ';')
48+
->assertFileExists($expectedPath, 'implements Initializer')
49+
->assertFileContains($expectedPath, 'public function initialize');
50+
}
51+
52+
public static function command_input_provider(): array
53+
{
54+
return [
55+
'make_with_defaults' => [
56+
'commandArgs' => 'BookInitializer',
57+
'expectedPath' => 'App/BookInitializer.php',
58+
'expectedNamespace' => 'App',
59+
],
60+
'make_with_other_namespace' => [
61+
'commandArgs' => 'Initializers\\BookInitializer',
62+
'expectedPath' => 'App/Initializers/BookInitializer.php',
63+
'expectedNamespace' => 'App\\Initializers',
64+
],
65+
'make_with_input_path' => [
66+
'commandArgs' => 'Initializers/BookInitializer',
67+
'expectedPath' => 'App/Initializers/BookInitializer.php',
68+
'expectedNamespace' => 'App\\Initializers',
69+
],
70+
];
71+
}
72+
73+
#[Test]
74+
public function make_singleton_command(): void
75+
{
76+
$this->console
77+
->call("make:initializer BookInitializer --singleton")
78+
->submit();
79+
80+
$this->installer
81+
->assertFileExists('App/BookInitializer.php')
82+
->assertFileContains('App/BookInitializer.php', 'namespace App;')
83+
->assertFileContains('App/BookInitializer.php', 'implements Initializer')
84+
->assertFileContains('App/BookInitializer.php', 'public function initialize')
85+
->assertFileContains('App/BookInitializer.php', 'use Tempest\Container\Singleton')
86+
->assertFileContains('App/BookInitializer.php', '#[Singleton]');
87+
}
88+
}

0 commit comments

Comments
 (0)