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