Example #1
1
 public function __construct(Torrent $torrent, $dir)
 {
     parent::__construct();
     $dir = realpath($dir);
     $dir = rtrim($dir, '/');
     if (!is_dir($dir)) {
         throw new Exception("Cannot find download directory '{$dir}'");
     }
     $this->torrent = $torrent;
     $this->dir = $dir;
     $this->myPeerID = sha1(time() . rand(), true);
     $this->pieceCache = new PieceCache($torrent, $dir);
     $this->lastFlush = time();
     $this->peers = array();
     $this->streams = array();
     $this->streamLookup = array();
     $this->trackers = array();
     $this->udp = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
     if (empty($this->udp)) {
         throw new Exception("Cannot create UDP socket");
     }
     foreach ($torrent->getTrackers() as $url) {
         $this->addTracker($url);
     }
 }
 public function testRun_RenderableAndResponse()
 {
     $this->emitter->attach('controller.load', array($this, 'handleControllerLoad'));
     $this->emitter->attach('controller.invoke', array($this, 'handleControllerInvoke_Presenter'));
     $this->emitter->attach('controller.view_context', array($this, 'handleControllerViewContext'));
     $this->emitter->attach('controller.view', array($this, 'handleControllerView'));
     $response = $this->object->run($this->createRoute());
     $this->assertThat($response, $this->isInstanceOf('IResponse'));
 }
 public function testFilters()
 {
     $this->emitter->filter('the_content', function ($content, $append) {
         return $content . ' ' . $append;
     });
     $this->emitter->filter('the_content', function ($content) {
         return $content . ' yolo';
     });
     $content = $this->emitter->applyFilters('the_content', 'ham', 'sandwich');
     $this->assertEquals('ham sandwich yolo', $content);
 }
Example #4
0
 public static function getBase()
 {
     if (!self::$globalBase) {
         self::$globalBase = event_base_new();
     }
     return self::$globalBase;
 }
 public function emit($event, array $arguments = array())
 {
     foreach ($this->anyListeners as $listener) {
         call_user_func_array($listener, $arguments);
     }
     parent::emit($event, $arguments);
 }
 protected function _removeListener($event, callable $listener)
 {
     foreach ($this->listeners as $ev => $_listeners) {
         if (preg_match('%' . $ev . '%i', $event)) {
             parent::removeListener($ev, $listener);
         }
     }
 }
 public function testOffRemovesAllListeners()
 {
     $function = function () {
     };
     $this->emitter->on('foo', $function);
     $this->emitter->off('foo', $function);
     $this->assertFalse($this->emitter->hasEventListener('foo', $function));
 }
Example #8
0
 function testRemoveAllListeners()
 {
     $self = $this;
     $ok = false;
     $bad = false;
     $eventEmitter = new EventEmitter();
     $listener1 = function () use(&$bad) {
         $bad = true;
     };
     $listener2 = function () use(&$bad) {
         $bad = true;
     };
     $eventEmitter->on('test', $listener1);
     $eventEmitter->on('test', $listener2);
     $eventEmitter->removeAllListeners('test');
     $eventEmitter->emit('test');
     $this->assertFalse($bad);
 }
 public function testNotifyUntil()
 {
     $name = 'test.listener';
     $mocks = array();
     $listeners = array();
     for ($i = 0; $i < 4; $i++) {
         $mocks[$i] = $this->getMock('IMockListener');
         $listeners[$i] = array($mocks[$i], 'testCallback');
         $this->dispatcher->attach($name, $listeners[$i]);
     }
     $this->assertThat($listeners, $this->equalTo($this->dispatcher->getListeners($name)), 'Both listener arrays must are equal');
     $e = new Event(null, $name);
     $mocks[0]->expects($this->once())->method('testCallback');
     $mocks[1]->expects($this->once())->method('testCallback')->will($this->returnValue(true));
     $mocks[2]->expects($this->never())->method('testCallback');
     $mocks[3]->expects($this->never())->method('testCallback');
     // notifies only first 2 mocks, other 2 mocks are not called
     $this->dispatcher->notifyUntil($e, true);
 }
Example #10
0
 function testContinueCallBackBreakByHandler()
 {
     $ee = new EventEmitter();
     $handlerCounter = 0;
     $bla = function () use(&$handlerCounter) {
         $handlerCounter++;
         return false;
     };
     $ee->on('foo', $bla);
     $ee->on('foo', $bla);
     $ee->on('foo', $bla);
     $continueCounter = 0;
     $r = $ee->emit('foo', [], function () use(&$continueCounter) {
         $continueCounter++;
         return false;
     });
     $this->assertFalse($r);
     $this->assertEquals(1, $handlerCounter);
     $this->assertEquals(0, $continueCounter);
 }
 public function testPriorityListeners()
 {
     $sum = 0;
     $emitter = new EventEmitter();
     $emitter->on('add', function () use(&$sum) {
         $sum <<= 1;
     }, -999);
     // low priority
     $emitter->on('add', function ($a, $b) use(&$sum) {
         $sum = $a + $b;
     });
     $this->assertTrue($emitter->emit('add', [100, 200]));
     $this->assertSame(600, $sum);
 }
Example #12
0
 public function testOffMulti()
 {
     $closure = function () {
     };
     $closure1 = function () {
     };
     $this->event->on(array('a', 'b'), $closure);
     $this->event->addListener(array('a', 'b'), $closure1);
     $this->assertEquals(array($closure, $closure1), $this->event->listeners('a'));
     $this->assertEquals(array($closure, $closure1), $this->event->listeners('b'));
     $this->event->off(array('a', 'b'), $closure);
     $this->assertEquals(array(1 => $closure1), $this->event->listeners('a'));
     $this->assertEquals(array(1 => $closure1), $this->event->listeners('b'));
 }
Example #13
0
File: View.php Project: pagon/core
 /**
  * Construct a view
  *
  * @param string $path
  * @param array  $data
  * @param array  $injectors
  * @throws \Exception
  * @return View
  */
 public function __construct($path, $data = array(), $injectors = array())
 {
     // Set dir for the view
     $injectors = array('data' => (array) $data, 'path' => $path) + $injectors + $this->injectors;
     // Set path
     $injectors['path'] = ltrim($path, '/');
     // If file exists?
     if (!is_file($injectors['dir'] . '/' . $injectors['path'])) {
         // Try to load file from absolute path
         if ($path[0] == '/' && is_file($path)) {
             $injectors['path'] = $path;
             $injectors['dir'] = '';
         } else {
             if ($injectors['app'] && ($_path = $injectors['app']->path($injectors['path']))) {
                 $injectors['path'] = $_path;
                 $injectors['dir'] = '';
             } else {
                 throw new \Exception('Template file is not exist: ' . $injectors['path']);
             }
         }
     }
     // Set data
     parent::__construct($injectors);
 }
 function testUnregisterAllListenersForMultipleEvents()
 {
     $a = 0;
     $b = 0;
     $ee = new EventEmitter();
     $ee->on(['foo', 'bar', 'qux'], function () use(&$a) {
         $a++;
     });
     $ee->on(['bar', 'qux'], function () use(&$b) {
         $b++;
     });
     $ee->removeAllListeners(['foo', 'bar']);
     $ee->emit('foo');
     $ee->emit('bar');
     $ee->emit('qux');
     $this->assertEquals(1, $a);
     $this->assertEquals(1, $b);
 }
Example #15
0
<?php

require '../vendor/autoload.php';
$listener = new EventEmitter();
/*
$listener->on( 'hello', function( $data, $callback ){
    print "Callback\n";
  });
$listener->on( 'hello', function( $data, $callback ){
    print "Callback 2\n";
  });
$listener->on( 'hello', function( $data, $callback ){
    print "Callback !!\n";
  });
*/
$remove_fn = function ($data, $callback) {
    print "Callback 3\n";
    $callback();
};
$listener->on('hello', $remove_fn);
$listener->emit('hello', 'second_arg', function () {
    print "emit_callback\n";
});
$listener->on('goodbye', function () {
    print "Goodbye world\n";
});
$listener->removeListener('hello', $remove_fn);
$listener->emit('hello', 'second_arg', function () {
    print "emit_callback second time trhough\n";
});
$listener->emit('goodbye');
Example #16
0
 /**
  * @depends testCancelEvent
  */
 function testPriorityOnce()
 {
     $argResult = 0;
     $ee = new EventEmitter();
     $ee->once('foo', function ($arg) use(&$argResult) {
         $argResult = 1;
         return false;
     });
     $ee->once('foo', function ($arg) use(&$argResult) {
         $argResult = 2;
         return false;
     }, 1);
     $this->assertFalse($ee->emit('foo', ['bar']));
     $this->assertEquals(2, $argResult);
 }
Example #17
0
 public function testLoadPluginIfRegisteredAfterPluginBootEvent()
 {
     $emitter = new EventEmitter();
     $manager = new PluginManager($this->mockInjector, $emitter);
     $emitter->emit(Engine::ENGINE_BOOTUP_EVENT);
     $plugin = new BootCalledPlugin();
     $manager->registerPlugin($plugin);
     $this->assertTrue($plugin->wasCalled());
 }
Example #18
0
 /**
  * Construct a view
  *
  * @param string $path
  * @param array  $data
  * @param array  $injectors
  * @throws \Exception
  * @return View
  */
 public function __construct($path, $data = array(), $injectors = array())
 {
     if (is_array($path)) {
         // Support View factory
         $this->injectors['data'] = $path;
         $this->compileDirectly = true;
     } else {
         // Set dir for the view
         $injectors = array('data' => (array) $data, 'path' => $path) + $injectors + array('dir' => (string) App::self()->get('views'), 'app' => App::self()) + $this->injectors;
         // Set path
         $injectors['path'] = ltrim($path, '/');
         // If file exists?
         if (!is_file($injectors['dir'] . '/' . $injectors['path'])) {
             // Try to load file from absolute path
             if ($path[0] == '/' && is_file($path)) {
                 $injectors['path'] = $path;
                 $injectors['dir'] = '';
             } else {
                 if (!empty($injectors['app']) && ($_path = $injectors['app']->path($injectors['path']))) {
                     $injectors['path'] = $_path;
                     $injectors['dir'] = '';
                 } else {
                     throw new \Exception('Template file is not exist: ' . $injectors['path']);
                 }
             }
         }
     }
     // Set data
     parent::__construct($injectors);
 }
Example #19
0
 /**
  * Constructor
  * 
  * @param   IOLoop  $ioloop
  */
 public function __construct(IOLoop $ioloop)
 {
     parent::__construct();
     $this->iloop = $ioloop;
 }