Skip to content

Commit 6155fa6

Browse files
committed
event
1 parent 8850620 commit 6155fa6

File tree

8 files changed

+45
-76
lines changed

8 files changed

+45
-76
lines changed

app/Controller/TestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ public function event($request, $response): array
197197
$params = [
198198
'test' => 23,
199199
];
200-
201-
event(TestEvent::preFoo, $params);
200+
event(new TestEvent($params),TestEvent::NAME);
201+
// 初始化事件分发器
202202
return [];
203203
}
204204

app/Event/TestEvent.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
11
<?php
2-
3-
declare(strict_types=1);
2+
declare(strict_types = 1);
43

54
namespace App\Event;
65

7-
use Doctrine\Common\EventArgs;
8-
use Doctrine\Common\EventManager;
6+
use Symfony\Contracts\EventDispatcher\Event;
97

108
/**
119
* Class TestEvent
12-
* @package app\Event
13-
* @see https://github.com/inhere/php-event-manager
10+
* @see https://code.tutsplus.com/tutorials/handling-events-in-your-php-applications-using-the-symfony-eventdispatcher-component--cms-31328
1411
*/
15-
final class TestEvent
12+
class TestEvent extends Event
1613
{
17-
public const preFoo = 'preFoo';
18-
public const postFoo = 'postFoo';
19-
20-
/** @var EventManager */
21-
private EventManager $eventManager;
22-
23-
/** @var bool */
24-
public bool $preFooInvoked = false;
14+
public const NAME = 'order.placed';
2515

26-
/** @var bool */
27-
public bool $postFooInvoked = false;
28-
29-
public function __construct(EventManager $eventManager)
30-
{
31-
$eventManager->addEventListener([self::preFoo, self::postFoo], $this);
32-
}
16+
protected $params;
3317

34-
public function preFoo(EventArgs $eventArgs): void
18+
public function __construct($params)
3519
{
36-
var_dump($eventArgs);
37-
echo 111;
38-
echo '<br />';
39-
$this->preFooInvoked = true;
20+
$this->params = $params;
4021
}
4122

42-
public function postFoo(EventArgs $eventArgs): void
23+
public function getParams()
4324
{
44-
echo 222;
45-
$this->postFooInvoked = true;
25+
return $this->params;
4626
}
47-
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace App\Listener;
5+
6+
7+
interface BaseListenerInterface
8+
{
9+
public function process(object $event);
10+
}

app/Listener/TestEventListener.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
<?php
2-
3-
declare(strict_types=1);
2+
declare(strict_types = 1);
43

54
namespace App\Listener;
65

7-
use Doctrine\Common\EventSubscriber;
6+
use App\Event\TestEvent;
7+
use Symfony\Contracts\EventDispatcher\Event;
88

9-
/**
10-
* 事件
11-
* @see https://www.doctrine-project.org/projects/doctrine-event-manager/en/latest/reference/index.html#setup
12-
*/
13-
final class TestEventListener implements EventSubscriber
9+
class TestEventListener implements BaseListenerInterface
1410
{
15-
/** @var bool */
16-
public bool $preFooInvoked = false;
17-
18-
public function preFoo(): void
19-
{
20-
$this->preFooInvoked = true;
21-
}
22-
23-
public function getSubscribedEvents(): array
11+
public function process(object $event)
2412
{
25-
return [];
26-
// return [TestEvent::preFoo];
13+
echo '打印参数'.PHP_EOL;
14+
var_dump($event->getParams());
2715
}
28-
}
16+
}

app/helpers.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,10 @@ function containerNew($name)
415415
}
416416
}
417417

418-
function event(string $eventName = '', array $eventArgs = []): bool
418+
function event($event,string $eventName = ''): bool
419419
{
420-
$eventManager = EventInstance::instance();
421-
422-
// EventServiceProvider 中配置
423-
// $testEvent = new TestEvent($eventManager);
424-
// $eventManager->addEventSubscriber(new TestEventListener());
425-
426-
$eventArgsObj = EventArgs::getEmptyInstance();
427-
$eventArgsObj->params = $eventArgs;
428-
$eventManager->dispatchEvent($eventName, $eventArgsObj);
420+
$dispatcher = EventInstance::instance();
421+
$dispatcher->dispatch($event, $eventName);
429422
return true;
430423
}
431424

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "dcrswoole/framework",
2+
"name": "dcr_swoole/framework",
33
"description": " PHP coroutine framework.",
44
"type": "project",
55
"license": "MIT",
@@ -29,7 +29,6 @@
2929
"ext-simplexml": "*",
3030
"ext-swoole": ">=4.8",
3131
"doctrine/annotations": "^2.0",
32-
"doctrine/event-manager": "^1.1",
3332
"doctrine/migrations": "^3.5",
3433
"elasticsearch/elasticsearch": "7.16",
3534
"firebase/php-jwt": "^6.3",

dcr/Event/EventInstance.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace DcrSwoole\Event;
66

7-
use Doctrine\Common\EventManager;
7+
use Symfony\Component\EventDispatcher\EventDispatcher;
88
use Exception;
99

1010
/**
@@ -13,18 +13,16 @@
1313
*/
1414
class EventInstance
1515
{
16-
/**
17-
* @var EventManager
18-
*/
16+
1917
public static $eventManager;
2018

2119
/**
2220
* @throws Exception
2321
*/
24-
public static function instance(): EventManager
22+
public static function instance(): EventDispatcher
2523
{
2624
if (!self::$eventManager) {
27-
$ins = new EventManager();
25+
$ins = new EventDispatcher;
2826
self::$eventManager = $ins;
2927
return self::$eventManager;
3028
}

dcr/Framework/Boostrap.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ protected function loadDotEnv(): void
113113
*/
114114
protected function loadEvents(): void
115115
{
116-
$eventManager = EventInstance::instance();
116+
$dispatcher = EventInstance::instance();
117117
$configs = EventServiceProvider::getEventConfig();
118-
119-
foreach ($configs as $event => $listeners) {
120-
new $event($eventManager);
121-
$eventManager->addEventSubscriber(new $listeners());
118+
foreach($configs as $eventClass =>$listenerClass)
119+
{
120+
$listener = new $listenerClass();
121+
$dispatcher->addListener($eventClass::NAME, [$listener, 'process']);
122122
}
123+
123124
}
124125

125126
protected function bootGuzzle(): void

0 commit comments

Comments
 (0)