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(); }
/** * Run the project. * * @return void */ public function run() { // If router exists, then route the project to the appropriate controller if (null !== $this->router) { $this->start = time(); if (isset($_SERVER['REQUEST_METHOD'])) { $session = '[' . $_SERVER['REQUEST_METHOD'] . ']'; if (isset($_SERVER['REMOTE_ADDR'])) { $session .= ' ' . $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['SERVER_PORT'])) { $session .= ':' . $_SERVER['SERVER_PORT']; } if (isset($_SERVER['HTTP_USER_AGENT'])) { $session .= ' ' . $_SERVER['HTTP_USER_AGENT']; } } } else { $session = '[CLI]'; } $this->log($session, time()); if (null !== $this->events->get('route.pre')) { $this->log('[Event] Pre-Route', time(), \Pop\Log\Logger::NOTICE); } // Trigger any pre-route events, route, then trigger any post-route events $this->events->trigger('route.pre', array('router' => $this->router)); // If still alive after 'route.pre' if ($this->events->alive()) { $this->log('Route Start', time()); $this->router->route($this); // If still alive after 'route' if ($this->events->alive()) { if (null !== $this->events->get('route.post')) { $this->log('[Event] Post-Route', time(), \Pop\Log\Logger::NOTICE); } $this->events->trigger('route.post', array('router' => $this->router)); // If still alive after 'route.post' and if a controller was properly // routed and created, then dispatch it if ($this->events->alive() && null !== $this->router->controller()) { // Trigger any pre-dispatch events if (null !== $this->events->get('dispatch.pre')) { $this->log('[Event] Pre-Dispatch', time(), \Pop\Log\Logger::NOTICE); } $this->events->trigger('dispatch.pre', array('router' => $this->router)); // If still alive after 'dispatch.pre' if ($this->events->alive()) { // Get the action and dispatch it $action = $this->router->getAction(); // Dispatch the found action, the error action or trigger the dispatch error events if (null !== $action && method_exists($this->router->controller(), $action)) { $this->router->controller()->dispatch($action); } else { if (method_exists($this->router->controller(), $this->router->controller()->getErrorAction())) { $this->router->controller()->dispatch($this->router->controller()->getErrorAction()); } else { if (null !== $this->events->get('dispatch.error')) { $this->log('[Event] Dispatch Error', time(), \Pop\Log\Logger::ERR); } $this->events->trigger('dispatch.error', array('router' => $this->router)); } } // If still alive after 'dispatch' if ($this->events->alive()) { // Trigger any post-dispatch events if (null !== $this->events->get('dispatch.post')) { $this->log('[Event] Post-Dispatch', time(), \Pop\Log\Logger::NOTICE); } $this->events->trigger('dispatch.post', array('router' => $this->router)); } } } } } $this->log('Route End', time()); } }
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(); }