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