-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
94 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.