public function testKill() { $e = new Manager(); $e->attach('pre', function () { return Manager::KILL; }); $e->trigger('pre'); $results = $e->getResults('pre'); $this->assertEquals(1, count($results)); $this->assertEquals(Manager::KILL, $results[0]); $this->assertFalse($e->alive()); }
require_once '../../bootstrap.php'; use Pop\Event\Manager; class Foo { public $value; public function __construct($arg = null) { $this->value = $arg; } public static function factory($arg) { return new self($arg); } public function bar($arg) { $this->value = $arg; return $this; } } try { $manager = new Manager(); $manager->attach('pre', 'Foo::factory', 2); $manager->attach('pre', array(new Foo(), 'bar'), 2); $manager->attach('pre', function ($result) { echo 'Hello, ' . $result->value . '<br />' . PHP_EOL; }, 1); $manager->trigger('pre', array('arg' => 'World')); } catch (\Exception $e) { echo $e->getMessage(); }
<?php require_once '../../bootstrap.php'; use Pop\Event\Manager; try { $manager = new Manager(); $manager->attach('pre', function () { return 'Hello, World'; }, 2); $manager->attach('pre', function ($name) { return 'Hello, ' . $name; }, 2); $manager->attach('pre', function ($result) { echo $result . '<br />' . PHP_EOL; }, 1); $manager->trigger('pre', array('name' => 'World')); } catch (\Exception $e) { echo $e->getMessage(); }
/** * Attach an event. Default project event name hook-points are: * * route.pre * route * route.error * route.post * * dispatch.pre * dispatch * dispatch.send * dispatch.post * dispatch.error * * @param string $name * @param mixed $action * @param int $priority * @return \Pop\Project\Project */ public function attachEvent($name, $action, $priority = 0) { $this->events->attach($name, $action, $priority); return $this; }
use Pop\Event\Manager; class Foo { public function __construct($val = null) { if (null !== $val) { echo 'Hello, ' . $val . '.'; } } public function bar($val) { echo 'How are you, ' . $val . '?'; } public static function baz($val) { echo 'Goodbye, ' . $val . '.'; } } try { $manager = new Manager(); $manager->attach('pre', function () { return 'World'; }, 2); //$manager->attach('pre', array(new Foo, 'bar'), 2); $manager->attach('pre', 'Foo', 2); //$manager->attach('pre', 'Foo->bar', 2); //$manager->attach('pre', 'Foo::baz', 2); $manager->trigger('pre', array('val' => 'World')); } catch (\Exception $e) { echo $e->getMessage(); }