Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/base/observer.php

Documentation is available at observer.php

  1. <?php
  2. /**
  3. @version        $Id: observer.php 6472 2007-02-03 10:47:26Z pasamio $
  4. @package        Joomla.Framework
  5. @subpackage    Base
  6. @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7. @license        GNU/GPL, see LICENSE.php
  8. *  Joomla! is free software. This version may have been modified pursuant
  9. *  to the GNU General Public License, and as distributed it includes or
  10. *  is derivative of works licensed under the GNU General Public License or
  11. *  other free or open source software licenses.
  12. *  See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Abstract observer class to implement the observer design pattern
  20.  *
  21.  * @abstract
  22.  * @author        Louis Landry <[email protected]>
  23.  * @package        Joomla.Framework
  24.  * @subpackage    Base
  25.  * @since        1.5
  26.  */
  27. class JObserver extends JObject {
  28.  
  29.     /**
  30.      * Event object to observe
  31.      *
  32.      * @access private
  33.      * @var object 
  34.      */
  35.     var $_subject null;
  36.  
  37.     /**
  38.      * Constructor
  39.      */
  40.     function __construct($subject)
  41.     {
  42.         // Register the observer ($this) so we can be notified
  43.         $subject->attach($this);
  44.  
  45.         // Set the subject to observe
  46.         $this->_subject $subject;
  47.     }
  48.  
  49.     /**
  50.      * Method to update the state of observable objects
  51.      *
  52.      * @abstract Implement in child classes
  53.      * @access public
  54.      * @return mixed 
  55.      */
  56.     function update({
  57.         return JError::raiseError('9''JObserver::update: Method not implemented''This method should be implemented in a child class');
  58.     }
  59. }
  60.  
  61. /**
  62.  * Abstract observable class to implement the observer design pattern
  63.  *
  64.  * @abstract
  65.  * @author        Louis Landry <[email protected]>
  66.  * @package        Joomla.Framework
  67.  * @subpackage    Base
  68.  * @since        1.5
  69.  */
  70. class JObservable extends JObject
  71. {
  72.     /**
  73.      * An array of Observer objects to notify
  74.      *
  75.      * @access private
  76.      * @var array 
  77.      */
  78.     var $_observers array();
  79.  
  80.     /**
  81.      * The state of the observable object
  82.      *
  83.      * @access private
  84.      * @var mixed 
  85.      */
  86.     var $_state null;
  87.  
  88.  
  89.     /**
  90.      * Constructor
  91.      */
  92.     function __construct({
  93.         $this->_observers array();
  94.     }
  95.  
  96.     /**
  97.      * Get the state of the JObservable object
  98.      *
  99.      * @access public
  100.      * @return mixed The state of the object
  101.      * @since 1.5
  102.      */
  103.     function getState({
  104.         return $this->_state;
  105.     }
  106.  
  107.     /**
  108.      * Update each attached observer object and return an array of their return values
  109.      *
  110.      * @access public
  111.      * @return array Array of return values from the observers
  112.      * @since 1.5
  113.      */
  114.     function notify()
  115.     {
  116.         // Iterate through the _observers array
  117.         foreach ($this->_observers as $observer{
  118.             $return[$observer->update();
  119.         }
  120.         return $return;
  121.     }
  122.  
  123.     /**
  124.      * Attach an observer object
  125.      *
  126.      * @access public
  127.      * @param object $observer An observer object to attach
  128.      * @return void 
  129.      * @since 1.5
  130.      */
  131.     function attach&$observer)
  132.     {
  133.         // Make sure we haven't already attached this object as an observer
  134.         if (is_object($observer))
  135.         {
  136.             $class get_class($observer);
  137.             foreach ($this->_observers as $check{
  138.                 if (is_a($check$class)) {
  139.                     return;
  140.                 }
  141.             }
  142.             $this->_observers[=$observer;
  143.         else {
  144.             $this->_observers[=$observer;
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Detach an observer object
  150.      *
  151.      * @access public
  152.      * @param object $observer An observer object to detach
  153.      * @return boolean True if the observer object was detached
  154.      * @since 1.5
  155.      */
  156.     function detach$observer)
  157.     {
  158.         // Initialize variables
  159.         $retval false;
  160.  
  161.         $key array_search($observer$this->_observers);
  162.  
  163.         if $key !== false )
  164.         {
  165.             unset($this->_observers[$key]);
  166.             $retval true;
  167.         }
  168.         return $retval;
  169.     }
  170. }
  171. ?>

Documentation generated on Mon, 05 Mar 2007 21:12:12 +0000 by phpDocumentor 1.3.1