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