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

Support League Event 2 and 3 #1050

Merged
merged 6 commits into from
Apr 1, 2023
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.4",
"illuminate/support": "9 - 10",
"league/event": "^3.0",
"psr/container": "^2.0"
"league/event": "^2.2 || ^3.0",
"psr/container": "^2.0",
"psr/event-dispatcher": "^1.0"
},
"require-dev": {
"irazasyed/docgen": "^0.2",
Expand Down
17 changes: 17 additions & 0 deletions src/Events/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Telegram\Bot\Events;

if (class_exists(\League\Event\AbstractEvent::class)) {
abstract class AbstractEvent extends \League\Event\AbstractEvent implements HasEventName
{
public function eventName(): string
{
return $this->getName();
}
}
} else {
abstract class AbstractEvent
{
}
}
16 changes: 16 additions & 0 deletions src/Events/Emitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Telegram\Bot\Events;

class Emitter extends \League\Event\Emitter implements EventDispatcherListenerContract
{
public function subscribeTo(string $event, callable $listener, int $priority = 0): void
{
$this->addListener($event, $listener, $priority);
}

public function dispatch(object $event): object
{
return $this->emit($event);
}
}
18 changes: 18 additions & 0 deletions src/Events/EventDispatcherFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Telegram\Bot\Events;

use League\Event\EventDispatcher;
use League\Event\PrioritizedListenerRegistry;

class EventDispatcherFactory
{
public static function create(): EventDispatcherListenerContract
{
if (class_exists(EventDispatcher::class)) {
return new LeagueEventDispatcher(new PrioritizedListenerRegistry());
}

return new Emitter();
}
}
10 changes: 10 additions & 0 deletions src/Events/EventDispatcherListenerContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Telegram\Bot\Events;

use Psr\EventDispatcher\EventDispatcherInterface;

interface EventDispatcherListenerContract extends EventDispatcherInterface
{
public function subscribeTo(string $event, callable $listener, int $priority = 0): void;
}
26 changes: 23 additions & 3 deletions src/Events/HasEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@

namespace Telegram\Bot\Events;

use League\Event\EventDispatcherAwareBehavior;

trait HasEventDispatcher
{
use EventDispatcherAwareBehavior;
/**
* @var EventDispatcherListenerContract|null
*/
protected $dispatcher;

public function useEventDispatcher(EventDispatcherListenerContract $emitter): void
{
$this->dispatcher = $emitter;
}

public function eventDispatcher(): EventDispatcherListenerContract
{
if ($this->dispatcher === null) {
$this->dispatcher = EventDispatcherFactory::create();
}

return $this->dispatcher;
}

public function hasEventDispatcher(): bool
{
return $this->eventDispatcher() !== null;
}

public function on(string $event, callable $listener, int $priority = 0): void
{
$this->eventDispatcher()->subscribeTo($event, $listener, $priority);
}
}
16 changes: 16 additions & 0 deletions src/Events/HasEventName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Telegram\Bot\Events;

use League\Event\EventDispatcher;

if (class_exists(EventDispatcher::class)) {
interface HasEventName extends \League\Event\HasEventName
{
}
} else {
interface HasEventName
{
public function eventName(): string;
}
}
9 changes: 9 additions & 0 deletions src/Events/LeagueEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Telegram\Bot\Events;

use League\Event\EventDispatcher;

class LeagueEventDispatcher extends EventDispatcher implements EventDispatcherListenerContract
{
}
10 changes: 7 additions & 3 deletions src/Events/UpdateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Telegram\Bot\Events;

use League\Event\HasEventName;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;

final class UpdateEvent implements HasEventName
final class UpdateEvent extends AbstractEvent implements HasEventName
{
/**
* @var string
Expand All @@ -19,12 +18,17 @@ public function __construct(
/**
* @deprecated Will be removed in SDK v4
*/
private string $name = self::NAME
protected string $name = self::NAME
) {
}

public function eventName(): string
{
return $this->name;
}

public function getName(): string
{
return $this->name;
}
}
28 changes: 11 additions & 17 deletions src/Events/UpdateWasReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@

namespace Telegram\Bot\Events;

use League\Event\HasEventName;
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;

/**
* Class UpdateWasReceived.
*/
final class UpdateWasReceived implements HasEventName
final class UpdateWasReceived extends AbstractEvent
{
/**
* @var string
*/
private const NAME = 'update.received';

/**
* UpdateWasReceived constructor.
*/
public function __construct(private Update $update, private Api $telegram)
{
}

public function update(): Update
public function __construct(public Api $telegram, public Update $update)
{
return $this->update;
}

public function telegram(): Api
public function eventName(): string
{
return $this->telegram;
return self::class;
}

public function eventName(): string
/**
* Backwards compatibility method
*
* @deprecated use eventName instead
*/
public function getName(): string
{
return self::NAME;
return self::class;
}
}
2 changes: 1 addition & 1 deletion src/Keyboard/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Base extends Collection
* @param string $method
* @return $this
*/
public function __call($method, array $parameters)
public function __call($method, $parameters)
{
if (! Str::startsWith($method, 'set')) {
return parent::__call($method, $parameters);
Expand Down
5 changes: 3 additions & 2 deletions src/Laravel/Facades/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
* @method static void hasMacro($name)
* @method static void flushMacros()
* @method static void macroCall($method, $parameters)
* @method static void useEventDispatcher(\Telegram\Bot\Events\EventDispatcherListenerContract $emitter)
* @method static \Telegram\Bot\Events\EventDispatcherListenerContract eventDispatcher()
* @method static bool hasEventDispatcher()
* @method static void useEventDispatcher(\League\Event\EventDispatcher $emitter)
* @method static \League\Event\EventDispatcher eventDispatcher()
* @method static void on(string $event, callable $listener, int $priority = 0)
* @method static \Telegram\Bot\Api setAsyncRequest(bool $isAsyncRequest)
* @method static \Telegram\Bot\Api setHttpClientHandler(\Telegram\Bot\HttpClients\HttpClientInterface $httpClientHandler)
* @method static \Telegram\Bot\Api setBaseBotUrl(string $baseBotUrl)
Expand Down
2 changes: 1 addition & 1 deletion src/Methods/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected function dispatchUpdateEvent(UpdateObject $update): void

$dispatcher = $this->eventDispatcher();

$dispatcher->dispatch(new UpdateWasReceived($update, $this));
$dispatcher->dispatch(new UpdateWasReceived($this, $update));
$dispatcher->dispatch(new UpdateEvent($this, $update));

$updateType = $update->objectType();
Expand Down
4 changes: 1 addition & 3 deletions tests/Fixtures/Events/ListenerSpy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Telegram\Bot\Tests\Fixtures\Events;

use League\Event\Listener;

class ListenerSpy implements Listener
class ListenerSpy
{
public array $events = [];

Expand Down
16 changes: 10 additions & 6 deletions tests/Integration/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,31 +299,35 @@ function createIncomeWebhookRequestInstance(array $updateData): Request
$this->assertStringContainsString('THISISSOMERANDOMKEYDATA', $response1);
});

test('check the webhook works and can emmit an event', function () {
test('check the webhook works and can dispatch an event', function () {
$listener = new ListenerSpy();

$this->api->eventDispatcher()->subscribeTo(UpdateWasReceived::class, $listener);
$api = api($this->httpClient);
$api->on(UpdateWasReceived::class, $listener);

$incomeWebhookRequest = createIncomeWebhookRequestInstance([]);

$update = $this->api->getWebhookUpdate(true, $incomeWebhookRequest);
$update = $api->getWebhookUpdate(true, $incomeWebhookRequest);

expect($update)->toBeEmpty()
->and($listener->numberOfTimeCalled())->toBeOne();
->and($listener->numberOfTimeCalled())->toBe(1);
});

it('dispatches 3 events of update event type', function () {
$listener = new ListenerSpy();

$this->api->eventDispatcher()->subscribeTo(UpdateEvent::class, $listener);
$api = api($this->httpClient);
$api->on('update', $listener);
$api->on('message', $listener);
$api->on('message.text', $listener);

$incomeWebhookRequest = createIncomeWebhookRequestInstance([
'message' => [ // to help SDK to detect Update of "message" type and send 2nd event (with name "message")
'text' => 'any', // to help SDK to detect message type and send 3rd event (with name "message.text")
],
]);

$this->api->getWebhookUpdate(true, $incomeWebhookRequest);
$api->getWebhookUpdate(true, $incomeWebhookRequest);

$allEvents = $listener->events;

Expand Down