Source for file AObjectRegistry.php

Documentation is available at AObjectRegistry.php

  1. <?php
  2. /**
  3.  * @package Alia
  4.  * @subpackage Core
  5.  */
  6.  
  7.  
  8. require_once 'AObject.php';
  9.  
  10. if(!isset($_SESSION)){$_SESSION=array();}
  11.  
  12.  
  13.  
  14. /**
  15.  * A Singleton registry for all AObject objects
  16.  * 
  17.  * All AObject subclasses that call parent::__construct() will be automatically added to this registry.
  18.  * @package Alia
  19.  * @subpackage Core
  20.  * @version $id$
  21.  * @copyright
  22.  * @author Jordan CM Wambaugh <jordan@wambaugh.org>
  23.  * @license
  24.  */
  25. {
  26.  
  27.     private $objects;
  28.     
  29.     private $IDCounter=0;
  30.     
  31.     
  32.     static private $instanceObject=null;
  33.     
  34.     
  35.     
  36.     /**
  37.      * __construct
  38.      * 
  39.      * @return void 
  40.      */
  41.     private function __construct(){
  42.         $this->objects = array();
  43.     }
  44.     
  45.     
  46.     /**
  47.      * returns the instance of the Alia object
  48.      *
  49.      * @return Alia 
  50.      */
  51.     public function instance(){
  52.         //prevent notices
  53.         if(!isset($_SESSION['alia']['objectRegistry']))$_SESSION['alia']['objectRegistry']=null;
  54.         
  55.         if($_SESSION['alia']['objectRegistry']==null){
  56.             $_SESSION['alia']['objectRegistry'new AObjectRegistry();
  57.         }
  58.         return $_SESSION['alia']['objectRegistry'];
  59.     }
  60.     
  61.     /**
  62.      * Adds an AObject to the index, and assigns it a unique ID
  63.      *
  64.      * @param AObject $object 
  65.      */
  66.     public function addObject(AObject $object){
  67.         $this->objects[]=$object;
  68.         $this->generateID($object);
  69.     }
  70.     
  71.     /**
  72.      * returns an ArrayObject containing all AObjects
  73.      *
  74.      * @return ArrayObject 
  75.      */
  76.     public function getObjects(){
  77.         return $this->objects;
  78.     }
  79.     
  80.     
  81.     /**
  82.      * Assigns an AObject a unique ID
  83.      *
  84.      * @param AObject $object 
  85.      */
  86.     private function generateID(AObject $object){
  87.         $id=get_class($object).$this->IDCounter++;
  88.         $object->setObjectID($id);
  89.     }
  90.     
  91.     
  92.     /**
  93.      * Returns the object of id $id, if it exists.
  94.      * 
  95.      * @param mixed $id 
  96.      * @return AObject|falsethe found object or false if not found
  97.      */
  98.     public function getObjectByID($id){
  99.         foreach ($this->objects as $object){
  100.             if($object->getObjectID(== $id ){
  101.                 return $object;
  102.             }
  103.         }
  104.         return false;
  105.     }
  106.  
  107.     public function unsetObject($object){
  108.         foreach ($this->objects as $k=>$o){
  109.             if($o === $object ){
  110.                 unset($this->objects[$k]);    
  111.             }
  112.         }
  113.     }
  114.     
  115. // end of AObjectRegistry
  116. ?>

Documentation generated on Fri, 11 Jul 2008 13:28:42 -0400 by phpDocumentor 1.4.2