Refactor handleEvent() to pass in the Game always.
This commit is contained in:
+1
-13
@@ -62,9 +62,7 @@ class Bootstrap
|
||||
$pdo = $this->connectToDatabase($config);
|
||||
$entityManager = $this->createEntityManager($pdo);
|
||||
|
||||
$eventManager = $this->createEventManager($entityManager);
|
||||
|
||||
$this->game = new Game($config, $logger, $entityManager, $eventManager);
|
||||
$this->game = new Game($config, $logger, $entityManager);
|
||||
|
||||
return $this->game;
|
||||
}
|
||||
@@ -144,16 +142,6 @@ class Bootstrap
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns an instance of the EventManager
|
||||
* @param EntityManagerInterface $entityManager
|
||||
* @return \LotGD\Core\EventManager
|
||||
*/
|
||||
protected function createEventManager(EntityManagerInterface $entityManager): EventManager
|
||||
{
|
||||
return new EventManager($entityManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the EntityManager using the pdo connection given in it's argument
|
||||
* @param \PDO $pdo
|
||||
|
||||
@@ -7,11 +7,12 @@ interface EventHandler
|
||||
/**
|
||||
* Called when an event is published that is handled by this class.
|
||||
*
|
||||
* @param Game $g The game.
|
||||
* @param string $event Name of this event.
|
||||
* @param array $context Arbitrary dictionary representing context around this event.
|
||||
* @return array|null Return an array if you want to make changes to the $context before
|
||||
* the next handler is called. Otherwise, return null. Any changes made will be propogated
|
||||
* to the event publisher as well.
|
||||
*/
|
||||
public static function handleEvent(string $event, array &$context);
|
||||
public static function handleEvent(Game $g, string $event, array &$context);
|
||||
}
|
||||
|
||||
@@ -17,14 +17,14 @@ use LotGD\Core\Exceptions\WrongTypeException;
|
||||
*/
|
||||
class EventManager
|
||||
{
|
||||
private $em;
|
||||
private $g;
|
||||
|
||||
/**
|
||||
* @param EntityManagerInterface $em The database entity manager.
|
||||
* @param Game $g The game.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
public function __construct(Game $g)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->g = $g;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +46,7 @@ class EventManager
|
||||
foreach ($subscriptions as $s) {
|
||||
if (preg_match($s->getPattern(), $event)) {
|
||||
$class = $s->getClass();
|
||||
$c = $class::handleEvent($event, $context);
|
||||
$c = $class::handleEvent($this->g, $event, $context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,7 +94,7 @@ class EventManager
|
||||
'class' => $class,
|
||||
'library' => $library
|
||||
]);
|
||||
$e->save($this->em);
|
||||
$e->save($this->g->getEntityManager());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +108,7 @@ class EventManager
|
||||
*/
|
||||
public function unsubscribe(string $pattern, string $class, string $library)
|
||||
{
|
||||
$e = $this->em->getRepository(EventSubscription::class)->find(array(
|
||||
$e = $this->g->getEntityManager()->getRepository(EventSubscription::class)->find(array(
|
||||
'pattern' => $pattern,
|
||||
'class' => $class,
|
||||
'library' => $library
|
||||
@@ -116,7 +116,7 @@ class EventManager
|
||||
if (!$e) {
|
||||
throw new SubscriptionNotFoundException("Subscription not found with pattern={$pattern} class={$class} library={$library}.");
|
||||
}
|
||||
$e->delete($this->em);
|
||||
$e->delete($this->g->getEntityManager());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,6 +124,6 @@ class EventManager
|
||||
*/
|
||||
public function getSubscriptions(): array
|
||||
{
|
||||
return $this->em->getRepository(EventSubscription::class)->findAll();
|
||||
return $this->g->getEntityManager()->getRepository(EventSubscription::class)->findAll();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-6
@@ -38,13 +38,11 @@ class Game
|
||||
public function __construct(
|
||||
Configuration $configuration,
|
||||
Logger $logger,
|
||||
EntityManagerInterface $entityManager,
|
||||
EventManager $eventManager
|
||||
EntityManagerInterface $entityManager
|
||||
) {
|
||||
$this->configuration = $configuration;
|
||||
$this->logger = $logger;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->eventManager = $eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,6 +102,9 @@ class Game
|
||||
*/
|
||||
public function getEventManager(): EventManager
|
||||
{
|
||||
if ($this->eventManager === null) {
|
||||
$this->eventManager = new EventManager($this);
|
||||
}
|
||||
return $this->eventManager;
|
||||
}
|
||||
|
||||
@@ -176,7 +177,6 @@ class Game
|
||||
// No viewpoint set up for this user. Run the hook to find the default
|
||||
// scene.
|
||||
$context = [
|
||||
'g' => $this,
|
||||
'character' => $this->getCharacter(),
|
||||
'scene' => null
|
||||
];
|
||||
@@ -228,7 +228,6 @@ class Game
|
||||
// $nextViewpoint, including the ability to redirect the user to
|
||||
// a different scene, by setting $context['redirect'] to a new action.
|
||||
$context = [
|
||||
'g' => $this,
|
||||
'viewpoint' => $v,
|
||||
'redirect' => null
|
||||
];
|
||||
@@ -262,7 +261,6 @@ class Game
|
||||
$v->setActions([$ag]);
|
||||
|
||||
$context = [
|
||||
'g' => $this,
|
||||
'viewpoint' => $v,
|
||||
];
|
||||
$this->getEventManager()->publish('h/lotgd/core/viewpoint-for/' . $s->getTemplate(), $context);
|
||||
|
||||
+30
-19
@@ -3,12 +3,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace LotGD\Core\Tests;
|
||||
|
||||
use LotGD\Core\Game;
|
||||
use LotGD\Core\EventManager;
|
||||
use LotGD\Core\Models\EventSubscription;
|
||||
use LotGD\Core\EventHandler;
|
||||
use LotGD\Core\Exceptions\WrongTypeException;
|
||||
use LotGD\Core\Exceptions\ClassNotFoundException;
|
||||
use LotGD\Core\Exceptions\SubscriptionNotFoundException;
|
||||
use LotGD\Core\Models\EventSubscription;
|
||||
use LotGD\Core\Tests\ModelTestCase;
|
||||
use LotGD\Core\Tests\FakeModule\Module as FakeModule;
|
||||
|
||||
@@ -19,7 +20,7 @@ class EventManagerTestInvalidSubscriber
|
||||
|
||||
class EventManagerTestSubscriber implements EventHandler
|
||||
{
|
||||
public static function handleEvent(string $event, array &$context) {}
|
||||
public static function handleEvent(Game $g, string $event, array &$context) {}
|
||||
}
|
||||
|
||||
class EventManagerTest extends ModelTestCase
|
||||
@@ -27,9 +28,19 @@ class EventManagerTest extends ModelTestCase
|
||||
/** @var string default data set */
|
||||
protected $dataset = "eventManager";
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->g = $this->getMockBuilder(Game::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->g->method('getEntityManager')->willReturn($this->getEntityManager());
|
||||
}
|
||||
|
||||
public function testSubscribeNoClass()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(ClassNotFoundException::class);
|
||||
$em->subscribe("/test.event/", 'LotGD\Core\Tests\NoClassHere', 'lotgd/tests');
|
||||
@@ -37,7 +48,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testSubscribeInvalidClass()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(WrongTypeException::class);
|
||||
$em->subscribe("/test.event/", 'LotGD\Core\Tests\EventManagerTestInvalidSubscriber', 'lotgd/tests');
|
||||
@@ -45,7 +56,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testSubscribeInvalidRegexp()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(WrongTypeException::class);
|
||||
$em->subscribe("/test.event", FakeModule::class, 'lotgd/tests');
|
||||
@@ -53,28 +64,28 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testGetSubscriptions()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$pattern = "/test\\.foo.*/";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
$pattern = "/test\\.foo.*/";
|
||||
$class = FakeModule::class;
|
||||
$library = 'lotgd/tests';
|
||||
|
||||
$sub = EventSubscription::create([
|
||||
$sub = EventSubscription::create([
|
||||
'pattern' => $pattern,
|
||||
'class' => $class,
|
||||
'library' => $library
|
||||
]);
|
||||
]);
|
||||
|
||||
$subscriptions = $em->getSubscriptions();
|
||||
$this->assertContainsOnlyInstancesOf(EventSubscription::class, $subscriptions);
|
||||
$subscriptions = $em->getSubscriptions();
|
||||
$this->assertContainsOnlyInstancesOf(EventSubscription::class, $subscriptions);
|
||||
|
||||
// This is a little fragile, but assertContains() doesn't seem to work.
|
||||
$this->assertEquals($sub, $subscriptions[0]);
|
||||
// This is a little fragile, but assertContains() doesn't seem to work.
|
||||
$this->assertEquals($sub, $subscriptions[0]);
|
||||
}
|
||||
|
||||
public function testSubscribeSuccess()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$pattern = "/test.event/";
|
||||
$class = FakeModule::class;
|
||||
@@ -97,7 +108,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testUnsubscribeSuccess()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$em->unsubscribe("/test\\.foo.*/", FakeModule::class, 'lotgd/tests');
|
||||
|
||||
@@ -107,7 +118,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testUnsubscribeNotFound()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$this->expectException(SubscriptionNotFoundException::class);
|
||||
$em->unsubscribe("/notfound/", FakeModule::class, 'lotgd/tests');
|
||||
@@ -115,7 +126,7 @@ class EventManagerTest extends ModelTestCase
|
||||
|
||||
public function testPublish()
|
||||
{
|
||||
$em = new EventManager($this->getEntityManager());
|
||||
$em = new EventManager($this->g);
|
||||
|
||||
$event = 'test.foo.something_here';
|
||||
$context = array('foo' => 'bar');
|
||||
|
||||
@@ -6,7 +6,7 @@ use LotGD\Core\Game;
|
||||
use LotGD\Core\Module as ModuleBase;
|
||||
|
||||
class Module implements ModuleBase {
|
||||
public static function handleEvent(string $event, array &$context) {
|
||||
public static function handleEvent(Game $g, string $event, array &$context) {
|
||||
$context['foo'] = 'baz';
|
||||
return $context;
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,14 +29,14 @@ class DefaultSceneProvider implements EventHandler
|
||||
public static $attachments = ['actions'];
|
||||
public static $data = ['data'];
|
||||
|
||||
public static function handleEvent(string $event, array &$context)
|
||||
public static function handleEvent(Game $g, string $event, array &$context)
|
||||
{
|
||||
switch ($event) {
|
||||
case 'h/lotgd/core/default-scene':
|
||||
if (!isset($context['character'])) {
|
||||
throw new \Exception("Key 'character' was expected on event h/lotgd/core/default-scene.");
|
||||
}
|
||||
$context['scene'] = $context['g']->getEntityManager()->getRepository(Scene::class)->find(1);
|
||||
$context['scene'] = $g->getEntityManager()->getRepository(Scene::class)->find(1);
|
||||
break;
|
||||
case 'h/lotgd/core/viewpoint-for/lotgd/tests/village':
|
||||
$v = $context['viewpoint'];
|
||||
@@ -62,7 +62,7 @@ class GameTest extends ModelTestCase
|
||||
$logger = new Logger('test');
|
||||
$logger->pushHandler(new NullHandler());
|
||||
|
||||
$this->g = new Game(new Configuration(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . getenv('LOTGD_CONFIG')), $logger, $this->getEntityManager(), new EventManager($this->getEntityManager()));
|
||||
$this->g = new Game(new Configuration(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . getenv('LOTGD_CONFIG')), $logger, $this->getEntityManager());
|
||||
}
|
||||
|
||||
public function testGetCharacterException()
|
||||
|
||||
Reference in New Issue
Block a user