Source for file events.cls.php

Documentation is available at events.cls.php

  1. <?php
  2.  
  3.     /***********************************************\
  4.      * N2F Yverdon v0                              *
  5.      * Copyright (c) 2009 Zibings Incorporated     *
  6.      *                                             *
  7.      * You should have received a copy of the      *
  8.      * Microsoft Reciprocal License along with     *
  9.      * this program.  If not, see:                 *
  10.      * <http://opensource.org/licenses/ms-rl.html> *
  11.     \***********************************************/
  12.  
  13.     /*
  14.      * $Id: events.cls.php 24 2009-09-12 11:41:30Z amale $
  15.      */
  16.  
  17.     /**
  18.      * Core event class for N2 Framework Yverdon
  19.      *
  20.      */
  21.     class n2f_events {
  22.         /**
  23.          * Contains an array of events and hooked callbacks.
  24.          *
  25.          * @var array 
  26.          */
  27.         protected $events;
  28.  
  29.         /**
  30.          * Initializes the n2f_events object.
  31.          *
  32.          * @return n2f_events 
  33.          */
  34.         public function __construct({
  35.             $this->events = array();
  36.  
  37.             return($this);
  38.         }
  39.  
  40.         /**
  41.          * Adds an event to the n2f_events object stack.
  42.          *
  43.          * @param string $name    Name of the event to make available
  44.          * @return n2f_events 
  45.          */
  46.         protected function addEvent($name$sys_evt false{
  47.             $this->events[$namearray(
  48.                 'sys'    => $sys_evt,
  49.                 'hooks'    => array()
  50.             );
  51.  
  52.             return($this);
  53.         }
  54.  
  55.         /**
  56.          * Causes an event in the n2f_events object stack to be 'hit' or 'bubbled'.
  57.          *
  58.          * @param string $name    Name of the event to hit/bubble
  59.          * @param array $args    Arguments to pass to event hooks
  60.          * @return boolean 
  61.          */
  62.         protected function hitEvent($namearray $args null{
  63.             if (!isset($this->events[$name]|| count($this->events[$name]['hooks']1{
  64.                 return(false);
  65.             }
  66.  
  67.             if ($args === null{
  68.                 $args array();
  69.             }
  70.  
  71.             $results array();
  72.  
  73.             foreach (array_values($this->events[$name]['hooks']as $callback{
  74.                 if (is_callable($callback)) {
  75.                     if ($this->events[$name]['sys'=== true{
  76.                         $results call_user_func_array($callback$args);
  77.                     else {
  78.                         $results[array(
  79.                             'callback'    => callback_toString($callback),
  80.                             'returned'    => call_user_func_array($callback$args)
  81.                         );
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             return($results);
  87.         }
  88.  
  89.         /**
  90.          * Attaches a callback to an event in the n2f_events object stack.
  91.          *
  92.          * @param string $name        Name of the event to hook to
  93.          * @param callback $callback    Callback method/function to hook to event
  94.          * @return boolean 
  95.          */
  96.         public function hookEvent($name$callback{
  97.             if (!isset($this->events[$name])) {
  98.                 return(false);
  99.             }
  100.  
  101.             if ($this->events[$name]['sys'=== true{
  102.                 $this->events[$name]['hooks'array($callback);
  103.             else {
  104.                 $this->events[$name]['hooks'][$callback;
  105.             }
  106.  
  107.             return(true);
  108.         }
  109.     }
  110.  
  111. ?>

Documentation generated on Sat, 23 Jan 2010 11:13:41 -0500 by phpDocumentor 1.4.1