Skip to content

Commit d0f3f53

Browse files
authored
feat(console): add make:config command (#863)
1 parent 2e69390 commit d0f3f53

File tree

18 files changed

+348
-31
lines changed

18 files changed

+348
-31
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Commands;
6+
7+
use InvalidArgumentException;
8+
use Tempest\Console\ConsoleArgument;
9+
use Tempest\Console\ConsoleCommand;
10+
use Tempest\Console\Enums\ConfigType;
11+
use Tempest\Core\PublishesFiles;
12+
use Tempest\Generation\DataObjects\StubFile;
13+
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
14+
use Tempest\Generation\Exceptions\FileGenerationFailedException;
15+
use function Tempest\Support\str;
16+
17+
final class MakeConfigCommand
18+
{
19+
use PublishesFiles;
20+
21+
#[ConsoleCommand(
22+
name: 'make:config',
23+
description: 'Creates a new config class',
24+
aliases: ['config:make', 'config:create', 'create:config'],
25+
)]
26+
public function __invoke(
27+
#[ConsoleArgument(
28+
name: 'type',
29+
help: 'The type of the config to create',
30+
)]
31+
ConfigType $configType,
32+
): void {
33+
try {
34+
$stubFile = $this->getStubFileFromConfigType($configType);
35+
$suggestedPath = str($this->getSuggestedPath('Dummy'))
36+
->replace('Dummy', $configType->value . '.config')
37+
->toString();
38+
$targetPath = $this->promptTargetPath($suggestedPath);
39+
$shouldOverride = $this->askForOverride($targetPath);
40+
41+
$this->stubFileGenerator->generateRawFile(
42+
stubFile: $stubFile,
43+
targetPath: $targetPath,
44+
shouldOverride: $shouldOverride,
45+
);
46+
47+
$this->success(sprintf('Middleware successfully created at "%s".', $targetPath));
48+
} catch (FileGenerationAbortedException|FileGenerationFailedException|InvalidArgumentException $e) {
49+
$this->error($e->getMessage());
50+
}
51+
}
52+
53+
private function getStubFileFromConfigType(ConfigType $configType): StubFile
54+
{
55+
try {
56+
$stubPath = dirname(__DIR__) . '/Stubs';
57+
58+
return match ($configType) {
59+
ConfigType::CONSOLE => StubFile::from($stubPath . '/console.config.stub.php'),
60+
ConfigType::CACHE => StubFile::from($stubPath . '/cache.config.stub.php'),
61+
ConfigType::LOG => StubFile::from($stubPath . '/log.config.stub.php'),
62+
ConfigType::COMMAND_BUS => StubFile::from($stubPath . '/command-bus.config.stub.php'),
63+
ConfigType::EVENT_BUS => StubFile::from($stubPath . '/event-bus.config.stub.php'),
64+
ConfigType::VIEW => StubFile::from($stubPath . '/view.config.stub.php'),
65+
ConfigType::BLADE => StubFile::from($stubPath . '/blade.config.stub.php'),
66+
ConfigType::TWIG => StubFile::from($stubPath . '/twig.config.stub.php'),
67+
ConfigType::DATABASE => StubFile::from($stubPath . '/database.config.stub.php'), // @phpstan-ignore match.alwaysTrue (Because this is a guardrail for the future implementations)
68+
default => throw new InvalidArgumentException(sprintf('The "%s" config type has no supported stub file.', $configType->value)),
69+
};
70+
} catch (InvalidArgumentException $invalidArgumentException) {
71+
throw new FileGenerationFailedException(sprintf('Cannot retrieve stub file: %s', $invalidArgumentException->getMessage()));
72+
}
73+
}
74+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Enums;
6+
7+
/**
8+
* Represents available config types in Tempest.
9+
*/
10+
enum ConfigType: string
11+
{
12+
case DATABASE = 'database';
13+
case TWIG = 'twig';
14+
case BLADE = 'blade';
15+
case VIEW = 'view';
16+
case EVENT_BUS = 'event-bus';
17+
case COMMAND_BUS = 'command-bus';
18+
case LOG = 'log';
19+
case CACHE = 'cache';
20+
case CONSOLE = 'console';
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\View\Renderers\BladeConfig;
6+
7+
return new BladeConfig(
8+
viewPaths: [
9+
__DIR__ . '/../views/',
10+
],
11+
cachePath: __DIR__ . '/../views/cache/',
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
6+
use Tempest\Cache\CacheConfig;
7+
8+
return new CacheConfig(
9+
projectCachePool: new FilesystemAdapter(
10+
directory: __DIR__ . '/../../../../.cache',
11+
),
12+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\CommandBus\CommandBusConfig;
6+
7+
return new CommandBusConfig(
8+
middleware: [
9+
// Add your command bus middleware here.
10+
],
11+
);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\Console\ConsoleConfig;
6+
use Tempest\Console\Middleware\ConsoleExceptionMiddleware;
7+
use Tempest\Console\Middleware\HelpMiddleware;
8+
use Tempest\Console\Middleware\InvalidCommandMiddleware;
9+
use Tempest\Console\Middleware\OverviewMiddleware;
10+
use Tempest\Console\Middleware\ResolveOrRescueMiddleware;
11+
12+
return new ConsoleConfig(
13+
name: 'Console Name',
14+
middleware: [
15+
OverviewMiddleware::class,
16+
ConsoleExceptionMiddleware::class,
17+
ResolveOrRescueMiddleware::class,
18+
InvalidCommandMiddleware::class,
19+
HelpMiddleware::class,
20+
],
21+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\Database\Connections\MySqlConnection;
6+
use Tempest\Database\DatabaseConfig;
7+
use function Tempest\env;
8+
9+
return new DatabaseConfig(
10+
connection: new MySqlConnection(
11+
host: env('DB_HOST'),
12+
port: env('DB_PORT'),
13+
username: env('DB_USERNAME'),
14+
password: env('DB_PASSWORD'),
15+
database: env('DB_DATABASE'),
16+
),
17+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\EventBus\EventBusConfig;
6+
7+
return new EventBusConfig(
8+
middleware: [
9+
// Add your event bus middleware here.
10+
],
11+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\Log\Channels\AppendLogChannel;
6+
use Tempest\Log\LogConfig;
7+
8+
return new LogConfig(
9+
channels: [
10+
new AppendLogChannel(
11+
path: __DIR__ . '/../logs/project.log',
12+
),
13+
],
14+
serverLogPath: '/path/to/nginx.log',
15+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tempest\View\Renderers\TwigConfig;
6+
7+
return new TwigConfig(
8+
viewPaths: [
9+
__DIR__ . '/../views/',
10+
],
11+
cachePath: __DIR__ . '/../views/cache/',
12+
);

0 commit comments

Comments
 (0)