Skip to content

Commit

Permalink
Merge pull request #1 from JonasPardon/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
JonasPardon authored May 9, 2022
2 parents bdc666a + 995e07d commit 2f58548
Show file tree
Hide file tree
Showing 19 changed files with 686 additions and 524 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ name: Check & fix styling

on: [push]

permissions:
actions: write
pull-requests: write
contents: write

jobs:
php-cs-fixer:
runs-on: ubuntu-latest
Expand Down
55 changes: 35 additions & 20 deletions .php_cs.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,44 @@

return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'@PSR2' => true,
'no_whitespace_in_blank_line' => true,
'array_syntax' => [
'syntax' => 'short',
],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => true,
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'single_quote' => true,
'constant_case' => true,
'lowercase_keywords' => true,
'lowercase_static_reference' => true,
'native_function_casing' => true,
'native_function_type_declaration_casing' => true,
'no_useless_else' => true,
'function_declaration' => true,
'function_typehint_space' => true,
'return_type_declaration' => [
'space_before' => 'none',
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
'single_import_per_statement' => true,
'list_syntax' => [
'syntax' => 'short',
],
'single_trait_insert_per_statement' => true,
'clean_namespace' => true,
'logical_operators' => true,
'new_with_braces' => true,
'ternary_to_null_coalescing' => true,
'no_closing_tag' => true,
'array_indentation' => true,
'compact_nullable_typehint' => true,
'no_spaces_around_offset' => true,
'blank_line_after_opening_tag' => false,
])
->setFinder($finder);
5 changes: 5 additions & 0 deletions Makefile
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
"nunomaduro/collision": "^6.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0",
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/VisualizerNodeInterface.php
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;
}
138 changes: 138 additions & 0 deletions src/EventVisualizer.php
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));
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Facades/LaravelEventVisualizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @see \JonasPardon\LaravelEventVisualizer\LaravelEventVisualizer
* @see \JonasPardon\LaravelEventVisualizer\EventVisualizer
*/
class LaravelEventVisualizer extends Facade
{
Expand Down
8 changes: 5 additions & 3 deletions src/Http/Controllers/LaravelEventVisualizerController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace JonasPardon\LaravelEventVisualizer\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\View\View;
use JonasPardon\LaravelEventVisualizer\LaravelEventVisualizer;
use JonasPardon\LaravelEventVisualizer\EventVisualizer;

final class LaravelEventVisualizerController extends Controller
{
public function __construct(
private LaravelEventVisualizer $visualizer,
private EventVisualizer $visualizer,
) {
}

Expand Down
Loading

0 comments on commit 2f58548

Please sign in to comment.