More code merged from ship-simu
[mailer.git] / inc / classes / main / factories / objects / class_ObjectFactory.php
index 3f8ac7fa61b116b5dd93c4d5b9735f8bec1fc0de..024020d6d4782d93e9063712e7715c34da869537 100644 (file)
  * 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
         *
@@ -35,7 +40,7 @@ class ObjectFactory extends BaseFactory {
                $this->setObjectDescription("Simple generic object factory");
 
                // Create unique ID number
-               $this->createUniqueID();
+               $this->generateUniqueId();
        }
 
        /**
@@ -43,12 +48,13 @@ 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) {
+       public final static function createObjectByName ($className, array $args=array()) {
                // First get an instance of this factory
                $factoryInstance = new ObjectFactory();
 
@@ -61,14 +67,17 @@ class ObjectFactory extends BaseFactory {
                        throw new ClassNotFoundException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
                }
 
-               // Then Prepare the eval() command
-               $eval = sprintf("\$objectInstance = %s::create%s();",
+               // Then Prepare the call-back function
+               $callback = sprintf("%s::create%s",
                        $className,
                        $className
                );
 
-               // Run the command
-               eval($eval);
+               // Run the user function
+               $objectInstance = call_user_func_array($callback, $args);
+
+               // Count generated objects up
+               self::$total++;
 
                // Return the prepared instance
                return $objectInstance;
@@ -77,19 +86,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]