diff --git a/config/paragon.php b/config/paragon.php index 514e003..036ad24 100644 --- a/config/paragon.php +++ b/config/paragon.php @@ -5,10 +5,16 @@ 'abstract-class' => 'Enum', 'paths' => [ - 'php' => '', + 'php' => 'Enums', 'ignore' => [], 'generated' => 'js/enums', 'methods' => 'js/vendors/paragon/enums', ], ], + 'events' => [ + 'paths' => [ + 'php' => 'Events', + 'generated' => 'js/events', + ], + ], ]; diff --git a/src/Commands/GenerateEnumsCommand.php b/src/Commands/GenerateEnumsCommand.php index 7324520..b4ab47a 100644 --- a/src/Commands/GenerateEnumsCommand.php +++ b/src/Commands/GenerateEnumsCommand.php @@ -2,6 +2,7 @@ namespace Kirschbaum\Paragon\Commands; +use Exception; use Illuminate\Console\Command; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -25,11 +26,17 @@ class GenerateEnumsCommand extends Command */ public function handle(): int { - $builder = $this->builder(); + try { + $builder = $this->builder(); - $generatedEnums = $this->enums() - ->map(fn ($enum) => app(EnumGenerator::class, ['enum' => $enum, 'builder' => $builder])()) - ->filter(); + $generatedEnums = $this->enums() + ->map(fn ($enum) => app(EnumGenerator::class, ['enum' => $enum, 'builder' => $builder])()) + ->filter(); + } catch (Exception $e) { + $this->components->error($e->getMessage()); + + return self::FAILURE; + } $this->components->info("{$generatedEnums->count()} enums have been (re)generated."); diff --git a/src/Commands/GenerateEventsCommand.php b/src/Commands/GenerateEventsCommand.php new file mode 100644 index 0000000..dcd05a1 --- /dev/null +++ b/src/Commands/GenerateEventsCommand.php @@ -0,0 +1,33 @@ +string('paragon.events.paths.php'))); + + app(EventGenerator::class, ['events' => $events])(); + } catch (Exception $e) { + $this->components->error($e->getMessage()); + + return self::FAILURE; + } + + $this->components->info("{$events->count()} events have been (re)generated."); + + return self::SUCCESS; + } +} diff --git a/src/Concerns/CanGetClassFromFile.php b/src/Concerns/CanGetClassFromFile.php new file mode 100644 index 0000000..f8ea28b --- /dev/null +++ b/src/Concerns/CanGetClassFromFile.php @@ -0,0 +1,27 @@ +getRealPath()) + ->replaceFirst(base_path(), '') + ->trim(DIRECTORY_SEPARATOR) + ->replaceLast('.php', '') + ->ucfirst() + ->replace( + search: [DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())) . '\\'], + replace: ['\\', app()->getNamespace()] + ) + ->toString(); + } +} diff --git a/src/Concerns/DiscoverBroadcastEvents.php b/src/Concerns/DiscoverBroadcastEvents.php new file mode 100644 index 0000000..56fac00 --- /dev/null +++ b/src/Concerns/DiscoverBroadcastEvents.php @@ -0,0 +1,43 @@ +files()->in($path)); + } + + /** + * Filter the files down to only concrete classes that implement ShouldBroadcast. + */ + protected static function getBroadcastEvents($files): Collection + { + return collect($files) + ->mapWithKeys(function ($file) { + try { + $reflector = new ReflectionClass($event = static::classFromFile($file)); + } catch (ReflectionException) { + return []; + } + + return $reflector->isInstantiable() + && $reflector->implementsInterface('Illuminate\Contracts\Broadcasting\ShouldBroadcast') + ? [$file->getRealPath() => $event] + : []; + }) + ->sort() + ->filter(); + } +} diff --git a/src/Concerns/DiscoverEnums.php b/src/Concerns/DiscoverEnums.php index cc1b980..07bd358 100644 --- a/src/Concerns/DiscoverEnums.php +++ b/src/Concerns/DiscoverEnums.php @@ -10,6 +10,7 @@ class DiscoverEnums { + use CanGetClassFromFile; /** * Get all the enums by searching the given directory. * @@ -52,24 +53,4 @@ protected static function getEnums(Finder $files): Collection }) ->filter(); } - - /** - * Extract the class name from the given file path. - * - * @return class-string<\UnitEnum> - */ - protected static function classFromFile(SplFileInfo $file): string - { - /** @var class-string<\UnitEnum> */ - return str($file->getRealPath()) - ->replaceFirst(base_path(), '') - ->trim(DIRECTORY_SEPARATOR) - ->replaceLast('.php', '') - ->ucfirst() - ->replace( - search: [DIRECTORY_SEPARATOR, ucfirst(basename(app()->path())) . '\\'], - replace: ['\\', app()->getNamespace()] - ) - ->toString(); - } } diff --git a/src/Generators/EventGenerator.php b/src/Generators/EventGenerator.php new file mode 100644 index 0000000..407875e --- /dev/null +++ b/src/Generators/EventGenerator.php @@ -0,0 +1,62 @@ +files = Storage::createLocalDriver([ + 'root' => resource_path(config('paragon.events.paths.generated')), + ]); + } + + public function __invoke(): bool + { + $this->files->put($this->path(), $this->contents()); + + return true; + } + + /** + * Typescript event file contents. + */ + protected function contents(): string + { + $content = $this->events + ->mapWithKeys(fn ($value, $key) => [str($value)->replace('\\', '.')->toString() => $value]) + ->undot(); + + return str(file_get_contents($this->stubPath())) + ->replace('{{ Events }}', json_encode($content, JSON_PRETTY_PRINT)); + } + + /** + * Get the path to the stubs. + */ + public function stubPath(): string + { + return __DIR__ . '/../../stubs/event.stub'; + } + + /** + * Path where the events will be saved. + */ + protected function path(): string + { + return str('Events.ts'); + } +} diff --git a/src/ParagonServiceProvider.php b/src/ParagonServiceProvider.php index 2b6acae..54cdff7 100644 --- a/src/ParagonServiceProvider.php +++ b/src/ParagonServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Support\ServiceProvider; use Kirschbaum\Paragon\Commands\ClearCacheCommand; use Kirschbaum\Paragon\Commands\GenerateEnumsCommand; +use Kirschbaum\Paragon\Commands\GenerateEventsCommand; use Kirschbaum\Paragon\Commands\MakeEnumMethodCommand; class ParagonServiceProvider extends ServiceProvider @@ -32,6 +33,7 @@ public function boot(): void $this->commands([ ClearCacheCommand::class, GenerateEnumsCommand::class, + GenerateEventsCommand::class, MakeEnumMethodCommand::class, ]); } diff --git a/stubs/event.stub b/stubs/event.stub new file mode 100644 index 0000000..0205d66 --- /dev/null +++ b/stubs/event.stub @@ -0,0 +1 @@ +export default {{ Events }};