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 all commits
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
29 changes: 28 additions & 1 deletion src/Extend/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
class Event implements ExtenderInterface
{
private $listeners = [];
private $subscribers = [];

/**
* Add a listener to a domain event dispatched by flarum or a flarum extension.
*
* The listener can either be:
* - a callback function or
* - a callback function
* - the class attribute of a class with a public `handle` method, which accepts an instance of the event as a parameter
* - an array, where the first argument is an object or class name, and the second argument is the method on the
* first argument that should be executed as the listener
*
* @param string $event
* @param callable|string $listener
Expand All @@ -34,12 +37,36 @@ public function listen(string $event, $listener)
return $this;
}

/**
* Add a subscriber for a set of domain events dispatched by flarum or a flarum extension.
* Event subscribers are classes that may subscribe to multiple events from within the subscriber class itself,
* allowing you to define several event handlers within a single class.
*
* @see https://laravel.com/docs/6.x/events#writing-event-subscribers
*
* @param string $subscriber: The class attribute of the subscriber class
*/
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);
}
});
}
}
55 changes: 53 additions & 2 deletions tests/integration/extenders/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
namespace Flarum\Tests\integration\extenders;

use Flarum\Extend;
use Flarum\Foundation\Application;
use Flarum\Group\Command\CreateGroup;
use Flarum\Group\Event\Created;
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 @@ -24,7 +26,7 @@ class EventTest extends TestCase

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 @@ -72,6 +74,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 @@ -88,3 +116,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(Application $app, TranslatorInterface $translator)
{
$this->bootedAtConstruct = $app->isBooted();
$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';
}
}