Skip to content

Commit

Permalink
move language selection to trait for other generators
Browse files Browse the repository at this point in the history
  • Loading branch information
dwjordan committed Oct 30, 2024
1 parent 37c46eb commit 57d4e4b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
39 changes: 7 additions & 32 deletions src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
use Kirschbaum\Paragon\Concerns\Builders\EnumTsBuilder;
use Kirschbaum\Paragon\Concerns\DiscoverEnums;
use Kirschbaum\Paragon\Concerns\GenerateAs;
use Kirschbaum\Paragon\Concerns\HasCommandLineOptions;
use Kirschbaum\Paragon\Concerns\IgnoreParagon;
use Kirschbaum\Paragon\Generators\AbstractEnumGenerator;
use Kirschbaum\Paragon\Generators\EnumGenerator;
use ReflectionEnum;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
use UnitEnum;

#[AsCommand(name: 'paragon:generate-enums', description: 'Generate Typescript versions of existing PHP enums')]
class GenerateEnumsCommand extends Command
{
use HasCommandLineOptions;

/**
* Execute the console command.
*/
Expand Down Expand Up @@ -49,7 +51,7 @@ public function handle(): int
*/
protected function enums(): Collection
{
/** @var string */
/** @var string $phpPath */
$phpPath = config('paragon.enums.paths.php');

return DiscoverEnums::within(app_path($phpPath))
Expand All @@ -73,38 +75,11 @@ protected function enums(): Collection

protected function builder(): EnumBuilder
{
/** @var string */
$generateAs = config('paragon.generate-as');

$builder = match (true) {
$this->option('javascript') => EnumJsBuilder::class,
$this->option('typescript') => EnumTsBuilder::class,
default => GenerateAs::from($generateAs)->builder()
$builder = match ($this->generateAs()) {
GenerateAs::Javascript => EnumJsBuilder::class,
GenerateAs::TypeScript => EnumTsBuilder::class,
};

return app($builder);
}

/**
* Get the console command options.
*
* @return array<int, InputOption>
*/
protected function getOptions(): array
{
return [
new InputOption(
name: 'javascript',
shortcut: 'j',
mode: InputOption::VALUE_NONE,
description: 'Output Javascript files',
),
new InputOption(
name: 'typescript',
shortcut: 't',
mode: InputOption::VALUE_NONE,
description: 'Output TypeScript files',
),
];
}
}
15 changes: 0 additions & 15 deletions src/Concerns/GenerateAs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@

namespace Kirschbaum\Paragon\Concerns;

use Kirschbaum\Paragon\Concerns\Builders\EnumBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumJsBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumTsBuilder;

enum GenerateAs: string
{
case Javascript = 'javascript';

case TypeScript = 'typescript';

/**
* @return class-string<EnumBuilder>
*/
public function builder(): string
{
return match ($this) {
self::Javascript => EnumJsBuilder::class,
self::TypeScript => EnumTsBuilder::class,
};
}
}
40 changes: 40 additions & 0 deletions src/Concerns/HasCommandLineOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Kirschbaum\Paragon\Concerns;

use Symfony\Component\Console\Input\InputOption;

trait HasCommandLineOptions
{
/**
* Get the console command options.
*
* @return array<int, InputOption>
*/
protected function getOptions(): array
{
return [
new InputOption(
name: 'javascript',
shortcut: 'j',
mode: InputOption::VALUE_NONE,
description: 'Output Javascript files',
),
new InputOption(
name: 'typescript',
shortcut: 't',
mode: InputOption::VALUE_NONE,
description: 'Output TypeScript files',
),
];
}

public function generateAs(): GenerateAs
{
return match (true) {
$this->option('javascript') => GenerateAs::Javascript,
$this->option('typescript') => GenerateAs::TypeScript,
default => GenerateAs::from(config('paragon.generate-as')),
};
}
}

0 comments on commit 57d4e4b

Please sign in to comment.