PHP Classes

File: Falcraft/examples/Event/Tracker.php

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

Contents

Class file image Download
<?php

require_once( __DIR__ . '/../../Event/Tracker.php' );
require_once(
__DIR__ . '/../../Event/Handler.php' );
require_once(
__DIR__ . '/../../Event/Resource/TrackerInterface.php' );
require_once(
__DIR__ . '/../../Event/Resource/AwareInterface.php' );
   
use
Falcraft\Event;
use
Falcraft\Event\Resource as EventResource;
   
echo
"Falcraft\\Event\\Handler.php Test\n";
echo
"-------------------------------\n\n";

echo
"Basic Instantiation -- \n";

echo
" Class Definition -> ";

$success = true;

try {
    class
TestClass implements EventResource\AwareInterface
   
{
        private
$events;
       
        public function
setTracker(EventResource\TrackerInterface $events)
        {
            if (!
$this->events)
            {
               
$this->events = new Event\Tracker();
            }
        }
       
        public function
getTracker()
        {
            if (
null === $this->events) {
               
$this->setTracker(new Event\Tracker());
            }
           
            return
$this->events;
        }
       
        public function
setOff()
        {
           
$this->events->trigger(
               
$this,
               
__METHOD__,
               
__CLASS__,
               
__NAMESPACE__,
               
null
           
);
        }
    }
   
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
" Instantiate Class -> ";

$testClass = null;

try {
   
$testClass = new TestClass();
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Operations -- \n";

echo
" Instantiate Handler (And Attach as Listener) -> ";

$success = true;

$handler = null;

try {
   
$handler = new Event\Handler(
       
null,
        function (
$e) {echo "\n"; var_dump($e->getState()); echo "\n";},
       
null,
        array(
'strict' => true)
    );
   
   
$testClass->getTracker()->attachListener($handler);
} catch (\
Exception $e) {
   
$success = false;
}

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

echo
"Fire! -> ";

$success = true;

try {
   
$testClass->setOff();
} catch (\
Exception $e) {
   
$success = false;
}

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