Login   Register  
PHP Classes
elePHPant
Icontem

File: sample.event.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Frédéric Minne  >  Event Driven  >  sample.event.php  >  Download  
File: sample.event.php
Role: Example script
Content type: text/plain
Description: Sample code for event driven architecture
Class: Event Driven
Manage abstract event processing
Author: By
Last change: Update sample
Date: 7 years ago
Size: 3,934 bytes
 

Contents

Class file image Download
<?php // $Id$

    // vim: expandtab sw=4 ts=4 sts=4:
    
    # ***** BEGIN LICENSE BLOCK *****
    # This file is part of EventManager.
    # Copyright (c) 2005-2007 Frederic Minne <zefredz@gmail.com>.
    # All rights reserved.
    #
    # EventManager is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; either version 2 of the License, or
    # (at your option) any later version.
    #
    # EventManager is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with DotClear; if not, write to the Free Software
    # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    #
    # ***** END LICENSE BLOCK *****
    
    
require_once dirname(__FILE__) . "/event/inc.event.php";

    
// ---------------------- Test Class --------------------

    /**
     * Sample event driven object
     */
    
class MyEventDrivenObject extends EventDriven
    
{
        
/**
         * sample event callback
         */
        
function onHelloEventCallMe( )
        {
            echo 
"Hello World\n";
        }

        
/**
         * sample event callback
         */
        
function onHelloEventCallMe2( )
        {
            echo 
"Hello World 2\n";
        }

        
/**
         * sample event callback
         */
        
function onAnotherEventCallMe( )
        {
            echo 
"another event occurs\n";
        }

        
/**
         * main() method
         * @static
         */
        
function main( )
        {
            echo 
"> creating objects...\n";
            
$registry EventManager::getInstance();
            
$edo = new MyEventDrivenObject();
            
$em = new EventGenerator();
            echo 
"> done\n\n";

            echo 
"> registrering listeners\n";
            
$id1 $edo->addListener"hello"'onHelloEventCallMe' );
            
$id2 $edo->addListener"another"'onAnotherEventCallMe' );
            echo 
"> done\n\n";

            echo 
"> sending events\n";
            
$em->sendEvent( new Event("hello") );
            
$em->sendEvent( new Event("another") );
            echo 
"> done\n\n";

            echo 
"> lists\n";
            echo 
"* registry:\n";
            
$registry->listRegisteredListeners( );

            echo 
"> registrering another hello listener\n";
            
$id3 $edo->addListener"hello"'onHelloEventCallMe2' );
            echo 
"> done\n\n";

            echo 
"> lists\n";
            echo 
"* registry:\n";
            
$registry->listRegisteredListeners( );

            echo 
"> sending events\n";
            
$em->sendEvent( new Event("hello") );
            
$em->sendEvent( new Event("another") );
            echo 
"> done\n\n";
            
            echo 
"> sending events staticaly\n";
            
EventManager::notify( new Event("hello") );
            
EventManager::notify( new Event("another") );
            echo 
"> done\n\n";

            echo 
"> unregistrering another and second hello listener\n";
            
$edo->removeListener"another"$id2 );
            
$edo->removeListener"hello"$id3 );
            echo 
"> done\n\n";

            echo 
"> lists\n";
            echo 
"* registry:\n";
            
$registry->listRegisteredListeners( );

            echo 
"> sending events\n";
            
$em->sendEvent( new Event("hello") );
            
$em->sendEvent( new Event("another") );
            echo 
"> done\n";
        }
    }

    
// -------------------- Run Test ---------------------

    
if ( ! defined"DEBUG_MODE" ) ) define "DEBUG_MODE"true );
    echo 
"<pre>";
    
MyEventDrivenObject::main();
    echo 
"</pre>";
?>