Beispiel #1
0
        }
        $this->hooks[$name][] = $callback;
    }
    public function getCallbacks($name)
    {
        return isset($this->hooks[$name]) ? $this->hooks[$name] : array();
    }
    public function fire($name)
    {
        foreach ($this->getCallbacks($name) as $callback) {
            // prevent fatal errors, do your own warning or
            // exception here as you need it.
            if (!is_callable($callback)) {
                continue;
            }
            call_user_func($callback);
        }
    }
}
/**
 * 
 */
$hooks = new Hooks();
$hooks->add('event', function () {
    echo 'morally disputed.';
});
$hooks->add('event', function () {
    echo 'explicitly called.';
});
$hooks->fire('event');
Beispiel #2
0
 /**
  * Tests Hooks::fire().
  * 
  * @test
  * @dataProvider provider_fire
  * @param   array   functions
  * @param   string  expected
  */
 public function test_fire($parameters, $expected)
 {
     foreach ($parameters as $parameter) {
         Hooks::register('event', function ($k, &$array) {
             for ($i = $k; $i < $k + 10; $i++) {
                 $array[] = $i;
             }
         }, array($parameter));
     }
     $array = array();
     Hooks::fire('event', array(&$array));
     $count = 0;
     foreach ($parameters as $parameter) {
         for ($i = $parameter; $i < $parameter + 10; $i++) {
             $count++;
             $this->assertContains($i, $array);
         }
     }
     $this->assertSame($count, sizeof($array));
 }