Exemplo n.º 1
0
/**
 * @author Marco Bunge
 * @copyright 2012 Marco Bunge <*****@*****.**>
 */
use Efika\Common\Logger;
use Efika\EventManager\EventManager;
use WebApplication\OutputHandler;
/**
 * An procedural example. This example would also work with complex
 * nested logic for example in MVC structure.
 */
require_once __DIR__ . '/../../app/boot/bootstrap.php';
Logger::getInstance()->addMessage('init start');
$outputHandler = new OutputHandler();
$em = new EventManager();
Logger::getInstance()->addMessage('init complete');
Logger::getInstance()->addMessage('attaching start');
//a simple chain
//add some lines of text
$em->attachEventHandler('onAssignOutput', function () use($outputHandler) {
    $outputHandler->addContent('Hello World');
    $outputHandler->addContent('some lines of code');
})->attachEventHandler('onAssignOutput', function () use($outputHandler) {
    $outputHandler->addContent('<b>A List of numbers</b>');
    $outputHandler->addContent('<ul>');
    for ($n = 0; $n < 10; $n++) {
        $outputHandler->addContent('<li>' . $n . '</li>');
    }
    $outputHandler->addContent('</ul>');
})->attachEventHandler('onDisplayOutput', function () use($outputHandler) {
<?php

/**
 * @author Marco Bunge
 * @copyright 2012 Marco Bunge <*****@*****.**>
 */
use Efika\Common\Logger;
use Efika\EventManager\EventManager;
require_once __DIR__ . '/../../app/boot/bootstrap.php';
echo '<pre>';
$em = new EventManager();
/**
 * 1. Attach an aggregate
 */
class CustomEventAggregate implements \Efika\EventManager\EventHandlerAggregateInterface
{
    /**
     * Attach events to parent event observer
     *
     * @param \Efika\EventManager\EventManagerInterface $parent
     *
     * @return mixed
     */
    public function attach($parent)
    {
        $parent->attachEventHandler('custom.eventName.foo', function ($e) {
            echo 'Attach event(\'s)';
        });
        $parent->attachEventHandler(array('custom.eventName.foo' => function ($e) {
            echo 'Attach event(\'s) in array';
        }, 'custom.eventName.bar' => array(function ($e) {