X-Git-Url: https://git.mxchange.org/?p=hub.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ffactories%2Fobjects%2Fclass_ObjectFactory.php;fp=inc%2Fclasses%2Fmain%2Ffactories%2Fobjects%2Fclass_ObjectFactory.php;h=fd983cac70f5da31b2ee42980200fc46a741960a;hp=0000000000000000000000000000000000000000;hb=af51358ab86a9d468bc642fdcaf931a339e6e439;hpb=9f6d7db3fa24270ec0c8a856a346575abd932918 diff --git a/inc/classes/main/factories/objects/class_ObjectFactory.php b/inc/classes/main/factories/objects/class_ObjectFactory.php new file mode 100644 index 000000000..fd983cac7 --- /dev/null +++ b/inc/classes/main/factories/objects/class_ObjectFactory.php @@ -0,0 +1,77 @@ + + * @version 0.0.0 + * @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 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class ObjectFactory extends BaseFactory { + /** + * Protected constructor + * + * @param $class Name of the real class (not BaseFactory) + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + + // Set object description + $this->setObjectDescription("Objekte-Fabrik"); + } + + /** + * Creates a new object given by the name or throws an exception if + * 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 + * @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(); + + // Is the class name valid and is the class there? + if (empty($className)) { + // Throw an exception here + throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif (!class_exists($className)) { + // Then throw an exception + throw new ClassNotFoundException($className, self::EXCEPTION_CLASS_NOT_FOUND); + } + + // Then Prepare the eval() command + $eval = sprintf("\$objectInstance = %s::create%s();", + $className, + $className + ); + + // Run the command + eval($eval); + + // Return the prepared instance + return $objectInstance; + } +} + +// [EOF] +?>