3 namespace CoreFramework\Factory;
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7 use CoreFramework\Generic\EmptyVariableException;
8 use CoreFramework\Loader\NoClassException;
11 * An general object factory
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class ObjectFactory extends BaseFactory {
34 * Protected constructor
36 * @param $className Name of this class
39 protected function __construct ($className = __CLASS__) {
40 // Call parent constructor
41 parent::__construct($className);
45 * Creates a new object given by the name or throws an exception if
46 * the class was not found. No parameters for the object are currently
49 * @param $className Name of the class we shall construct
50 * @param $args Arguments in an indexed array
51 * @return $objectInstance An instance of the requested object
52 * @throws NoClassException If the requested class was not found
53 * @throws EmptyVariableException If a variable is empty unexpectly
55 public static final function createObjectByName ($className, array $args=array()) {
56 // First get an instance of this factory
57 $factoryInstance = new ObjectFactory();
59 // Is the class name valid and is the class there?
60 if (empty($className)) {
61 // Throw an exception here
62 throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
63 } elseif (!class_exists($className)) {
64 // Then throw an exception
65 throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
69 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
71 // Run the user function
72 $objectInstance = call_user_func_array(array($className, $methodName), $args);
75 self::countObject($className);
77 // Return the prepared instance
78 return $objectInstance;
82 * Creates an object by it's configured name
84 * @param $configEnttry Configuration entry to read
85 * @param $args Arguments in an indexed array
86 * @return $objectInstance An instance of the requested object
88 public static final function createObjectByConfiguredName ($configEntry, array $args=array()) {
89 // Read the configuration entry
90 $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry($configEntry);
92 // Send this to the other factory...
93 $objectInstance = self::createObjectByName($className, $args);
95 // Return the instance
96 return $objectInstance;
100 * Strips of namespace from given class name. An exception is thrown when
101 * there is no namespace found as this is the now the norm for any objects
102 * being created with this object factory.
104 * @param $fullClassName Class name with namespace
105 * @return $shortClassName Stripped class name (no namespace)
106 * @throws InvalidClassNameException If the class name does not follow naming convention
108 private static function stripNamespaceFromClassName ($fullClassName) {
109 // The class name should contain at least 2 back-slashes, so split at them
110 $classNameParts = explode("\\", $fullClassName);
112 // At least 3 parts should be there
113 if (count($classNameParts) < 3) {
114 // Namespace scheme is: Project\Package[\SubPackage...]
115 throw new InvalidClassNameException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
119 $shortClassName = array_pop($classNameParts);
122 return $shortClassName;