Skip to content

Commit

Permalink
event
Browse files Browse the repository at this point in the history
  • Loading branch information
guanhui07 committed Feb 6, 2023
1 parent 8850620 commit 6155fa6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 76 deletions.
4 changes: 2 additions & 2 deletions app/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public function event($request, $response): array
$params = [
'test' => 23,
];

event(TestEvent::preFoo, $params);
event(new TestEvent($params),TestEvent::NAME);
// 初始化事件分发器
return [];
}

Expand Down
42 changes: 11 additions & 31 deletions app/Event/TestEvent.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace App\Event;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Class TestEvent
* @package app\Event
* @see https://github.com/inhere/php-event-manager
* @see https://code.tutsplus.com/tutorials/handling-events-in-your-php-applications-using-the-symfony-eventdispatcher-component--cms-31328
*/
final class TestEvent
class TestEvent extends Event
{
public const preFoo = 'preFoo';
public const postFoo = 'postFoo';

/** @var EventManager */
private EventManager $eventManager;

/** @var bool */
public bool $preFooInvoked = false;
public const NAME = 'order.placed';

/** @var bool */
public bool $postFooInvoked = false;

public function __construct(EventManager $eventManager)
{
$eventManager->addEventListener([self::preFoo, self::postFoo], $this);
}
protected $params;

public function preFoo(EventArgs $eventArgs): void
public function __construct($params)
{
var_dump($eventArgs);
echo 111;
echo '<br />';
$this->preFooInvoked = true;
$this->params = $params;
}

public function postFoo(EventArgs $eventArgs): void
public function getParams()
{
echo 222;
$this->postFooInvoked = true;
return $this->params;
}
}
}
10 changes: 10 additions & 0 deletions app/Listener/BaseListenerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types = 1);

namespace App\Listener;


interface BaseListenerInterface
{
public function process(object $event);
}
28 changes: 8 additions & 20 deletions app/Listener/TestEventListener.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
<?php

declare(strict_types=1);
declare(strict_types = 1);

namespace App\Listener;

use Doctrine\Common\EventSubscriber;
use App\Event\TestEvent;
use Symfony\Contracts\EventDispatcher\Event;

/**
* 事件
* @see https://www.doctrine-project.org/projects/doctrine-event-manager/en/latest/reference/index.html#setup
*/
final class TestEventListener implements EventSubscriber
class TestEventListener implements BaseListenerInterface
{
/** @var bool */
public bool $preFooInvoked = false;

public function preFoo(): void
{
$this->preFooInvoked = true;
}

public function getSubscribedEvents(): array
public function process(object $event)
{
return [];
// return [TestEvent::preFoo];
echo '打印参数'.PHP_EOL;
var_dump($event->getParams());
}
}
}
13 changes: 3 additions & 10 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,10 @@ function containerNew($name)
}
}

function event(string $eventName = '', array $eventArgs = []): bool
function event($event,string $eventName = ''): bool
{
$eventManager = EventInstance::instance();

// EventServiceProvider 中配置
// $testEvent = new TestEvent($eventManager);
// $eventManager->addEventSubscriber(new TestEventListener());

$eventArgsObj = EventArgs::getEmptyInstance();
$eventArgsObj->params = $eventArgs;
$eventManager->dispatchEvent($eventName, $eventArgsObj);
$dispatcher = EventInstance::instance();
$dispatcher->dispatch($event, $eventName);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "dcrswoole/framework",
"name": "dcr_swoole/framework",
"description": " PHP coroutine framework.",
"type": "project",
"license": "MIT",
Expand Down Expand Up @@ -29,7 +29,6 @@
"ext-simplexml": "*",
"ext-swoole": ">=4.8",
"doctrine/annotations": "^2.0",
"doctrine/event-manager": "^1.1",
"doctrine/migrations": "^3.5",
"elasticsearch/elasticsearch": "7.16",
"firebase/php-jwt": "^6.3",
Expand Down
10 changes: 4 additions & 6 deletions dcr/Event/EventInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DcrSwoole\Event;

use Doctrine\Common\EventManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Exception;

/**
Expand All @@ -13,18 +13,16 @@
*/
class EventInstance
{
/**
* @var EventManager
*/

public static $eventManager;

/**
* @throws Exception
*/
public static function instance(): EventManager
public static function instance(): EventDispatcher
{
if (!self::$eventManager) {
$ins = new EventManager();
$ins = new EventDispatcher;
self::$eventManager = $ins;
return self::$eventManager;
}
Expand Down
11 changes: 6 additions & 5 deletions dcr/Framework/Boostrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ protected function loadDotEnv(): void
*/
protected function loadEvents(): void
{
$eventManager = EventInstance::instance();
$dispatcher = EventInstance::instance();
$configs = EventServiceProvider::getEventConfig();

foreach ($configs as $event => $listeners) {
new $event($eventManager);
$eventManager->addEventSubscriber(new $listeners());
foreach($configs as $eventClass =>$listenerClass)
{
$listener = new $listenerClass();
$dispatcher->addListener($eventClass::NAME, [$listener, 'process']);
}

}

protected function bootGuzzle(): void
Expand Down

0 comments on commit 6155fa6

Please sign in to comment.