Skip to content

Commit

Permalink
feat(console): add make:config command (tempestphp#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
gturpin-dev authored Dec 17, 2024
1 parent 2e69390 commit d0f3f53
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 31 deletions.
74 changes: 74 additions & 0 deletions src/Tempest/Console/src/Commands/MakeConfigCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Commands;

use InvalidArgumentException;
use Tempest\Console\ConsoleArgument;
use Tempest\Console\ConsoleCommand;
use Tempest\Console\Enums\ConfigType;
use Tempest\Core\PublishesFiles;
use Tempest\Generation\DataObjects\StubFile;
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
use Tempest\Generation\Exceptions\FileGenerationFailedException;
use function Tempest\Support\str;

final class MakeConfigCommand
{
use PublishesFiles;

#[ConsoleCommand(
name: 'make:config',
description: 'Creates a new config class',
aliases: ['config:make', 'config:create', 'create:config'],
)]
public function __invoke(
#[ConsoleArgument(
name: 'type',
help: 'The type of the config to create',
)]
ConfigType $configType,
): void {
try {
$stubFile = $this->getStubFileFromConfigType($configType);
$suggestedPath = str($this->getSuggestedPath('Dummy'))
->replace('Dummy', $configType->value . '.config')
->toString();
$targetPath = $this->promptTargetPath($suggestedPath);
$shouldOverride = $this->askForOverride($targetPath);

$this->stubFileGenerator->generateRawFile(
stubFile: $stubFile,
targetPath: $targetPath,
shouldOverride: $shouldOverride,
);

$this->success(sprintf('Middleware successfully created at "%s".', $targetPath));
} catch (FileGenerationAbortedException|FileGenerationFailedException|InvalidArgumentException $e) {
$this->error($e->getMessage());
}
}

private function getStubFileFromConfigType(ConfigType $configType): StubFile
{
try {
$stubPath = dirname(__DIR__) . '/Stubs';

return match ($configType) {
ConfigType::CONSOLE => StubFile::from($stubPath . '/console.config.stub.php'),
ConfigType::CACHE => StubFile::from($stubPath . '/cache.config.stub.php'),
ConfigType::LOG => StubFile::from($stubPath . '/log.config.stub.php'),
ConfigType::COMMAND_BUS => StubFile::from($stubPath . '/command-bus.config.stub.php'),
ConfigType::EVENT_BUS => StubFile::from($stubPath . '/event-bus.config.stub.php'),
ConfigType::VIEW => StubFile::from($stubPath . '/view.config.stub.php'),
ConfigType::BLADE => StubFile::from($stubPath . '/blade.config.stub.php'),
ConfigType::TWIG => StubFile::from($stubPath . '/twig.config.stub.php'),
ConfigType::DATABASE => StubFile::from($stubPath . '/database.config.stub.php'), // @phpstan-ignore match.alwaysTrue (Because this is a guardrail for the future implementations)
default => throw new InvalidArgumentException(sprintf('The "%s" config type has no supported stub file.', $configType->value)),
};
} catch (InvalidArgumentException $invalidArgumentException) {
throw new FileGenerationFailedException(sprintf('Cannot retrieve stub file: %s', $invalidArgumentException->getMessage()));
}
}
}
21 changes: 21 additions & 0 deletions src/Tempest/Console/src/Enums/ConfigType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tempest\Console\Enums;

/**
* Represents available config types in Tempest.
*/
enum ConfigType: string
{
case DATABASE = 'database';
case TWIG = 'twig';
case BLADE = 'blade';
case VIEW = 'view';
case EVENT_BUS = 'event-bus';
case COMMAND_BUS = 'command-bus';
case LOG = 'log';
case CACHE = 'cache';
case CONSOLE = 'console';
}
12 changes: 12 additions & 0 deletions src/Tempest/Console/src/Stubs/blade.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Tempest\View\Renderers\BladeConfig;

return new BladeConfig(
viewPaths: [
__DIR__ . '/../views/',
],
cachePath: __DIR__ . '/../views/cache/',
);
12 changes: 12 additions & 0 deletions src/Tempest/Console/src/Stubs/cache.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Tempest\Cache\CacheConfig;

return new CacheConfig(
projectCachePool: new FilesystemAdapter(
directory: __DIR__ . '/../../../../.cache',
),
);
11 changes: 11 additions & 0 deletions src/Tempest/Console/src/Stubs/command-bus.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Tempest\CommandBus\CommandBusConfig;

return new CommandBusConfig(
middleware: [
// Add your command bus middleware here.
],
);
21 changes: 21 additions & 0 deletions src/Tempest/Console/src/Stubs/console.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Tempest\Console\ConsoleConfig;
use Tempest\Console\Middleware\ConsoleExceptionMiddleware;
use Tempest\Console\Middleware\HelpMiddleware;
use Tempest\Console\Middleware\InvalidCommandMiddleware;
use Tempest\Console\Middleware\OverviewMiddleware;
use Tempest\Console\Middleware\ResolveOrRescueMiddleware;

return new ConsoleConfig(
name: 'Console Name',
middleware: [
OverviewMiddleware::class,
ConsoleExceptionMiddleware::class,
ResolveOrRescueMiddleware::class,
InvalidCommandMiddleware::class,
HelpMiddleware::class,
],
);
17 changes: 17 additions & 0 deletions src/Tempest/Console/src/Stubs/database.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Tempest\Database\Connections\MySqlConnection;
use Tempest\Database\DatabaseConfig;
use function Tempest\env;

return new DatabaseConfig(
connection: new MySqlConnection(
host: env('DB_HOST'),
port: env('DB_PORT'),
username: env('DB_USERNAME'),
password: env('DB_PASSWORD'),
database: env('DB_DATABASE'),
),
);
11 changes: 11 additions & 0 deletions src/Tempest/Console/src/Stubs/event-bus.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Tempest\EventBus\EventBusConfig;

return new EventBusConfig(
middleware: [
// Add your event bus middleware here.
],
);
15 changes: 15 additions & 0 deletions src/Tempest/Console/src/Stubs/log.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Tempest\Log\Channels\AppendLogChannel;
use Tempest\Log\LogConfig;

return new LogConfig(
channels: [
new AppendLogChannel(
path: __DIR__ . '/../logs/project.log',
),
],
serverLogPath: '/path/to/nginx.log',
);
12 changes: 12 additions & 0 deletions src/Tempest/Console/src/Stubs/twig.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Tempest\View\Renderers\TwigConfig;

return new TwigConfig(
viewPaths: [
__DIR__ . '/../views/',
],
cachePath: __DIR__ . '/../views/cache/',
);
10 changes: 10 additions & 0 deletions src/Tempest/Console/src/Stubs/view.config.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Tempest\View\Renderers\TwigViewRenderer;
use Tempest\View\ViewConfig;

return new ViewConfig(
rendererClass: TwigViewRenderer::class,
);
2 changes: 1 addition & 1 deletion src/Tempest/Database/src/Builder/ModelQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function all(mixed ...$bindings): array
}

/**
* @param \Closure(TModelClass[] $models): void $closure
* @param Closure(TModelClass[] $models): void $closure
*/
public function chunk(Closure $closure, int $amountPerChunk = 200): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
private readonly string $tableName,
private array $statements = [],
private array $createIndexStatements = [],
) {}
) {
}

/** @param class-string<\Tempest\Database\DatabaseModel> $modelClass */
public static function forModel(string $modelClass): self
Expand Down
36 changes: 13 additions & 23 deletions src/Tempest/Database/src/QueryStatements/CreateTableStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
private readonly string $tableName,
private array $statements = [],
private array $indexStatements = [],
) {}
) {
}

/** @param class-string<\Tempest\Database\DatabaseModel> $modelClass */
public static function forModel(string $modelClass): self
Expand All @@ -38,8 +39,7 @@ public function belongsTo(
OnDelete $onDelete = OnDelete::RESTRICT,
OnUpdate $onUpdate = OnUpdate::NO_ACTION,
bool $nullable = false,
): self
{
): self {
[$localTable, $localKey] = explode('.', $local);

$this->integer($localKey, nullable: $nullable);
Expand All @@ -58,8 +58,7 @@ public function text(
string $name,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new TextStatement(
name: $name,
nullable: $nullable,
Expand All @@ -74,8 +73,7 @@ public function varchar(
int $length = 255,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new VarcharStatement(
name: $name,
size: $length,
Expand All @@ -90,8 +88,7 @@ public function char(
string $name,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new CharStatement(
name: $name,
nullable: $nullable,
Expand All @@ -106,8 +103,7 @@ public function integer(
bool $unsigned = false,
bool $nullable = false,
?int $default = null,
): self
{
): self {
$this->statements[] = new IntegerStatement(
name: $name,
unsigned: $unsigned,
Expand All @@ -122,8 +118,7 @@ public function float(
string $name,
bool $nullable = false,
?float $default = null,
): self
{
): self {
$this->statements[] = new FloatStatement(
name: $name,
nullable: $nullable,
Expand All @@ -137,8 +132,7 @@ public function datetime(
string $name,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new DatetimeStatement(
name: $name,
nullable: $nullable,
Expand All @@ -152,8 +146,7 @@ public function date(
string $name,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new DateStatement(
name: $name,
nullable: $nullable,
Expand All @@ -167,8 +160,7 @@ public function boolean(
string $name,
bool $nullable = false,
?bool $default = null,
): self
{
): self {
$this->statements[] = new BooleanStatement(
name: $name,
nullable: $nullable,
Expand All @@ -182,8 +174,7 @@ public function json(
string $name,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new JsonStatement(
name: $name,
nullable: $nullable,
Expand All @@ -198,8 +189,7 @@ public function set(
array $values,
bool $nullable = false,
?string $default = null,
): self
{
): self {
$this->statements[] = new SetStatement(
name: $name,
values: $values,
Expand Down
7 changes: 4 additions & 3 deletions src/Tempest/Generation/src/DataObjects/StubFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Tempest\Generation\DataObjects;

use Exception;
use InvalidArgumentException;
use Nette\InvalidStateException;
use ReflectionException;
use Tempest\Generation\ClassManipulator;
use Tempest\Generation\Enums\StubFileType;

Expand All @@ -32,9 +33,9 @@ public static function from(string $pathOrClass): self
filePath: $pathOrClass,
type: StubFileType::CLASS_FILE,
);
} catch (InvalidStateException) {
} catch (InvalidStateException|ReflectionException) {
if (! file_exists($pathOrClass)) {
throw new Exception(sprintf('The file "%s" does not exist.', $pathOrClass));
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $pathOrClass));
}

return new self(
Expand Down
Loading

0 comments on commit d0f3f53

Please sign in to comment.