Skip to content

Commit

Permalink
working event generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dwjordan committed Oct 29, 2024
1 parent 03b0ec6 commit 3d80697
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Exception;
use Illuminate\Console\Command;
use Kirschbaum\Paragon\Concerns\DiscoverBroadcastEvents;
use Kirschbaum\Paragon\Concerns\HasCommandLineOptions;
use Kirschbaum\Paragon\Generators\EventGenerator;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'paragon:generate-events', description: 'Generate Javascript versions of existing Laravel Events')]
class GenerateEventsCommand extends Command
#[AsCommand(name: 'paragon:generate-broadcast-events', description: 'Generate Typescript/Javascript definitions for Laravel Broadcast Events')]
class GenerateBroadcastEventsCommand extends Command
{
use HasCommandLineOptions;

/**
* Execute the console command.
*/
Expand All @@ -19,7 +22,7 @@ public function handle(): int
try {
$events = DiscoverBroadcastEvents::within(app_path(config()->string('paragon.events.paths.php')));

app(EventGenerator::class, ['events' => $events])();
app(EventGenerator::class, ['events' => $events, 'generateJavascript' => $this->option('javascript')])();
} catch (Exception $e) {
$this->components->error($e->getMessage());

Expand Down
24 changes: 4 additions & 20 deletions src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
use Kirschbaum\Paragon\Concerns\Builders\EnumJsBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumTsBuilder;
use Kirschbaum\Paragon\Concerns\DiscoverEnums;
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;

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

/**
* Execute the console command.
*/
Expand Down Expand Up @@ -78,23 +80,5 @@ protected function builder(): EnumBuilder
return $this->option('javascript')
? app(EnumJsBuilder::class)
: app(EnumTsBuilder::class);

}

/**
* 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',
),
];
}
}
25 changes: 25 additions & 0 deletions src/Concerns/HasCommandLineOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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',
),
];
}
}
29 changes: 23 additions & 6 deletions src/Generators/EventGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EventGenerator
/**
* Create new EventGenerator instance.
*/
public function __construct(protected Collection $events)
public function __construct(protected Collection $events, protected bool $generateJavascript = false)

Check failure on line 20 in src/Generators/EventGenerator.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method Kirschbaum\Paragon\Generators\EventGenerator::__construct() has parameter $events with generic class Illuminate\Support\Collection but does not specify its types: TKey, TValue
{
$this->files = Storage::createLocalDriver([
'root' => resource_path(config('paragon.events.paths.generated')),

Check failure on line 23 in src/Generators/EventGenerator.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Parameter #1 $path of function resource_path expects string, mixed given.
Expand All @@ -36,27 +36,44 @@ public function __invoke(): bool
*/
protected function contents(): string
{
$content = $this->events
$object = $this->events
->mapWithKeys(fn ($value, $key) => [str($value)->replace('\\', '.')->toString() => $value])
->undot();
->undot()
->toJson(JSON_PRETTY_PRINT);

$interface = $this->events
->map(function ($path) {
$parts = explode('\\', $path);
array_pop($parts);

return implode('\\', $parts);
})
->mapWithKeys(fn ($value, $key) => [str($value)->replace('\\', '.')->toString() => ['[key:string]' => 'string | { [subKey: string]: string }']])
->undot()
->toJson(JSON_PRETTY_PRINT);

return str(file_get_contents($this->stubPath()))
->replace('{{ Events }}', json_encode($content, JSON_PRETTY_PRINT));
->replace('{{ Interface }}', str($interface)->replace('"', ''))
->replace('{{ Events }}', $object);
}

/**
* Get the path to the stubs.
*/
public function stubPath(): string
{
return __DIR__ . '/../../stubs/event.stub';
return $this->generateJavascript
? __DIR__ . '/../../stubs/event-js.stub'
: __DIR__ . '/../../stubs/event-ts.stub';
}

/**
* Path where the events will be saved.
*/
protected function path(): string
{
return str('Events.ts');
return $this->generateJavascript
? str('Events.js')
: str('Events.ts');
}
}
4 changes: 2 additions & 2 deletions src/ParagonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Support\ServiceProvider;
use Kirschbaum\Paragon\Commands\ClearCacheCommand;
use Kirschbaum\Paragon\Commands\GenerateBroadcastEventsCommand;
use Kirschbaum\Paragon\Commands\GenerateEnumsCommand;
use Kirschbaum\Paragon\Commands\GenerateEventsCommand;
use Kirschbaum\Paragon\Commands\MakeEnumMethodCommand;

class ParagonServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -33,7 +33,7 @@ public function boot(): void
$this->commands([
ClearCacheCommand::class,
GenerateEnumsCommand::class,
GenerateEventsCommand::class,
GenerateBroadcastEventsCommand::class,
MakeEnumMethodCommand::class,
]);
}
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions stubs/event-ts.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface AppEvents {{ Interface }}

const events: AppEvents = {{ Events }};

export default events;

0 comments on commit 3d80697

Please sign in to comment.