本文整理汇总了PHP中EventManager::trigger方法的典型用法代码示例。如果您正苦于以下问题:PHP EventManager::trigger方法的具体用法?PHP EventManager::trigger怎么用?PHP EventManager::trigger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EventManager
的用法示例。
在下文中一共展示了EventManager::trigger方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testListener
public function testListener()
{
$subject1 = $this->getMockBuilder('Jivoo\\Core\\EventSubject')->getMock();
$subject1->method('getEvents')->willReturn(array('someEvent'));
$subject2 = $this->getMockBuilder('Jivoo\\Core\\EventSubject')->getMock();
$subject2->method('getEvents')->willReturn(array('someEvent'));
$em1 = new EventManager($subject1);
$em2 = new EventManager($subject2, $em1);
$l = $this->getMockBuilder('Jivoo\\Core\\EventListener')->setMethods(array('getEventHandlers', 'someEvent'))->getMock();
$l->method('getEventHandlers')->wilLReturn(array(get_class($subject2) . '.someEvent'));
$l->expects($this->once())->method('someEvent')->willReturn(false);
$em1->attachListener($l);
$this->assertFalse($em2->trigger('someEvent'));
$em1->detachListener($l);
$this->assertTrue($em2->trigger('someEvent'));
}
示例2: trigger
/**
* Execute all functions attached to an event.
*
* @param string $event
* Event name.
* @param Event $event
* Event object.
* @return bool False if event was stopped, true otherwise.
*/
public function trigger($name, Event $event = null)
{
if (!isset($event)) {
$event = new Event($this->subject);
}
if (isset($this->parent)) {
if (!$this->parent->trigger($this->subjectClass . '.' . $name, $event)) {
return false;
}
}
if (isset($this->events[$name])) {
$event->name = $name;
foreach ($this->events[$name] as $function) {
$continue = call_user_func($function, $event);
if ($event->stopped or $continue === false) {
return false;
}
}
} elseif (strpos($name, '.') === false) {
throw new InvalidEventException('Event subject "' . $this->subjectClass . '" does not have event "' . $name . '"');
}
return true;
}
示例3: attach
public function attach($name, $callback, $priority = 0)
{
$this->events->insert(array($name, $callback), $priority);
}
public function trigger($name, $params = array(), $callback = null)
{
foreach ($this->events as $event) {
if ($event[0] = $name) {
$e = new Event($name, $params);
if ($r = $event[1]($e)) {
if (is_callable($callback)) {
$callback($r);
}
}
}
}
}
}
$events = new EventManager();
$events->attach('do', function ($e) {
echo "Registered first\n";
return "Hello SALAM";
}, 102);
$events->attach('do', function ($e) {
echo "Registered second\n";
return time();
}, 101);
$return = $events->trigger('do', array('a', 'b', 'c'), function ($r) {
echo "<h1>{$r}</h1>\n";
});
echo "<h1>{$return}</h1>";
示例4: myEvent
$events = $eventManager->getEventsByGroup('system');
echo "Events: \n";
print_r($events);
echo "\nRegistering an 'echo'/'system' event...\n";
$eventManager->register("echo", "system", "myEvent");
echo "\nRegistering an 'echo'/'system' event...\n";
$eventManager->register("echo", "system", "myEvent2");
echo "\nRegistering a 'script'/'project' event...\n";
$eventManager->register("script", "project", "Test::event");
echo "\nGetting events for 'system' group...\n";
$events = $eventManager->getEventsByGroup('system');
echo "Events: \n";
print_r($events);
echo "\nTriggering said event with 'apple' and 'water' passed...\n";
echo "Also triggering event 2...\n";
$eventManager->trigger("echo", "system", array('fruit' => 'apple', 'beverage' => 'water'));
echo "\nRemoving said event...\n";
$eventManager->remove("echo", "system", "myEvent");
echo "\nGetting events for 'system' group...\n";
$events = $eventManager->getEventsByGroup('system');
echo "Events: \n";
print_r($events);
echo "\nRemoving all echo event...\n";
$eventManager->remove("echo", "system");
echo "\nGetting events for 'system' group...\n";
$events = $eventManager->getEventsByGroup('system');
echo "Events: \n";
print_r($events);
echo "\nTriggering 'script' event with 'hck' passed...\n";
$eventManager->trigger("script", "project", array('project' => 'hck'));
function myEvent($params)
示例5: attach
class EventManager
{
private $events = array();
public function attach($name, $callback)
{
$this->events[$name][] = $callback;
}
public function trigger($name, $params = array())
{
foreach ($this->events[$name] as $event => $callback) {
$e = new Event($name, $params);
$callback($e);
}
}
}
$events = new EventManager();
$events->attach('do', function ($e) {
echo $e->getName() . "\n";
print_r($e->getParams());
});
$hello = ['a' => 'qwerty', 'b' => time()];
$events->attach('do', function ($e) use($hello) {
echo "<br/> Second Chance";
echo $e->getName() . "\n";
print_r($e->getParams());
print_r($hello);
return array(time());
});
echo "<hr/>";
echo $events->trigger('do', array('a', 'b', 'c'));
示例6: triggerEvent
/**
* Trigger an event on this object.
*
* @param string $name
* Name of event.
* @param Event $event
* Event object.
* @return bool False if event was stopped, true otherwise.
*/
public function triggerEvent($name, Event $event = null)
{
return $this->e->trigger($name, $event);
}