Exemplo n.º 1
0
 public function __construct(LoopInterface $loop, EventEmitter $emit, \SplObjectStorage $clients, RoundProcessor $roundProcessor, EngineHelper $helper, $time = 60, $debug = false)
 {
     $this->debug = $debug;
     $this->helper = $helper;
     $this->emit = $emit;
     $this->loop = $loop;
     $this->roundTimer = new RoundTimer($loop, $emit);
     $this->roundNumber = 0;
     $this->clients = $clients;
     $this->disconnected = new \SplObjectStorage();
     $this->time = $time;
     $that = $this;
     $this->say = new Say($this->clients);
     $this->roundProcessor = $roundProcessor;
     $players = new Say($this->clients);
     // Setup listeners on the roundTimer object.
     $emit->on('GAME.COUNTDOWN', function () use($that) {
         if ($this->playerCount !== $this->clients->count()) {
             $this->playerCount = $this->clients->count();
             $this->say->to($this->clients)->that(Say::TICK, ["players" => $this->playerCount]);
         }
     });
     // Setup listeners on the roundTimer object.
     $emit->on('GAME.COUNTDOWN_END', function () use($that, $loop, $emit) {
         $this->say->to($this->clients)->that(Say::GAME_STARTING, ["players" => $this->clients->count()]);
         $this->setGameStarted(true);
         $loop->addTimer(3, function () use($loop, $emit) {
             $this->startRound($loop, $emit);
         });
     });
     $this->roundTimer->startCountDown($this->time);
 }
Exemplo n.º 2
0
 public function testRegisterAndForwardThenUnregister()
 {
     $listener = new TestListener();
     $target = new EventEmitter();
     $n = 0;
     $target->on('received', function ($type, $data) use(&$n, &$capturedType, &$capturedData) {
         $n++;
         $capturedData = $data;
         $capturedType = $type;
     });
     $m = 0;
     $listener->on('received', function ($type, $data) use(&$m, &$capturedType2, &$capturedData2) {
         $m++;
         $capturedData2 = $data;
         $capturedType2 = $type;
     });
     $listeners = new Listeners();
     $listeners->register($listener, $target);
     $type = 'type';
     $data = 'data';
     $listener->handle($type, $data);
     $listener->handle($type, $data);
     $listeners->unregister($listener, $target);
     $listener->handle($type, $data);
     $this->assertEquals(2, $n);
     $this->assertEquals(3, $m);
     $this->assertEquals($type, $capturedType);
     $this->assertEquals($data, $capturedData);
     $this->assertEquals($type, $capturedType2);
     $this->assertEquals($data, $capturedData2);
 }
 public function testWithOverrides()
 {
     $called = false;
     $callback = function ($case, $class, $method) use(&$called) {
         $called = true;
         $this->assertInstanceOf('Phantestic\\Test\\Test', $case);
         $this->assertSame(__NAMESPACE__ . '\\PassingTest', $class);
         $this->assertSame('testPassingTestMethod', $method);
     };
     $filter = function ($file, $class, $method) {
         return $file === __DIR__ . '/PassingTest.php' && $class === __NAMESPACE__ . '\\PassingTest' && $method === 'testPassingTestMethod';
     };
     $generator = function ($class, $method) {
         $this->assertSame(__NAMESPACE__ . '\\PassingTest', $class);
         $this->assertSame('testPassingTestMethod', $method);
         $callback = function () {
             // noop
         };
         return new \Phantestic\Test\Test($callback, 'foo');
     };
     $emitter = new EventEmitter();
     $emitter->on('phantestic.loader.loaded', $callback);
     $loader = new ClassmapObjectLoaderSubclass($emitter, $filter, $generator);
     $this->testLoader($loader);
     $this->assertTrue($called, 'Event callback was not called');
 }
 public function testDoNoRemoveOtherListeners()
 {
     $emitter = new EventEmitter();
     $listener = function () {
     };
     $emitter->on('end', $listener);
     EventPromise::listen($emitter, ['end'])->done();
     $emitter->emit('end', ['payload']);
     $this->assertSame([$listener], $emitter->listeners('end'));
 }
Exemplo n.º 5
0
 /**
  * @param EventEmitter $server
  * @param LoggerInterface $logger
  */
 public function __construct(EventEmitter $server, LoggerInterface $logger)
 {
     $this->server = $server;
     $this->logger = $logger;
     $this->handlers = new \SplObjectStorage();
     $this->membership = new \SplObjectStorage();
     /**
      * @var $membership \SplObjectStorage|WebSocketUriHandlerInterface[]
      */
     $membership = $this->membership;
     $that = $this;
     $server->on("connect", function (WebSocketTransportInterface $client) use($that, $logger, $membership) {
         $handler = $that->matchConnection($client);
         if ($handler) {
             $logger->notice("Added client {$client->getId()} to " . get_class($handler));
             $membership->attach($client, $handler);
             $handler->emit("connect", ["client" => $client]);
             $handler->addConnection($client);
         } else {
             $logger->err(sprintf("Cannot route %s with request uri %s", $client->getId(), $client->getHandshakeRequest()->getUriString()));
         }
     });
     $server->on('disconnect', function (WebSocketTransportInterface $client) use($that, $logger, $membership) {
         if ($membership->contains($client)) {
             $handler = $membership[$client];
             $membership->detach($client);
             $logger->notice("Removed client {$client->getId()} from" . get_class($handler));
             $handler->removeConnection($client);
             $handler->emit("disconnect", ["client" => $client]);
         } else {
             $logger->warn("Client {$client->getId()} not attached to any handler, so cannot remove it!");
         }
     });
     $server->on("message", function (WebSocketTransportInterface $client, WebSocketMessageInterface $message) use($that, $logger, $membership) {
         if ($membership->contains($client)) {
             $handler = $membership[$client];
             $handler->emit("message", compact('client', 'message'));
         } else {
             $logger->warn(sprintf("Client %s not attached to any handler, so cannot forward the message!", $client->getId()));
         }
     });
 }
Exemplo n.º 6
0
 public function testMuteListener()
 {
     $listenersCalled = 0;
     $listener = function () use(&$listenersCalled) {
         $listenersCalled++;
     };
     $this->emitter->on('foo', $listener);
     $this->emitter->on('foo', function () use(&$listenersCalled) {
         $listenersCalled++;
         return;
     });
     $this->emitter->mute('foo', $listener);
     $this->emitter->emit('foo');
     $this->assertEquals(1, $listenersCalled);
     $this->emitter->unMute('foo', $listener);
     $this->emitter->emit('foo');
     $this->assertEquals(3, $listenersCalled);
 }
Exemplo n.º 7
0
 /**
  * Adds an event listener to log data emitted by a stream.
  *
  * @param \Evenement\EventEmitter $emitter
  * @param \Phergie\Irc\ConnectionInterface $connection Connection
  *        corresponding to the stream
  */
 protected function addLogging(EventEmitter $emitter, ConnectionInterface $connection)
 {
     $emitter->on('data', $this->getOutputLogCallback($connection, 'debug'));
     $emitter->on('error', $this->getOutputLogCallback($connection, 'notice'));
 }
Exemplo n.º 8
0
 /**
  * Registers a new signal handler
  *
  * @param int      $signo    The signal number
  * @param callable $listener The listener
  */
 public function on($signo, callable $listener)
 {
     pcntl_signal($signo, array($this, 'emit'));
     parent::on($signo, $listener);
 }
Exemplo n.º 9
0
 /**
  * @param EventEmitter $dispatcher
  */
 private function injectVisibilityFilter(EventEmitter $dispatcher)
 {
     $listener = function ($base, $challenger, &$skip) {
         $allowed = $this->input->getOption('visibility');
         $skip = $skip || (!$base || !in_array($base->getVisibility(), $allowed)) && (!$challenger || !in_array($challenger->getVisibility(), $allowed));
     };
     $dispatcher->on(MemberCollectionComparator::EVENT_PRE_COMPARE, $listener);
     $dispatcher->on(MethodCollectionComparator::EVENT_PRE_COMPARE, $listener);
     $dispatcher->on(ConstantCollectionComparator::EVENT_PRE_COMPARE, function ($base, $challenger, &$skip) {
         $allowed = $this->input->getOption('visibility');
         $skip = $skip || !in_array('public', $allowed);
     });
 }
Exemplo n.º 10
0
 /**
  * @see AbstractDispatcherAdapter::on
  */
 public function on($name, callable $listener)
 {
     $this->dispatcher->on($name, $listener);
     return $this;
 }
Exemplo n.º 11
0
             $result = new TestResult(new EventEmitter());
             $test->run($result);
             $expected = "1 run, 1 failed";
             $actual = $result->getSummary();
             assert($expected == $actual, "expected {$expected}, got {$actual}");
         });
         it('should not result in a pass and fail if tear down fails', function () {
             $test = new Test("passing", function () {
             });
             $test->addTearDownFunction(function () {
                 throw new Exception("failure");
             });
             $emitter = new EventEmitter();
             $count = 0;
             $emitter->on('test.passed', function () use(&$count) {
                 $count++;
             });
             $emitter->on('test.failed', function () use(&$count) {
                 $count++;
             });
             $test->run(new TestResult($emitter));
             assert($count == 1, "should not have emitted a pass and fail event");
         });
     });
 });
 describe("->getTitle()", function () {
     it("should return the full text for a spec including parents", function () {
         $root = new Suite("parent", function () {
         });
         $child = new Suite("nested", function () {
         });
 /**
  * Adds an event listener to log data emitted by a stream.
  *
  * @param \Evenement\EventEmitter $emitter
  * @param \Phergie\Irc\ConnectionInterface $connection Connection
  *        corresponding to the stream
  */
 protected function addLogging(EventEmitter $emitter, ConnectionInterface $connection)
 {
     $logger = $this->getLogger();
     $callback = function ($msg) use($logger, $connection) {
         $mask = sprintf('%s!%s@%s', $connection->getNickname(), $connection->getUsername(), $connection->getServerHostname());
         $logger->debug($mask . ' ' . trim($msg));
     };
     $emitter->on('data', $callback);
     $emitter->on('error', $callback);
 }
Exemplo n.º 13
0
 /**
  * @param string   $event
  * @param \Closure $callback
  */
 public function on($event, \Closure $callback)
 {
     $this->_eventEmitter->on($event, $callback);
 }