PHP Classes

File: Falcraft/examples/Event/HandlerPriorityQueue.php

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

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/HandlerPriorityQueue.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\\HandlerPriorityQueue.php Test\n";
echo
"--------------------------------------------\n\n";

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

$success = true;

$queue = null;

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

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

echo
"Operations -- \n";
echo
" Push Several Handlers -> ";

$handler = null;

try {
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo " 2\n";},
       
null,
       
2,
        array(
'strict' => true)
    );
   
   
$queue->push($handler);
   
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo " 1\n";},
       
null,
       
1,
        array(
'strict' => true)
    );
   
   
$queue->push($handler);
   
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo " 0\n";},
       
null,
       
0,
        array(
'strict' => true)
    );
   
   
$queue->push($handler);
   
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo " A\n";},
       
null,
       
1.5,
        array(
'strict' => true)
    );
   
   
$queue->push($handler);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire! -> \n";

$success = true;

$publisher = $event = null;

try {
   
$publisher = new Patterns\Publisher();
   
   
$publisher->attachListener($queue);
   
   
$event = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null
   
);
   
   
$publisher->setState($event);
} catch (\
Exception $e) {
   
$success = false;
}

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