]> git.mxchange.org Git - mailer.git/blobdiff - inc/classes/main/factories/objects/class_ObjectFactory.php
Launcher scripts updated
[mailer.git] / inc / classes / main / factories / objects / class_ObjectFactory.php
index 3f8ac7fa61b116b5dd93c4d5b9735f8bec1fc0de..0848e79fabd832f0405555e47de65481b1629c57 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class ObjectFactory extends BaseFactory {
+       /**
+        * Total objects generated
+        */
+       private static $total = 0;
+
        /**
         * Protected constructor
         *
@@ -30,12 +35,6 @@ class ObjectFactory extends BaseFactory {
        protected function __construct () {
                // Call parent constructor
                parent::__construct(__CLASS__);
-
-               // Set object description
-               $this->setObjectDescription("Simple generic object factory");
-
-               // Create unique ID number
-               $this->createUniqueID();
        }
 
        /**
@@ -43,32 +42,38 @@ class ObjectFactory extends BaseFactory {
         * the class was not found. No parameters for the object are currently
         * supported.
         *
-        * @param       $className                              Name of the class we shall construct
-        * @return      $objectInstance                 An instance of the requested object
+        * @param       $className                      Name of the class we shall construct
+        * @param       $args                           Arguments in an indexed array
+        * @return      $objectInstance         An instance of the requested object
         * @throws      ClassNotFoundException  If the requested class was not found
         * @throws      EmptyVariableException  If a variable is empty unexpectly
         */
-       public final static function createObjectByName ($className) {
-               // First get an instance of this factory
-               $factoryInstance = new ObjectFactory();
-
+       public final static function createObjectByName ($className, array $args=array()) {
                // Is the class name valid and is the class there?
                if (empty($className)) {
+                       // First get an instance of this factory
+                       $factoryInstance = new ObjectFactory();
+
                        // Throw an exception here
                        throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
                } elseif (!class_exists($className)) {
+                       // First get an instance of this factory
+                       $factoryInstance = new ObjectFactory();
+
                        // Then throw an exception
                        throw new ClassNotFoundException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
                }
 
-               // Then Prepare the eval() command
-               $eval = sprintf("\$objectInstance = %s::create%s();",
-                       $className,
+               // Create method name
+               $methodName = sprintf("create%s",
                        $className
                );
 
-               // Run the command
-               eval($eval);
+               // Run the user function
+               $objectInstance = call_user_func_array(array($className, $methodName), $args);
+
+               // Count generated objects up
+               self::$total++;
 
                // Return the prepared instance
                return $objectInstance;
@@ -77,19 +82,29 @@ class ObjectFactory extends BaseFactory {
        /**
         * Creates an object by it's configured name
         *
-        * @param       $configEnttry                   Configuration entry to read
-        * @return      $objectInstance                 An instance of the requested object
+        * @param       $configEnttry           Configuration entry to read
+        * @param       $args                           Arguments in an indexed array
+        * @return      $objectInstance         An instance of the requested object
         */
-       public final static function createObjectByConfiguredName ($configEntry) {
+       public final static function createObjectByConfiguredName ($configEntry, array $args=array()) {
                // Read the configuration entry
                $className = FrameworkConfiguration::getInstance()->readConfig($configEntry);
 
                // Send this to the other factory...
-               $objectInstance = self::createObjectByName($className);
+               $objectInstance = self::createObjectByName($className, $args);
 
                // Return the instance
                return $objectInstance;
        }
+
+       /**
+        * Static getter for total object count
+        *
+        * @return      $total  Total amount of generated objects
+        */
+       public final static function getTotal () {
+               return self::$total;
+       }
 }
 
 // [EOF]