home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / System / Socket / Observer.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  3.7 KB  |  158 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: System :: Socket :: Observer                                 |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: Observer.php,v 1.1 2004/04/29 15:55:38 mike Exp $
  15.  
  16. /**
  17. * System::Socket::Observer
  18. * @author       Michael Wallner <mike@php.net>
  19. * @package      System_Socket
  20. * @category     System
  21. */
  22.  
  23. /** 
  24. * System_Socket_Observer
  25. *
  26. * Base class for observers which handles the unique hash code
  27. * generation and defines a thight base API.  A class implementing/extending 
  28. * System_Socket_Observer must provide an uniqe hash code retrievable by a
  29. * getHashCode() method and must also provide a notify() method.  Observer
  30. * objects may be chained because System_Socket_Observer are also observable.
  31. * @author   Michael Wallner <mike@php.net>
  32. * @version  $Revision: 1.1 $
  33. * @access   public
  34. */
  35. class System_Socket_Observer
  36. {
  37.     /**
  38.     * @access   protected
  39.     */
  40.     var $hashCode;
  41.     
  42.     /**
  43.     * @access   protected
  44.     */
  45.     var $observers = array();
  46.     
  47.     /**
  48.     * Constructor
  49.     * 
  50.     * @access   protected
  51.     * @return   object  System_Socket_Observer
  52.     */
  53.     function System_Socket_Observer()
  54.     {
  55.         $this->__construct();
  56.     }
  57.     
  58.     /**
  59.     * ZE2 Constructor
  60.     * @ignore
  61.     */
  62.     function __construct()
  63.     {
  64.         $this->generateHashCode();
  65.     }
  66.     
  67.     /**
  68.     * Generate hash code
  69.     * 
  70.     * @access   protected
  71.     * @return   void
  72.     */
  73.     function generateHashCode()
  74.     {
  75.         $this->hashCode = md5(uniqid(get_class($this), true));
  76.     }
  77.  
  78.     /**
  79.     * ZE2 copy
  80.     * @ignore
  81.     */
  82.     function __clone()
  83.     {
  84.         $new = $this;
  85.         $new->_generateHashCode();
  86.         return $new;
  87.     }
  88.     
  89.     /**
  90.     * Notify
  91.     * 
  92.     * @abstract
  93.     * @access   protected
  94.     * @param    array       $note
  95.     */
  96.     function notify($note)
  97.     {
  98.         reset($this->observers);
  99.         while (list($hashCode) = each($this->observers)){
  100.             $this->observers[$hashCode]->notify($note);
  101.         }
  102.     }
  103.     
  104.     /**
  105.     * Get hash code
  106.     * 
  107.     * @access   public
  108.     * @return   string
  109.     */
  110.     function getHashCode()
  111.     {
  112.         return $this->hashCode;
  113.     }
  114.  
  115.     /**
  116.     * Attach observer
  117.     * 
  118.     * @access   public
  119.     * @return   bool
  120.     * @param    object  System_Socket_Observer  $observer
  121.     */
  122.     function attach(&$observer)
  123.     {
  124.         if (is_a($observer, 'System_Socket_Observer')) {
  125.             $this->observers[$observer->getHashCode()] = &$observer;
  126.             return true;
  127.         }
  128.         return false;
  129.     }
  130.     
  131.     /**
  132.     * Detach observer
  133.     * 
  134.     * @access   public
  135.     * @return   void
  136.     * @param    object  System_Socket_Observer $observer
  137.     */
  138.     function detach(&$observer)
  139.     {
  140.         unset($this->observers[$observer->getHashCode()]);
  141.     }
  142.     
  143.     /** 
  144.     * Check if a specific observer is already attached
  145.     * 
  146.     * @access   public
  147.     * @return   bool
  148.     * @param    object  System_Socket_Observer $observer
  149.     */
  150.     function attached(&$observer)
  151.     {
  152.         return isset($this->observers[$observer->getHashCode()]);
  153.     }
  154.     
  155. }
  156. ?>