3 namespace CoreFramework\Factory;
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\Loader\NoClassException;
10 use \InvalidArgumentException;
13 * An general object factory
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class ObjectFactory extends BaseFactory {
36 * Protected constructor
38 * @param $className Name of this class
41 protected function __construct ($className = __CLASS__) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Creates a new object given by the name or throws an exception if
48 * the class was not found. No parameters for the object are currently
51 * @param $className Name of the class we shall construct
52 * @param $args Arguments in an indexed array
53 * @return $objectInstance An instance of the requested object
54 * @throws NoClassException If the requested class was not found
55 * @throws InvalidArgumentException If className is empty or the name not following naming-convention
57 public static final function createObjectByName ($className, array $args = array()) {
58 // First get an instance of this factory
59 $factoryInstance = new ObjectFactory();
61 // Split class name on backslash to check naming-convention
62 $classNameParts = explode("\\", $className);
64 // Is the class name valid and is the class there?
65 if (empty($className)) {
66 // Throw an exception here
67 throw new InvalidArgumentException('Parameter "className" is empty');
68 } elseif (!class_exists($className)) {
69 // Then throw an exception
70 throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
71 } elseif (count($classNameParts) < 3) {
72 // Namespaces are missing
73 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
77 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
79 // Run the user function
80 $objectInstance = call_user_func_array(array($className, $methodName), $args);
83 self::countObject($className);
85 // Return the prepared instance
86 return $objectInstance;
90 * Creates an object by it's configured name
92 * @param $configEnttry Configuration entry to read
93 * @param $args Arguments in an indexed array
94 * @return $objectInstance An instance of the requested object
96 public static final function createObjectByConfiguredName ($configEntry, array $args = array()) {
97 // Read the configuration entry
98 $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
100 // Send this to the other factory...
101 $objectInstance = self::createObjectByName($className, $args);
103 // Return the instance
104 return $objectInstance;
108 * Strips of namespace from given class name. An exception is thrown when
109 * there is no namespace found as this is the now the norm for any objects
110 * being created with this object factory.
112 * @param $fullClassName Class name with namespace
113 * @return $shortClassName Stripped class name (no namespace)
114 * @throws InvalidClassNameException If the class name does not follow naming convention
116 private static function stripNamespaceFromClassName ($fullClassName) {
117 // The class name should contain at least 2 back-slashes, so split at them
118 $classNameParts = explode("\\", $fullClassName);
120 // At least 3 parts should be there
121 if (count($classNameParts) < 3) {
122 // Namespace scheme is: Project\Package[\SubPackage...]
123 throw new InvalidClassNameException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
127 $shortClassName = array_pop($classNameParts);
130 return $shortClassName;