PHP Classes

File: Falcraft/examples/Event/Handler.php

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

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/Handler.php' );
require_once(
__DIR__ . '/../../Event/GenericEvent.php' );
require_once(
__DIR__ . '/../../Event/Tracker.php' );
   
use
Falcraft\Event;
   
echo
"Falcraft\\Event\\Handler.php Test\n";
echo
"-------------------------------\n\n";

echo
"Basic Instantiation -- \n";

echo
" Instantiate With Closure -> ";

$success = true;

$handler = null;

try {
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo "Inside Closure";},
       
null,
       
Event\Handler::DEFAULT_PRIORITY,
        array(
'strict' => true)
    );
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Basic Operation -- \n";

$success = true;

$publisher = $event = null;

try {
    echo
" publisher = new Tracker()\n";
   
$publisher = new Event\Tracker();
   
    echo
" event = new GenericEvent()\n";
   
$event = new Event\GenericEvent(
       
null,
       
null,
       
null,
       
null
   
);
   
    echo
" publisher->attachListener( handle )\n";
   
$publisher->attachListener($handler);
   
    echo
" publisher->triggerObject( event ) -> ";
   
$publisher->triggerObject($event);
    echo
"\n";
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Additional Operations -- \n\n";

echo
" Class Method -- \n";

$success = true;

try {
    echo
" Define Class A\n";
    class
A {
        public static function
staticFunction($e)
        {
            echo
"Inside Static Function";
        }
    }
   
    echo
" handler->setObject( A ) \n";
   
$handler->setObject('A');
   
    echo
" handler->setNamespace( __NAMESPACE__ )\n";
   
$handler->setNamespace(__NAMESPACE__);
   
    echo
" handler->setFunction( staticFunction )\n";
   
$handler->setFunction('staticFunction');
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire! -> ";

$success = true;

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

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

echo
" Object Method -- \n";

$success = true;

$object = null;

try {
    echo
" Define Class B\n";
   
    class
B extends A
   
{
        public function
publicMethod($e)
        {
            echo
"Inside Public Method";
        }
    }
   
    echo
" object = new B\n";
   
$object = new B();
   
    echo
" handler->setObject( object )\n";
   
$handler->setObject($object);
   
    echo
" handler->setFunction( publicMethod )\n";
   
$handler->setFunction('publicMethod');
   
    echo
" handler->setNamespace( )\n";
   
$handler->setNamespace('');
   
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Fire! -> ";

$success = true;

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

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

echo
" Function -- \n";

try {
    echo
" Define Function\n";
    function
func($e)
    {
        echo
"Inside Function";
    }
   
    echo
" handler->setFunction( func )\n";
   
$handler->setFunction('func');
   
    echo
" handler->setObject( null )\n";
   
$handler->setObject(null);
   
    echo
" handler->setNamespace( __NAMESPACE__ )\n";
   
$handler->setNamespace(__NAMESPACE__);
   
} catch (\
Exception $e) {
   
$success = false;
}

echo
"\n Fire! -> ";

$success = true;

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

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