mirror of
https://github.com/flarum/framework.git
synced 2024-11-26 18:33:40 +08:00
Add event extender (used for domain events) (#2097)
This commit is contained in:
parent
83e69dc61d
commit
f0adb6a120
45
framework/core/src/Extend/Event.php
Normal file
45
framework/core/src/Extend/Event.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Extend;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Event implements ExtenderInterface
|
||||
{
|
||||
private $listeners = [];
|
||||
|
||||
/**
|
||||
* Add a listener to a domain event dispatched by flarum or a flarum extension.
|
||||
*
|
||||
* The listener can either be:
|
||||
* - a callback function or
|
||||
* - the class attribute of a class with a public `handle` method, which accepts an instance of the event as a parameter
|
||||
*
|
||||
* @param string $event
|
||||
* @param callable $listener
|
||||
*/
|
||||
public function listen(string $event, $listener)
|
||||
{
|
||||
$this->listeners[] = [$event, $listener];
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
84
framework/core/tests/integration/extenders/EventTest.php
Normal file
84
framework/core/tests/integration/extenders/EventTest.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Tests\integration\extenders;
|
||||
|
||||
use Flarum\Extend;
|
||||
use Flarum\Group\Event\Created;
|
||||
use Flarum\Group\Group;
|
||||
use Flarum\Tests\integration\TestCase;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Translation\Translator;
|
||||
|
||||
class EventTest extends TestCase
|
||||
{
|
||||
protected function buildGroup()
|
||||
{
|
||||
$events = $this->app()->getContainer()->make(Dispatcher::class);
|
||||
|
||||
$group = Group::build('test group', 'test groups', '#000000', 'fas fa-crown');
|
||||
$group->save();
|
||||
|
||||
$events->dispatch(new Created($group));
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_doesnt_work_by_default()
|
||||
{
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'test group');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_works_with_closure()
|
||||
{
|
||||
$this->extend((new Extend\Event)->listen(Created::class, function (Created $event) {
|
||||
$event->group->name_singular = 'modified group';
|
||||
}));
|
||||
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'modified group');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function custom_listener_works_with_class_with_handle_method_and_can_inject_stuff()
|
||||
{
|
||||
// Because it injects a translator, this also tests that stuff can be injected into this callback.
|
||||
$this->extend((new Extend\Event)->listen(Created::class, CustomListener::class));
|
||||
|
||||
$group = $this->buildGroup();
|
||||
|
||||
$this->assertEquals($group->name_singular, 'core.group.admin');
|
||||
}
|
||||
}
|
||||
|
||||
class CustomListener
|
||||
{
|
||||
protected $translator;
|
||||
|
||||
public function __construct(Translator $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function handle(Created $event)
|
||||
{
|
||||
$event->group->name_singular = $this->translator->trans('core.group.admin');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user