Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscribe method to event extender #2535

Merged
merged 4 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Extend/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class Event implements ExtenderInterface
{
private $listeners = [];
private $subscribers = [];

/**
* Add a listener to a domain event dispatched by flarum or a flarum extension.
Expand All @@ -34,12 +35,32 @@ public function listen(string $event, $listener)
return $this;
}

/**
* Add a subscriber for a set of domain events dispatched by flarum or a flarum extension.
*
* @param string $subscriber
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
*/
public function subscribe(string $subscriber)
{
$this->subscribers[] = $subscriber;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$events = $container->make(Dispatcher::class);

foreach ($this->listeners as $listener) {
$events->listen($listener[0], $listener[1]);
}

$app = $container->make('flarum');

$app->booted(function () use ($events) {
foreach ($this->subscribers as $subscriber) {
$events->subscribe($subscriber);
}
});
}
}
54 changes: 52 additions & 2 deletions tests/integration/extenders/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\User;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Bus\Dispatcher as BusDispatcher;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Translation\TranslatorInterface;

class EventTest extends TestCase
Expand All @@ -33,7 +34,7 @@ protected function buildGroup()
],
]);

$bus = $this->app()->getContainer()->make(Dispatcher::class);
$bus = $this->app()->getContainer()->make(BusDispatcher::class);

return $bus->dispatch(
new CreateGroup(User::find(1), ['attributes' => [
Expand Down Expand Up @@ -81,6 +82,32 @@ public function custom_listener_works_with_class_with_handle_method_and_can_inje

$this->assertEquals('core.group.admin', $group->name_singular);
}

/**
* @test
*/
public function custom_subscriber_works()
{
// Because it injects a translator, this also tests that stuff can be injected into this callback.
$this->extend((new Extend\Event)->subscribe(CustomSubscriber::class));

$group = $this->buildGroup();

$this->assertEquals('core.group.admin', $group->name_singular);
}

/**
* @test
*/
public function custom_subscriber_applied_after_app_booted()
{
// Because it injects a translator, this also tests that stuff can be injected into this callback.
$this->extend((new Extend\Event)->subscribe(CustomSubscriber::class));

$group = $this->buildGroup();

$this->assertEquals('booted', $group->name_plural);
}
}

class CustomListener
Expand All @@ -97,3 +124,26 @@ public function handle(Created $event)
$event->group->name_singular = $this->translator->trans('core.group.admin');
}
}

class CustomSubscriber
{
protected $bootedAtConstruct;
protected $translator;

public function __construct(TranslatorInterface $translator)
{
$this->bootedAtConstruct = app('flarum')->isBooted();
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
$this->translator = $translator;
}

public function subscribe(Dispatcher $events)
{
$events->listen(Created::class, [$this, 'whenGroupCreated']);
}

public function whenGroupCreated(Created $event)
{
$event->group->name_singular = $this->translator->trans('core.group.admin');
$event->group->name_plural = $this->bootedAtConstruct ? 'booted' : 'not booted';
}
}