flight\core\Dispatcher::run PHP Method

run() public method

Dispatches an event.
public run ( string $name, array $params = [] ) : string
$name string Event name
$params array Callback parameters
return string Output of callback
    public function run($name, array $params = array())
    {
        $output = '';
        // Run pre-filters
        if (!empty($this->filters[$name]['before'])) {
            $this->filter($this->filters[$name]['before'], $params, $output);
        }
        // Run requested method
        $output = $this->execute($this->get($name), $params);
        // Run post-filters
        if (!empty($this->filters[$name]['after'])) {
            $this->filter($this->filters[$name]['after'], $params, $output);
        }
        return $output;
    }

Usage Example

Example #1
0
 function testBeforeAndAfter()
 {
     $this->dispatcher->set('hello', function ($name) {
         return "Hello, {$name}!";
     });
     $this->dispatcher->hook('hello', 'before', function (&$params, &$output) {
         // Manipulate the parameter
         $params[0] = 'Fred';
     });
     $this->dispatcher->hook('hello', 'after', function (&$params, &$output) {
         // Manipulate the output
         $output .= " Have a nice day!";
     });
     $result = $this->dispatcher->run('hello', array('Bob'));
     $this->assertEquals('Hello, Fred! Have a nice day!', $result);
 }