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 e12698f commit 95221d2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 122 deletions.
13 changes: 6 additions & 7 deletions app/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use app\Event\TestEvent;
use app\Facade\TestFacade;
use app\Listener\TestEventListener;
use app\Middleware\TestMiddleware;
use app\Model\UserModel;
use app\Service\Entity\ExchGiftInfo;
Expand All @@ -18,6 +19,7 @@
use app\Utils\LogBase;
use Carbon\Carbon;
use dcr\Annotation\Mapping\RequestMapping;
use dcr\EventInstance;
use DI\Attribute\Inject;
use Exception;
use Gregwar\Captcha\CaptchaBuilder;
Expand All @@ -26,6 +28,7 @@
use Inhere\Validate\Validation;
use Intervention\Image\Facades\Image;
use Intervention\Image\ImageManager;
use Symfony\Component\EventDispatcher\EventDispatcher;

class TestController extends Controller
{
Expand Down Expand Up @@ -130,21 +133,17 @@ public function test4(): string

/**
* 测试事件
* @see https://github.com/inhere/php-event-manager
* @see https://www.doctrine-project.org/projects/doctrine-event-manager/en/latest/reference/index.html#setup
*/
#[RequestMapping(methods: 'GET , POST', path:'/test/event')]
public function event(): string
{
$test = request()->get('aa', 23);
$test = $this->request->get('ab', 24);
echo $test;
$params = [
'test' => 23,
];
event(new TestEvent($params),TestEvent::NAME);
// 初始化事件分发器

// return reponse()->setContent('abc')->send();

event(TestEvent::preFoo, $params);
return apiResponse([]);
}

Expand Down
44 changes: 10 additions & 34 deletions app/Event/TestEvent.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
<?php
declare(strict_types = 1);
/**
* The file is part of xxx/xxx
*
*
*/

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;
public const NAME = 'order.placed';

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

/** @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);
}
31 changes: 8 additions & 23 deletions app/Listener/TestEventListener.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
<?php
declare(strict_types = 1);
/**
* The file is part of xxx/xxx
*
*
*/

namespace app\Listener;

use Doctrine\Common\EventSubscriber;
use app\Event\TestEvent;
use app\Utils\LogBase;
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());
}
}
}
4 changes: 2 additions & 2 deletions app/Provider/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

/**
* 事件
* @see https://www.doctrine-project.org/projects/doctrine-event-manager/en/latest/reference/index.html#setup
* @see https://code.tutsplus.com/tutorials/handling-events-in-your-php-applications-using-the-symfony-eventdispatcher-component--cms-31328
*/
class EventServiceProvider
{
/**
* @return string[]
* @return array
*/
public static function getEventConfig()
{
Expand Down
Loading

0 comments on commit 95221d2

Please sign in to comment.