|
| 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 | +} |
0 commit comments