PHP Classes

File: Falcraft/examples/Event/Actor.php

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

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/Actor.php' );
require_once(
__DIR__ . '/../../Event/Filter.php' );
require_once(
__DIR__ . '/../../Event/Handler.php' );
require_once(
__DIR__ . '/../../Event/GenericEvent.php' );
require_once(
__DIR__ . '/../../Patterns/Publisher.php' );

use
Falcraft\Event;
use
Falcraft\Patterns;

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

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

$success = true;

$handler = $filter = $event1 = $event2 = $publisher = $actor = null;

try {
    echo
" Instantiate Handler With Closure -> ";
   
$handler = new Event\Handler(
       
null,
        function(
$e){echo "Inside Closure";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
    echo
$handler ? "Success!\n" : "Failure...\n";
   
    echo
" Instantiate Filter (filter => tag1) -> ";
   
$filter = new Event\Filter();
   
$filter->setState(array('tags' => array('tag1',),));
   
    echo
$filter ? "Success!\n" : "Failure...\n";
   
    echo
" Instantiate Generic Events (Event1 => tag1) -> ";
   
$event1 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null,
       
null,
        array(
'tag1')
    );
   
   
$event2 = new Event\GenericEvent(null, null, null, null);
   
    echo (
$event1 && $event2) ? "Success!\n" : "Failure...\n";
   
    echo
" Instantiate Publisher -> ";
   
$publisher = new Patterns\Publisher();
   
    echo
$publisher ? "Success!\n" : "Failure...\n";
   
    echo
" Instantiate Actor ( filter, handler, publisher ) -> ";
   
$actor = new Event\Actor($filter, $handler, $publisher);
   
    echo
$actor ? "Success!\n" : "Failure...\n";
   
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\nBasic Operations -- \n\n";

$success = true;

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

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

$success = true;

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

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

echo
"\n";