PHP Classes

File: Falcraft/examples/Event/Conduit.php

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

Contents

Class file image Download
<?php

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

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

echo
"Basic Instantiation -- \n";

echo
" Empty Instantiation (1) -> ";

$success = true;

$conduit1 = null;

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

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

echo
" Instantiaton With Filter and Handler -- \n";

$success = true;

$filter = $handlerz = null;

try {
    echo
" Instantiate Empty Filter -> ";
   
$filter = new Event\Filter();
   
    echo
"Success!\n";
   
    echo
" Instantiate Handler (Z) -> ";
   
$handlerz = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler Z\n";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
    echo
"Success!\n";
   
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"\n Instantiate Conduit (2) -> ";

$success = true;

$conduit2 = null;

try {
   
$conduit2 = new Event\Conduit(
        array(
$filter, $handlerz),
        array(),
        array(
'strict' => true)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Instantiate with Publishers Also -- \n";

$success = true;

$publisher1 = $publisher2 = $publisher3 = null;

try {
    echo
" Instantiate Publisher1 -> ";
   
$publisher1 = new Patterns\Publisher();
    echo
"Success!\n";
   
    echo
" Instantiate Publisher2 -> ";
   
$publisher2 = new Patterns\Publisher();
    echo
"Success!\n";
   
    echo
" Instantiate Publisher3 -> ";
   
$publisher3 = new Patterns\Publisher();
    echo
"Success!\n";
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Instantiate Conduit (3) -> ";

$success = true;

$conduit3 = null;

try {
   
$conduit3 = new Event\Conduit(
        array(array(
$filter, $handlerz)),
        array(
$publisher1, $publisher2, $publisher3,),
        array(
'strict' => true)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Remove Publisher1 and Publisher3 from Conduit 3 -> ";

$success = true;

try {
   
$conduit3->removePublisher($publisher1);
   
$conduit3->removePublisher($publisher3);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Prepare 2 Queues & Filters in Conduit -- \n";

$success = true;

$handler1 = $handler2 = $handler3 = $handler4 = $handler5 = $handler6 = null;
$queue1 = $queue2 = null;

try {
    echo
" Instantiate Handlers 1 - 3 and Queue 1 -> ";
   
   
$handler1 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 1\n";},
       
null,
       
1,
        array(
'strict' => true)
    );
   
   
$handler2 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 2\n";},
       
null,
       
0.5,
        array(
'strict' => true)
    );
   
   
$handler3 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 3\n";},
       
null,
       
0.3,
        array(
'strict' => true)
    );
   
   
$queue1 = new Event\HandlerPriorityQueue(array($handler1, $handler2, $handler3,));
} catch (\
Exception $e) {
   
$success = false;
}

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

try {
    echo
" Instantiate Handlers 4 - 6 and Queue 2 -> ";
   
   
$handler4 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 4\n";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
   
$handler5 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 5\n";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
   
$handler6 = new Event\Handler(
       
null,
        function(
$e){echo "Inside Handler 6\n";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
   
   
$queue2 = new Event\HandlerPriorityQueue(array($handler4, $handler5, $handler6,));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Instantiate Filter 1 (tag1) and Filter 2 (tag2) -> ";

$success = true;

$filter1 = $filter2 = null;

try {
   
$filter1 = new Event\Filter();
   
$filter1->setState(array('tags' => array('tag1',)));
   
   
$filter2 = new Event\Filter();
   
$filter2->setState(array('tags' => array('tag2',)));
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Instantiate Conduit (4) With Filters, Queues, and Publishers -> ";

$success = true;

$conduit4 = null;

try {
   
$conduit4 = new Event\Conduit(
        array(array(
$filter1, $queue1),
              array(
$filter2, $queue2),),
        array(
$publisher1, $publisher2, $publisher3),
        array(
'strict' => true)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Basic Operations -- \n";

echo
" Event Firing -- \n";

echo
" Insantiate Events (event2 -> tag1, event3 -> tag2)-> ";

$success = true;

try {
   
$event1 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null
   
);
   
   
$event2 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null,
       
null,
        array(
'tag1')
    );
   
   
$event3 = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null,
       
null,
        array(
'tag2')
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire Publisher 1 (Event1) -- ";

$success = true;

try {
   
$publisher1->setState($event1);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire Publisher 2 (Event2) -- ";

$success = true;

try {
   
$publisher2->setState($event2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Remove Publisher 2 from Conduit 3 -> ";

$success = true;

try {
   
$conduit3->removePublisher($publisher2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire Publisher 2 (Event2) -- ";

$success = true;

try {
   
$publisher2->setState($event2);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire Publisher 3 (Event3) -- ";

$success = true;

try {
   
$publisher3->setState($event3);
} catch (\
Exception $e) {
   
$success = false;
}

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