Skip to content

Commit

Permalink
Merge pull request #216 from apiato/API-1049-Events-Listeners-are-not…
Browse files Browse the repository at this point in the history
…-added-to-EventServiceProvider

fix(generator): Listeners are not added to EventServiceProvider
  • Loading branch information
Mohammad-Alavi authored Dec 13, 2024
2 parents fb015ec + 4fcfe92 commit e20b4b4
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 44 deletions.
61 changes: 45 additions & 16 deletions src/Generator/Commands/ContainerApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContainerApiGenerator extends GeneratorCommand implements ComponentsGenera
['controllertype', null, InputOption::VALUE_OPTIONAL, 'The controller type (SAC, MAC)'],
['events', null, InputOption::VALUE_OPTIONAL, 'Generate Events for this Container?'],
['listeners', null, InputOption::VALUE_OPTIONAL, 'Generate Event Listeners for Events of this Container?'],
['register-listeners', null, InputOption::VALUE_OPTIONAL, 'Register the Event Listeners in the EventServiceProvider?'],
['tests', null, InputOption::VALUE_OPTIONAL, 'Generate Tests for this Container?'],
['maincalled', false, InputOption::VALUE_NONE],
];
Expand Down Expand Up @@ -85,7 +86,7 @@ public function getUserInputs(): array|null
'--section' => $sectionName,
'--container' => $containerName,
'--file' => 'MainServiceProvider',
'--stub' => 'mainserviceprovider',
'--stub' => 'main-service-provider',
]);

$this->printInfoMessage('Generating Model and Repository');
Expand Down Expand Up @@ -133,19 +134,23 @@ public function getUserInputs(): array|null

$generateEvents = $this->checkParameterOrConfirm('events', 'Do you want to generate the corresponding CRUD Events for this Container?', false);
$generateListeners = false;
$registerListeners = false;
if ($generateEvents) {
$generateListeners = $this->checkParameterOrConfirm('listeners', 'Do you want to generate the corresponding Event Listeners for this Events?', false);
if ($generateListeners) {
$registerListeners = $this->checkParameterOrConfirm('register-listeners', 'Do you want the Event Listeners to be registered in the EventServiceProvider?', true);
}
}
$generateTests = $this->checkParameterOrConfirm('tests', 'Do you want to generate the corresponding Tests for this Container?', true);

$generateEvents ?: $this->printInfoMessage('Generating CRUD Events');
$generateListeners ?: $this->printInfoMessage('Generating Event Listeners');
$generateTests ?: $this->printInfoMessage('Generating Tests for Container');
$this->printInfoMessage('Generating Requests for Routes');
$this->printInfoMessage('Generating Default Actions');
$this->printInfoMessage('Generating Default Tasks');
$this->printInfoMessage('Generating Default Controller/s');

$events = [];
$routes = [
[
'stub' => 'List',
Expand All @@ -164,7 +169,7 @@ public function getUserInputs(): array|null
],
],
'functionaltest' => 'List' . $models . 'Test',
'event' => $models . 'ListedEvent',
'event' => $models . 'Listed',
'controller' => 'List' . $models . 'Controller',
],
[
Expand All @@ -184,7 +189,7 @@ public function getUserInputs(): array|null
],
],
'functionaltest' => 'Find' . $model . 'ByIdTest',
'event' => $model . 'FoundByIdEvent',
'event' => $model . 'Requested',
'controller' => 'Find' . $model . 'ByIdController',
],
[
Expand All @@ -204,7 +209,7 @@ public function getUserInputs(): array|null
],
],
'functionaltest' => 'Create' . $model . 'Test',
'event' => $model . 'CreatedEvent',
'event' => $model . 'Created',
'controller' => 'Create' . $model . 'Controller',
],
[
Expand All @@ -224,7 +229,7 @@ public function getUserInputs(): array|null
],
],
'functionaltest' => 'Update' . $model . 'Test',
'event' => $model . 'UpdatedEvent',
'event' => $model . 'Updated',
'controller' => 'Update' . $model . 'Controller',
],
[
Expand All @@ -244,7 +249,7 @@ public function getUserInputs(): array|null
],
],
'functionaltest' => 'Delete' . $model . 'Test',
'event' => $model . 'DeletedEvent',
'event' => $model . 'Deleted',
'controller' => 'Delete' . $model . 'Controller',
],
];
Expand Down Expand Up @@ -283,16 +288,9 @@ public function getUserInputs(): array|null
'--file' => $route['event'],
'--model' => $model,
'--stub' => $route['stub'],
'--listener' => $generateListeners,
]);

$this->printInfoMessage('Generating EventServiceProvider');
$this->call('apiato:generate:provider', [
'--section' => $sectionName,
'--container' => $containerName,
'--file' => 'EventServiceProvider',
'--stub' => 'eventserviceprovider',
'--listener' => false,
]);
$events[] = $route['event'];
}

if ($generateTests) {
Expand Down Expand Up @@ -390,6 +388,37 @@ public function getUserInputs(): array|null
]);
}

if ($generateEvents) {
$listeners = [];
if ($generateListeners) {
$this->printInfoMessage('Generating Event Listeners');
foreach ($events as $event) {
$listener = $event . 'Listener';
$listeners[$listener] = [$event];
$this->call('apiato:generate:listener', [
'--section' => $this->sectionName,
'--container' => $this->containerName,
'--file' => $listener,
'--event' => $event,
]);
}
}

$stub = 'generic-event-service-provider';
if ($generateListeners && $registerListeners) {
$stub = 'event-service-provider-with-listener';
}

$this->printInfoMessage('Generating EventServiceProvider');
$this->call('apiato:generate:provider', [
'--section' => $sectionName,
'--container' => $containerName,
'--file' => 'EventServiceProvider',
'--stub' => $stub,
'--event-listeners' => $listeners,
]);
}

$generateComposerFile = [
'path-parameters' => [
'section-name' => $this->sectionName,
Expand Down
6 changes: 6 additions & 0 deletions src/Generator/Commands/ContainerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ContainerGenerator extends GeneratorCommand implements ComponentsGenerator
['ui', null, InputOption::VALUE_OPTIONAL, 'The user-interface to generate the Controller for.'],
['events', null, InputOption::VALUE_OPTIONAL, 'Generate Events for this Container?'],
['listeners', null, InputOption::VALUE_OPTIONAL, 'Generate Event Listeners for Events of this Container?'],
['register-listeners', null, InputOption::VALUE_OPTIONAL, 'Register the Event Listeners in the EventServiceProvider?'],
['tests', null, InputOption::VALUE_OPTIONAL, 'Generate Tests for this Container?'],
];
/**
Expand Down Expand Up @@ -53,8 +54,12 @@ public function getUserInputs(): array|null
$ui = Str::lower($this->checkParameterOrChoice('ui', 'Select the UI for this container', ['API', 'WEB', 'BOTH'], 0));
$generateEvents = $this->checkParameterOrConfirm('events', 'Do you want to generate the corresponding CRUD Events for this Container?', false);
$generateListeners = false;
$registerListeners = false;
if ($generateEvents) {
$generateListeners = $this->checkParameterOrConfirm('listeners', 'Do you want to generate the corresponding Event Listeners for this Events?', false);
if ($generateListeners) {
$registerListeners = $this->checkParameterOrConfirm('register-listeners', 'Do you want the Event Listeners to be registered in the EventServiceProvider?', true);
}
}
$generateTests = $this->checkParameterOrConfirm('tests', 'Do you want to generate the corresponding Tests for this Container?', true);
if ($generateTests) {
Expand Down Expand Up @@ -114,6 +119,7 @@ public function getUserInputs(): array|null
'--file' => 'composer',
'--events' => $generateEvents,
'--listeners' => $generateListeners,
'--register-listeners' => $registerListeners,
'--tests' => $generateTests,
'--maincalled' => true,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Commands/ContainerWebGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getUserInputs(): array|null
'--section' => $sectionName,
'--container' => $containerName,
'--file' => 'MainServiceProvider',
'--stub' => 'mainserviceprovider',
'--stub' => 'main-service-provider',
]);

$this->printInfoMessage('Generating Model and Repository');
Expand Down
29 changes: 10 additions & 19 deletions src/Generator/Commands/EventGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,21 @@ class EventGenerator extends GeneratorCommand implements ComponentsGenerator
public function getUserInputs(): array|null
{
$model = $this->checkParameterOrAsk('model', 'Enter the name of the Model to generate this Event for', Str::ucfirst($this->containerName));

$listener = $this->checkParameterOrConfirm('listener', 'Do you want to generate a Listener for this Event?', false);
if ($listener) {
// We need to generate a corresponding listener
// so call the other command
$status = $this->call('apiato:generate:listener', [
'--section' => $this->sectionName,
'--container' => $this->containerName,
'--file' => $this->fileName . 'Listener',
'--event' => $this->fileName,
]);

if (0 == $status) {
$this->printInfoMessage('The Listener for Event was successfully generated');
} else {
$this->printErrorMessage('Could not generate the corresponding Listener!');
$listener = $this->option('listener');
if (is_null($listener)) {
$listener = $this->checkParameterOrConfirm('listener', 'Do you want to generate a Listener for this Event?', false);
if ($listener) {
$this->call('apiato:generate:listener', [
'--section' => $this->sectionName,
'--container' => $this->containerName,
'--file' => $this->fileName . 'Listener',
'--event' => $this->fileName,
]);
}
}

$this->printInfoMessage('!!! Do not forget to register the Event and/or EventListener !!!');

$stub = Str::lower($this->option('stub')) ?: 'generic';

// Load a new stub-file based on the users choice
$this->stubName = 'events/' . $stub . '.stub';

return [
Expand Down
70 changes: 63 additions & 7 deletions src/Generator/Commands/ServiceProviderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ServiceProviderGenerator extends GeneratorCommand implements ComponentsGen
*/
public array $inputs = [
['stub', null, InputOption::VALUE_OPTIONAL, 'The stub file to load for this generator.'],
['event-listeners', null, InputOption::VALUE_OPTIONAL, 'The Event Listeners that this Provider should register.'],
];
/**
* The console command name.
Expand Down Expand Up @@ -43,21 +44,74 @@ class ServiceProviderGenerator extends GeneratorCommand implements ComponentsGen
/**
* The name of the stub file.
*/
protected string $stubName = 'providers/mainserviceprovider.stub';
protected string $stubName = 'providers/generic.stub';

private const TAB2 = ' ';
private const TAB3 = ' ';

public function getUserInputs(): array|null
{
$stub = Str::lower(
$this->checkParameterOrChoice(
$stub = $this->option('stub');
if (!$stub) {
$stub = $this->checkParameterOrChoice(
'stub',
'Select the Stub you want to load',
['Generic', 'MainServiceProvider', 'EventServiceProvider', 'MiddlewareServiceProvider'],
0,
),
);
);

$stub = match ($stub) {
'MainServiceProvider' => 'main-service-provider',
'EventServiceProvider' => 'generic-event-service-provider',
'MiddlewareServiceProvide' => 'middleware-service-provider',
default => 'generic',
};
}
$this->stubName = "providers/$stub.stub";
$eventListeners = $this->option('event-listeners');
$eventListenersString = '[]';
$listenersUseStatements = '';
$eventsUseStatements = '';
if ($eventListeners) {
$listenersWithClass = array_map(static function ($listeners, $listener) {
return [$listener . '::class' => array_map(static fn ($event) => $event . '::class', $listeners)];
}, $eventListeners, array_keys($eventListeners));
$eventListenersString = '[' . PHP_EOL . array_reduce($listenersWithClass, static function ($carry, $item) {
$carry .= array_reduce(array_keys($item), static function ($carry, $key) use ($item) {
$carry .= self::TAB2 . $key . ' => [' . PHP_EOL;
$carry .= array_reduce($item[$key], static function ($carry, $event) {
$carry .= self::TAB3 . $event . ',' . PHP_EOL;

return $carry;
});
$carry .= self::TAB2 . '],' . PHP_EOL;

return $carry;
});

return $carry;
}) . ' ]';
$listenersUseStatements = array_reduce(array_keys($eventListeners), function ($carry, $item) {
$carry .= 'use App\Containers\\' . $this->sectionName . '\\' . $this->containerName . '\Listeners\\' . $item . ';' . PHP_EOL;

return $carry;
});

$eventsUseStatements = array_map(function ($listeners, $listener) {
return array_map(fn ($event) => 'use App\Containers\\' . $this->sectionName . '\\' . $this->containerName . '\Events\\' . $event . ';', $listeners);
}, $eventListeners, array_keys($eventListeners));
$eventsUseStatements = array_reduce($eventsUseStatements, static function ($carry, $item) {
$carry .= array_reduce(array_keys($item), static function ($carry, $key) use ($item) {
$carry .= $item[$key] . PHP_EOL;

return $carry;
});

return $carry;
});
}

// load a new stub-file based on the users choice
$this->stubName = 'providers/' . $stub . '.stub';
$useStatements = $eventsUseStatements . $listenersUseStatements;

return [
'path-parameters' => [
Expand All @@ -70,6 +124,8 @@ public function getUserInputs(): array|null
'_container-name' => Str::lower($this->containerName),
'container-name' => $this->containerName,
'class-name' => $this->fileName,
'event-listeners' => $eventListenersString,
'use-statements' => $useStatements,
],
'file-parameters' => [
'file-name' => $this->fileName,
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Stubs/listeners/listener.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class {{class-name}} extends ParentListener implements ShouldQueue
{
}

public function handle({{model}} $event): void
public function __invoke({{model}} $event): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Containers\{{section-name}}\{{container-name}}\Providers;

{{use-statements}}
use App\Ship\Parents\Providers\EventServiceProvider as ParentEventServiceProvider;

class {{class-name}} extends ParentEventServiceProvider
{
protected $listen = {{event-listeners}};
}
25 changes: 25 additions & 0 deletions src/Generator/Stubs/providers/main-service-provider.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Containers\{{section-name}}\{{container-name}}\Providers;

use App\Ship\Parents\Providers\MainServiceProvider as ParentMainServiceProvider;

/**
* The Main Service Provider of this container.
* It will be automatically registered by the framework.
*/
class {{class-name}} extends ParentMainServiceProvider
{
public array $serviceProviders = [
// InternalServiceProviderExample::class,
];

public array $aliases = [
// 'Foo' => Bar::class,
];

public function register(): void
{
parent::register();
}
}

0 comments on commit e20b4b4

Please sign in to comment.