PHP Classes

File: Falcraft/examples/Event/Aggregator.php

Recommend this page to a friend!
  Classes of Asher Wolfstein   Universal PHP Event System   Falcraft/examples/Event/Aggregator.php   Download  
File: Falcraft/examples/Event/Aggregator.php
Role: Example script
Content type: text/plain
Description: Event Aggregator Example
Class: Universal PHP Event System
Manage events with the Observer/Publisher patterns
Author: By
Last change:
Date: 8 years ago
Size: 5,477 bytes
 

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/Aggregator.php' );
require_once(
__DIR__ . '/../../Event/Handler.php' );
require_once(
__DIR__ . '/../../Event/GenericEvent.php' );
require_once(
__DIR__ . '/../../Event/Filter.php' );
require_once(
__DIR__ . '/../../Patterns/Publisher.php' );
   
use
Falcraft\Event;
use
Falcraft\Patterns;

echo
"Falcraft\\Event\\Aggregator.php Test\n";
echo
"----------------------------------\n\n";

echo
"Basic Instantiation -- \n\n";

echo
" Empty Instantiation -> ";

$success = true;

$aggregator = null;

try {
   
$aggregator = new Event\Aggregator();
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
"Basic Operations -- \n\n";

$success = true;

$handler = null;

try {
    echo
" Instantiate Handler With Closure -> ";
   
$handler = new Event\Handler(
       
null,
        function(
$e){echo "Aggregator: " . $e->getData() . "\n";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
    echo
$handler ? "Success!\n" : "Failure...\n";
   
    echo
" Attach Handler to Aggregator -> ";
   
   
$aggregator->attachListener($handler);
   
    echo
"Success!\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED\n\n";
}

$success = true;

$publisher = null;

try {
    echo
" Instantiate Publisher -> ";
   
$publisher = new Patterns\Publisher();
    echo
$publisher ? "Success!\n" : "Failure...\n";
   
    echo
" Add Publisher to Aggregate -> ";
   
$aggregator->addPublisher($publisher);
   
    echo
"Success!\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED\n\n";
}

$success = true;

$event1 = null;

try {
    echo
" Instantiating Generic Event (tag1) -> ";
   
   
$event1 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null,
       
1,
        array(
'tag1')
    );
   
    echo
$event1 ? "Success!\n" : "Failure...\n";
   
    echo
" Fire Event -> ";
   
   
$publisher->setState($event1);
   
    echo
"\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED\n\n";
}

echo
"\nExtended Operations -- \n\n";

echo
" Multiple Publishers -- \n\n";

$success = true;

$publisher2 = $event2 = null;

try {
    echo
" Instantiate Publisher2 -> ";
   
$publisher2 = new Patterns\Publisher();
   
    echo
$publisher2 ? "Success!\n" : "Failure...\n";
   
    echo
" Add Publisher to Aggregate -> ";
   
$aggregator->addPublisher($publisher2);
   
    echo
"Success!\n";
   
    echo
" Instantiate Event2 (tag2) -> ";
   
   
$event2 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null,
       
2,
        array(
'tag2')
    );
   
    echo
$event2 ? "Success!\n" : "Failure...\n";
   
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED!\n\n";
}

$success = true;

try {
    echo
" Fire Publisher2 With Event2 -> ";
   
$publisher2->setState($event2);
    echo
"\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
" Success!\n";
} else {
    echo
" Failure...\n";
}

$success = true;

try {
    echo
" Remove Publisher(1) -> ";
   
   
$aggregator->removePublisher($publisher);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

$success = true;

try {
    echo
" Fire Publisher(1) with Event1 -> ";
   
$publisher->setState($event1);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n\n";
} else {
    echo
"Failure...\n\n";
}

echo
" Multiple Observers -- \n\n";

$success = true;

$handler2 = null;

try {
    echo
" Instantiate Handler2 (Closure) -> ";
   
$handler2 = new Event\Handler(
       
null,
        function(
$e){echo "(Handler2) Aggregator: " . $e->getData();},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
    echo
$handler2 ? "Success\n" : "Failure...\n";
   
    echo
" Attach Handler2 to Aggregate -> ";
   
$aggregator->attachListener($handler2);
   
    echo
"Success!\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED!\n\n";
}

$success = true;

try {
    echo
" Fire Publisher2 with Event2 -> ";
   
$publisher2->setState($event2);
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
" Success!\n\n";
} else {
    echo
" Failure...\n\n";
}

echo
" Filter -- \n";

$success = true;

try {
    echo
" Instantiate Filter (tag1) -> ";
   
$filter = new Event\Filter();
   
$filter->setState(array('tags' => array('tag1')));
   
    echo
$filter ? "Success!\n" : "Failure...\n";
   
    echo
" Set Filter on Aggregate -> ";
   
$aggregator->setFilter($filter);
   
    echo
"Success!\n";
} catch (\
Exception $e) {
   
$success = false;
}

$success = true;

try {
    echo
" Fire Publisher2 with Event2 -> ";
   
$publisher2->setState($event2);
   
    echo
"\n Fire Publisher2 with Event1 -> ";
   
$publisher2->setState($event1);
   
    echo
"\n";
} catch (\
Exception $e) {
   
$success = false;
}

if (!
$success) {
    echo
"EXCEPTION RAISED\n\n";
}