forked from tempestphp/tempest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): add
make:config
command (tempestphp#863)
- Loading branch information
1 parent
2e69390
commit d0f3f53
Showing
18 changed files
with
348 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.