Skip to content

Commit 058fa85

Browse files
authored
Merge pull request #1050 from irazasyed/3.x
Support League Event 2 and 3
2 parents 574ebff + 50d5401 commit 058fa85

15 files changed

+146
-38
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
"guzzlehttp/guzzle": "^7.4.5",
2727
"guzzlehttp/psr7": "^2.4",
2828
"illuminate/support": "9 - 10",
29-
"league/event": "^3.0",
30-
"psr/container": "^2.0"
29+
"league/event": "^2.2 || ^3.0",
30+
"psr/container": "^2.0",
31+
"psr/event-dispatcher": "^1.0"
3132
},
3233
"require-dev": {
3334
"irazasyed/docgen": "^0.2",

src/Events/AbstractEvent.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
if (class_exists(\League\Event\AbstractEvent::class)) {
6+
abstract class AbstractEvent extends \League\Event\AbstractEvent implements HasEventName
7+
{
8+
public function eventName(): string
9+
{
10+
return $this->getName();
11+
}
12+
}
13+
} else {
14+
abstract class AbstractEvent
15+
{
16+
}
17+
}

src/Events/Emitter.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
class Emitter extends \League\Event\Emitter implements EventDispatcherListenerContract
6+
{
7+
public function subscribeTo(string $event, callable $listener, int $priority = 0): void
8+
{
9+
$this->addListener($event, $listener, $priority);
10+
}
11+
12+
public function dispatch(object $event): object
13+
{
14+
return $this->emit($event);
15+
}
16+
}

src/Events/EventDispatcherFactory.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
use League\Event\EventDispatcher;
6+
use League\Event\PrioritizedListenerRegistry;
7+
8+
class EventDispatcherFactory
9+
{
10+
public static function create(): EventDispatcherListenerContract
11+
{
12+
if (class_exists(EventDispatcher::class)) {
13+
return new LeagueEventDispatcher(new PrioritizedListenerRegistry());
14+
}
15+
16+
return new Emitter();
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
use Psr\EventDispatcher\EventDispatcherInterface;
6+
7+
interface EventDispatcherListenerContract extends EventDispatcherInterface
8+
{
9+
public function subscribeTo(string $event, callable $listener, int $priority = 0): void;
10+
}

src/Events/HasEventDispatcher.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22

33
namespace Telegram\Bot\Events;
44

5-
use League\Event\EventDispatcherAwareBehavior;
6-
75
trait HasEventDispatcher
86
{
9-
use EventDispatcherAwareBehavior;
7+
/**
8+
* @var EventDispatcherListenerContract|null
9+
*/
10+
protected $dispatcher;
11+
12+
public function useEventDispatcher(EventDispatcherListenerContract $emitter): void
13+
{
14+
$this->dispatcher = $emitter;
15+
}
16+
17+
public function eventDispatcher(): EventDispatcherListenerContract
18+
{
19+
if ($this->dispatcher === null) {
20+
$this->dispatcher = EventDispatcherFactory::create();
21+
}
22+
23+
return $this->dispatcher;
24+
}
1025

1126
public function hasEventDispatcher(): bool
1227
{
1328
return $this->eventDispatcher() !== null;
1429
}
30+
31+
public function on(string $event, callable $listener, int $priority = 0): void
32+
{
33+
$this->eventDispatcher()->subscribeTo($event, $listener, $priority);
34+
}
1535
}

src/Events/HasEventName.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
use League\Event\EventDispatcher;
6+
7+
if (class_exists(EventDispatcher::class)) {
8+
interface HasEventName extends \League\Event\HasEventName
9+
{
10+
}
11+
} else {
12+
interface HasEventName
13+
{
14+
public function eventName(): string;
15+
}
16+
}

src/Events/LeagueEventDispatcher.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Telegram\Bot\Events;
4+
5+
use League\Event\EventDispatcher;
6+
7+
class LeagueEventDispatcher extends EventDispatcher implements EventDispatcherListenerContract
8+
{
9+
}

src/Events/UpdateEvent.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Telegram\Bot\Events;
44

5-
use League\Event\HasEventName;
65
use Telegram\Bot\Api;
76
use Telegram\Bot\Objects\Update;
87

9-
final class UpdateEvent implements HasEventName
8+
final class UpdateEvent extends AbstractEvent implements HasEventName
109
{
1110
/**
1211
* @var string
@@ -19,12 +18,17 @@ public function __construct(
1918
/**
2019
* @deprecated Will be removed in SDK v4
2120
*/
22-
private string $name = self::NAME
21+
protected string $name = self::NAME
2322
) {
2423
}
2524

2625
public function eventName(): string
2726
{
2827
return $this->name;
2928
}
29+
30+
public function getName(): string
31+
{
32+
return $this->name;
33+
}
3034
}

src/Events/UpdateWasReceived.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,33 @@
22

33
namespace Telegram\Bot\Events;
44

5-
use League\Event\HasEventName;
65
use Telegram\Bot\Api;
76
use Telegram\Bot\Objects\Update;
87

98
/**
109
* Class UpdateWasReceived.
1110
*/
12-
final class UpdateWasReceived implements HasEventName
11+
final class UpdateWasReceived extends AbstractEvent
1312
{
14-
/**
15-
* @var string
16-
*/
17-
private const NAME = 'update.received';
18-
1913
/**
2014
* UpdateWasReceived constructor.
2115
*/
22-
public function __construct(private Update $update, private Api $telegram)
23-
{
24-
}
25-
26-
public function update(): Update
16+
public function __construct(public Api $telegram, public Update $update)
2717
{
28-
return $this->update;
2918
}
3019

31-
public function telegram(): Api
20+
public function eventName(): string
3221
{
33-
return $this->telegram;
22+
return self::class;
3423
}
3524

36-
public function eventName(): string
25+
/**
26+
* Backwards compatibility method
27+
*
28+
* @deprecated use eventName instead
29+
*/
30+
public function getName(): string
3731
{
38-
return self::NAME;
32+
return self::class;
3933
}
4034
}

src/Keyboard/Base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Base extends Collection
2121
* @param string $method
2222
* @return $this
2323
*/
24-
public function __call($method, array $parameters)
24+
public function __call($method, $parameters)
2525
{
2626
if (! Str::startsWith($method, 'set')) {
2727
return parent::__call($method, $parameters);

src/Laravel/Facades/Telegram.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
* @method static void hasMacro($name)
2929
* @method static void flushMacros()
3030
* @method static void macroCall($method, $parameters)
31+
* @method static void useEventDispatcher(\Telegram\Bot\Events\EventDispatcherListenerContract $emitter)
32+
* @method static \Telegram\Bot\Events\EventDispatcherListenerContract eventDispatcher()
3133
* @method static bool hasEventDispatcher()
32-
* @method static void useEventDispatcher(\League\Event\EventDispatcher $emitter)
33-
* @method static \League\Event\EventDispatcher eventDispatcher()
34+
* @method static void on(string $event, callable $listener, int $priority = 0)
3435
* @method static \Telegram\Bot\Api setAsyncRequest(bool $isAsyncRequest)
3536
* @method static \Telegram\Bot\Api setHttpClientHandler(\Telegram\Bot\HttpClients\HttpClientInterface $httpClientHandler)
3637
* @method static \Telegram\Bot\Api setBaseBotUrl(string $baseBotUrl)

src/Methods/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function dispatchUpdateEvent(UpdateObject $update): void
206206

207207
$dispatcher = $this->eventDispatcher();
208208

209-
$dispatcher->dispatch(new UpdateWasReceived($update, $this));
209+
$dispatcher->dispatch(new UpdateWasReceived($this, $update));
210210
$dispatcher->dispatch(new UpdateEvent($this, $update));
211211

212212
$updateType = $update->objectType();

tests/Fixtures/Events/ListenerSpy.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Telegram\Bot\Tests\Fixtures\Events;
44

5-
use League\Event\Listener;
6-
7-
class ListenerSpy implements Listener
5+
class ListenerSpy
86
{
97
public array $events = [];
108

tests/Integration/Api.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,31 +299,35 @@ function createIncomeWebhookRequestInstance(array $updateData): Request
299299
$this->assertStringContainsString('THISISSOMERANDOMKEYDATA', $response1);
300300
});
301301

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

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

307308
$incomeWebhookRequest = createIncomeWebhookRequestInstance([]);
308309

309-
$update = $this->api->getWebhookUpdate(true, $incomeWebhookRequest);
310+
$update = $api->getWebhookUpdate(true, $incomeWebhookRequest);
310311

311312
expect($update)->toBeEmpty()
312-
->and($listener->numberOfTimeCalled())->toBeOne();
313+
->and($listener->numberOfTimeCalled())->toBe(1);
313314
});
314315

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

318-
$this->api->eventDispatcher()->subscribeTo(UpdateEvent::class, $listener);
319+
$api = api($this->httpClient);
320+
$api->on('update', $listener);
321+
$api->on('message', $listener);
322+
$api->on('message.text', $listener);
319323

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

326-
$this->api->getWebhookUpdate(true, $incomeWebhookRequest);
330+
$api->getWebhookUpdate(true, $incomeWebhookRequest);
327331

328332
$allEvents = $listener->events;
329333

0 commit comments

Comments
 (0)