mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-17 14:45:15 +02:00
Create StaticEventSubscriber
This commit is contained in:
parent
ddcad69ce4
commit
af73575c2d
3 changed files with 55 additions and 12 deletions
|
@ -10,24 +10,24 @@ declare(strict_types = 1);
|
||||||
namespace Friendica\EventSubscriber;
|
namespace Friendica\EventSubscriber;
|
||||||
|
|
||||||
use Friendica\Event\DataFilterEvent;
|
use Friendica\Event\DataFilterEvent;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bridge between the EventDispatcher and the Hook class.
|
* Bridge between the EventDispatcher and the Hook class.
|
||||||
*/
|
*/
|
||||||
final class HookEventSubscriber implements EventSubscriberInterface
|
final class HookEventSubscriber implements StaticEventSubscriber
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return array<string, string>
|
* @return array<string, string>
|
||||||
*/
|
*/
|
||||||
public static function getSubscribedEvents(): array
|
public static function getStaticSubscribedEvents(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
DataFilterEvent::class => 'onDataFilterEvent',
|
DataFilterEvent::class => 'onDataFilterEvent',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onDataFilterEvent(DataFilterEvent $event): void
|
public static function onDataFilterEvent(DataFilterEvent $event): void
|
||||||
{
|
{
|
||||||
|
// Call the Hook class to process the event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
src/EventSubscriber/StaticEventSubscriber.php
Normal file
32
src/EventSubscriber/StaticEventSubscriber.php
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Copyright (C) 2010-2024, the Friendica project
|
||||||
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace Friendica\EventSubscriber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define events that should be reacted to.
|
||||||
|
*/
|
||||||
|
interface StaticEventSubscriber
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Return an array of events to subscribe to.
|
||||||
|
* The key must the event class name.
|
||||||
|
* The value must the method of the implementing class to call.
|
||||||
|
* The method will be called statically with the event class as first parameter.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* ```php
|
||||||
|
* return [Event::class => 'onEvent'];
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @return array<class-string, string>
|
||||||
|
*/
|
||||||
|
public static function getStaticSubscribedEvents(): array;
|
||||||
|
}
|
|
@ -11,26 +11,37 @@ namespace Friendica\Test\Unit\EventSubscriber;
|
||||||
|
|
||||||
use Friendica\Event\DataFilterEvent;
|
use Friendica\Event\DataFilterEvent;
|
||||||
use Friendica\EventSubscriber\HookEventSubscriber;
|
use Friendica\EventSubscriber\HookEventSubscriber;
|
||||||
|
use Friendica\EventSubscriber\StaticEventSubscriber;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
||||||
|
|
||||||
class HookEventSubscriberTest extends TestCase
|
class HookEventSubscriberTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCorrectImplementation(): void
|
public function testCorrectImplementation(): void
|
||||||
{
|
{
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
is_subclass_of(HookEventSubscriber::class, EventSubscriberInterface::class, true),
|
is_subclass_of(HookEventSubscriber::class, StaticEventSubscriber::class, true),
|
||||||
HookEventSubscriber::class . ' does not implement ' . EventSubscriberInterface::class
|
HookEventSubscriber::class . ' does not implement ' . StaticEventSubscriber::class
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSubscribedEvents(): void
|
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
|
||||||
{
|
{
|
||||||
$this->assertSame(
|
$expected = [
|
||||||
[
|
|
||||||
DataFilterEvent::class => 'onDataFilterEvent',
|
DataFilterEvent::class => 'onDataFilterEvent',
|
||||||
],
|
];
|
||||||
HookEventSubscriber::getSubscribedEvents()
|
|
||||||
|
$this->assertSame(
|
||||||
|
$expected,
|
||||||
|
HookEventSubscriber::getStaticSubscribedEvents()
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ($expected as $methodName) {
|
||||||
|
$this->assertTrue(method_exists(HookEventSubscriber::class, $methodName));
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
(new \ReflectionMethod(HookEventSubscriber::class, $methodName))->isStatic(),
|
||||||
|
$methodName . ' is not static'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue