Ejemplo n.º 1
0
            if (is_callable(self::$events[$name][0])) {
                return call_user_func_array(self::$events[$name][0], $params);
            } else {
                return false;
            }
        }
        // Loop through all the events and call them
        foreach (self::$events[$name] as $event) {
            if (is_callable($event)) {
                call_user_func_array($event, $params);
            }
        }
    }
    public static function unsubscribe($name)
    {
        if (!empty(self::$events[$name])) {
            unset(self::$events[$name]);
        }
    }
}
// Basic usage
PubSub::subscribe('beforeSave', function ($helo) {
    echo $helo;
    echo 'PubSub::beforeSave';
});
PubSub::subscribe('afterSave', function () {
    echo 'PubSub::afterSave';
});
PubSub::publish('beforeSave', "Helo");
PubSub::unsubscribe('beforeSave');
Ejemplo n.º 2
0
 /**
  * @covers PubSub::unsubscribe
  */
 public function testRemove()
 {
     $callbacks = array(function () {
         PubSubTest::$callbackCalled = true;
     }, array('PubSubTest', 'staticCallback'), array($this, 'instanceCallback'), 'global_pub_sub_test_callback');
     foreach ($callbacks as $key => $callback) {
         PubSub::subscribe('my_event', $callback);
         $this->assertEquals(1, count(PubSub::subscriptions('my_event')), '1 callback was added, so 1 callback should be in array.');
         PubSub::unsubscribe('my_event', $callback);
         $this->assertEquals(0, count(PubSub::subscriptions('my_event')), '1 callback was unsubscribed, so 0 callbacks should be in array.');
     }
 }