Skip to content

Instantly share code, notes, and snippets.

@viktorprogger
Last active December 23, 2020 11:22
Show Gist options
  • Save viktorprogger/1102fbb7880d70fd0f99485bbce72713 to your computer and use it in GitHub Desktop.
Save viktorprogger/1102fbb7880d70fd0f99485bbce72713 to your computer and use it in GitHub Desktop.
Event testing
{
"name": "root/app",
"require": {
"php": "^7.4",
"yiisoft/yii-event": "3.0.x-dev",
"yiisoft/di": "3.0.x-dev",
"yiisoft/composer-config-plugin": "1.0.x-dev"
},
"minimum-stability": "dev",
"extra": {
"config-plugin": {
"console": [
"$common",
"$console"
],
"events-console": "events-console.php"
}
}
}
<?php
declare(strict_types=1);
return [Message::class => [[EventHandler::class, 'handle']]];
<?php
declare(strict_types=1);
use Psr\EventDispatcher\EventDispatcherInterface;
use Yiisoft\Composer\Config\Builder;
use Yiisoft\Di\Container;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Event\ListenerCollectionFactory;
require __DIR__ . '/vendor/autoload.php';
final class Message {
private ?string $text;
public function __construct(?string $text)
{
$this->text = $text;
}
public function getText(): ?string
{
return $this->text;
}
}
final class EventHandler {
public function handle(Message $message)
{
var_dump($message->getText());
}
}
$container = new Container(require Builder::path('console'));
$message = new Message('test message');
$container->get(EventDispatcherInterface::class)->dispatch($message);
@viktorprogger
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment