PEIP_Generic_Builder.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of the PEIP package.
00005  * (c) 2010 Timo Michna <timomichna/yahoo.de>
00006  * 
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00021 class PEIP_Generic_Builder 
00022     implements 
00023         PEIP_INF_Singleton_Map {
00024 
00025     protected 
00026         $className,
00027         $reflectionClass;
00028     
00029     protected static 
00030         $instances = array();   
00031     
00032     
00040     public function __construct($className, ReflectionClass $reflectionClass = NULL, $storeRef = true){ 
00041         if(!class_exists($className)){
00042             spl_autoload_call($className);
00043         }       
00044         $this->className = $className;
00045         if($reflectionClass){
00046             if($className != $reflectionClass->getName()){
00047                 throw new Exception('Constructing PEIP_Generic_Builder with wrong ReflectionClass'); 
00048             }   
00049             $this->reflectionClass = $reflectionClass;  
00050         }
00051         if($storeRef){
00052             self::$instances[$className] = $this;
00053         }            
00054     }
00055     
00056     public static function getInstance($className){
00057         if(!array_key_exists((string)$className, self::$instances)) {
00058             new PEIP_Generic_Builder($className);
00059         }
00060         return self::$instances[$className];
00061     }
00062 
00063     
00069     public function build(array $arguments = array()){      
00070         if($constructor = $this->getReflectionClass()->getConstructor()){
00071             if(count($arguments) < $constructor->getNumberOfRequiredParameters()){ 
00072                 throw new Exception('Missing Argument '.(count($arguments) + 1).' for '.$className.'::__construct');
00073             }       
00074             return $this->getReflectionClass()->newInstanceArgs($arguments);
00075         }else{
00076             return $this->getReflectionClass()->newInstance();
00077         }               
00078     }
00079 
00080     
00085     protected function getReflectionClass(){
00086         return $this->reflectionClass 
00087             ? $this->reflectionClass 
00088             : $this->reflectionClass = new ReflectionClass($this->className); 
00089     }
00090     
00091 }

Generated on Mon Apr 12 11:39:00 2010 for PEIP by  doxygen 1.5.1