generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JonasPardon/cleanup
Cleanup
- Loading branch information
Showing
19 changed files
with
686 additions
and
524 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,5 @@ | ||
lint: | ||
php vendor/bin/php-cs-fixer fix --allow-risky=yes . | ||
|
||
phpstan: | ||
php vendor/bin/phpstan |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JonasPardon\LaravelEventVisualizer\Contracts; | ||
|
||
interface VisualizerNodeInterface | ||
{ | ||
public function getType(): string; | ||
|
||
public function getName(): string; | ||
|
||
public function getIdentifier(): string; | ||
} |
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,138 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JonasPardon\LaravelEventVisualizer; | ||
|
||
use Closure; | ||
use Illuminate\Support\Str; | ||
use JonasPardon\LaravelEventVisualizer\Models\Event; | ||
use JonasPardon\LaravelEventVisualizer\Models\Job; | ||
use JonasPardon\LaravelEventVisualizer\Models\Listener; | ||
use JonasPardon\LaravelEventVisualizer\Models\VisualizerNode; | ||
use JonasPardon\LaravelEventVisualizer\Services\CodeParser; | ||
|
||
class EventVisualizer | ||
{ | ||
private bool $showLaravelEvents; | ||
private array $classesToIgnore; | ||
private string $eventColor; | ||
private string $listenerColor; | ||
private string $jobColor; | ||
private bool $autoDiscoverJobsAndEvents; | ||
private string $mermaidString = ''; | ||
|
||
public function __construct(private CodeParser $parser) | ||
{ | ||
$this->showLaravelEvents = config('event-visualizer.show_laravel_events', false); | ||
$this->classesToIgnore = config('event-visualizer.classes_to_ignore', []); | ||
$this->eventColor = config('event-visualizer.theme.colors.event', '#55efc4'); | ||
$this->listenerColor = config('event-visualizer.theme.colors.listener', '#74b9ff'); | ||
$this->jobColor = config('event-visualizer.theme.colors.job', '#a29bfe'); | ||
$this->autoDiscoverJobsAndEvents = config('event-visualizer.auto_discover_jobs_and_events', false); | ||
} | ||
|
||
public function getMermaidStringForEvents(): string | ||
{ | ||
$events = $this->getRawAppEvents(); | ||
return $this->buildMermaidString($events); | ||
} | ||
|
||
private function getRawAppEvents(): array | ||
{ | ||
$rawEvents = app()->make('events')->getRawListeners(); | ||
$sanitizedEvents = []; | ||
|
||
foreach ($rawEvents as $event => $rawListeners) { | ||
foreach ($rawListeners as $rawListener) { | ||
if (is_string($rawListener)) { | ||
$sanitizedEvents[$event][] = $rawListener; | ||
} elseif ($rawListener instanceof Closure) { | ||
unset($sanitizedEvents[$event]); | ||
} elseif (is_array($rawListener) && count($rawListener) === 2) { | ||
if (is_object($rawListener[0])) { | ||
$rawListener[0] = get_class($rawListener[0]); | ||
} | ||
|
||
$sanitizedEvents[$event][] = implode('@', $rawListener); | ||
} | ||
} | ||
} | ||
|
||
return $sanitizedEvents; | ||
} | ||
|
||
public function buildMermaidString(array $events): string | ||
{ | ||
foreach ($events as $event => $listeners) { | ||
if (!$this->showLaravelEvents && !Str::startsWith($event, 'App')) { | ||
// Get only our own events, not the default laravel ones | ||
continue; | ||
} | ||
|
||
foreach ($listeners as $listener) { | ||
// todo: this currently only ignores listeners. Should also ignore events and jobs. | ||
if (Str::contains($listener, $this->classesToIgnore)) { | ||
continue; | ||
} | ||
|
||
$this->connectNodes(new Event($event), new Listener($listener)); | ||
} | ||
} | ||
|
||
// Add styling to classes. Classes are defined with the ':::' above | ||
$this->mermaidString .= "classDef event fill:{$this->eventColor};" . PHP_EOL; | ||
$this->mermaidString .= "classDef listener fill:{$this->listenerColor};" . PHP_EOL; | ||
$this->mermaidString .= "classDef job fill:{$this->jobColor};" . PHP_EOL; | ||
|
||
return $this->mermaidString; | ||
} | ||
|
||
private function entryExists(string $entry): bool | ||
{ | ||
return Str::contains($this->mermaidString, $entry); | ||
} | ||
|
||
private function connectNodes(VisualizerNode $from, VisualizerNode $to): void | ||
{ | ||
$entry = $from->toString() . ' --> ' . $to->toString() . PHP_EOL; | ||
|
||
if ($this->entryExists($entry)) { | ||
return; | ||
} | ||
|
||
$this->mermaidString .= $entry; | ||
$this->handleChildren($to); | ||
} | ||
|
||
private function handleChildren(VisualizerNode $parentNode): void | ||
{ | ||
$className = $parentNode->getClassName(); | ||
|
||
if ($this->autoDiscoverJobsAndEvents) { | ||
$this->parser | ||
->getDispatchedJobsFromVisualizerNode($parentNode) | ||
->each(function (Job $job) use ($parentNode) { | ||
$this->connectNodes($parentNode, $job); | ||
}); | ||
|
||
$this->parser | ||
->getDispatchedEventsFromVisualizerNode($parentNode) | ||
->each(function (Event $event) use ($parentNode) { | ||
$this->connectNodes($parentNode, $event); | ||
}); | ||
} else { | ||
if (method_exists($className, 'dispatchesJobs')) { | ||
foreach ($className::dispatchesJobs() as $job) { | ||
$this->connectNodes($parentNode, new Job($job)); | ||
} | ||
} | ||
|
||
if (method_exists($className, 'dispatchesEvents')) { | ||
foreach ($className::dispatchesEvents() as $event) { | ||
$this->connectNodes($parentNode, new Event($event)); | ||
} | ||
} | ||
} | ||
} | ||
} |
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.