-
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.
- Loading branch information
Showing
9 changed files
with
187 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Kirschbaum\Paragon\Commands; | ||
|
||
use Exception; | ||
use Illuminate\Console\Command; | ||
use Kirschbaum\Paragon\Concerns\DiscoverBroadcastEvents; | ||
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 | ||
{ | ||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): int | ||
{ | ||
try { | ||
$events = DiscoverBroadcastEvents::within(app_path(config()->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; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Kirschbaum\Paragon\Concerns; | ||
|
||
use const DIRECTORY_SEPARATOR; | ||
|
||
use SplFileInfo; | ||
|
||
trait CanGetClassFromFile | ||
{ | ||
/** | ||
* Extract the class name from the given file path. | ||
*/ | ||
protected static function classFromFile(SplFileInfo $file): string | ||
{ | ||
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(); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace Kirschbaum\Paragon\Concerns; | ||
|
||
use Illuminate\Support\Collection; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class DiscoverBroadcastEvents | ||
{ | ||
use CanGetClassFromFile; | ||
|
||
/** | ||
* Get all the events by searching the given directory. | ||
*/ | ||
public static function within(array|string $path): Collection | ||
{ | ||
return static::getBroadcastEvents(Finder::create()->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(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Kirschbaum\Paragon\Generators; | ||
|
||
use const JSON_PRETTY_PRINT; | ||
|
||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class EventGenerator | ||
{ | ||
protected Filesystem $cache; | ||
|
||
protected Filesystem $files; | ||
|
||
/** | ||
* Create new EventGenerator instance. | ||
*/ | ||
public function __construct(protected Collection $events) | ||
{ | ||
$this->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'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {{ Events }}; |