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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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 $fullClassName Name of this class
41 protected function __construct ($fullClassName = __CLASS__) {
42 // Call parent constructor
43 parent::__construct($fullClassName);
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 $fullClassName 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 ($fullClassName, array $args = array()) {
58 // Is the class name valid and is the class there?
59 if (empty($fullClassName)) {
60 // Throw an exception here
61 throw new InvalidArgumentException('Parameter "className" is empty');
62 } elseif (!class_exists($fullClassName)) {
63 // First get an instance of this factory
64 $factoryInstance = new ObjectFactory();
66 // Then throw an exception
67 throw new NoClassException(array($factoryInstance, $fullClassName), self::EXCEPTION_CLASS_NOT_FOUND);
70 // Split class name on backslash to check naming-convention
71 $classNameParts = explode("\\", $fullClassName);
73 // Okay, does it follow naming-convention?
74 if (count($classNameParts) < 4) {
75 // Namespaces are missing
76 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $fullClassName));
80 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName));
82 // Run the user function
83 $objectInstance = call_user_func_array(array($fullClassName, $methodName), $args);
86 self::countObject($fullClassName);
88 // Return the prepared instance
89 return $objectInstance;
93 * Creates an object by it's configured name
95 * @param $configEnttry Configuration entry to read
96 * @param $args Arguments in an indexed array
97 * @return $objectInstance An instance of the requested object
99 public static final function createObjectByConfiguredName ($configEntry, array $args = array()) {
100 // Read the configuration entry
101 $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
103 // Send this to the other factory...
104 $objectInstance = self::createObjectByName($fullClassName, $args);
106 // Return the instance
107 return $objectInstance;
111 * Strips of namespace from given class name. An exception is thrown when
112 * there is no namespace found as this is the now the norm for any objects
113 * being created with this object factory.
115 * @param $fullClassName Class name with namespace
116 * @return $shortClassName Stripped class name (no namespace)
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);
123 $shortClassName = array_pop($classNameParts);
126 return $shortClassName;