3 namespace Org\Mxchange\CoreFramework\Factory\Object;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
8 use Org\Mxchange\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 - 2020 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 (string $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 $fullClassName 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 InvalidArgumentException If className is empty or the name not following naming-convention
58 public static final function createObjectByName (string $fullClassName, array $args = []) {
59 // Is the class name valid and is the class there?
60 if (empty($fullClassName)) {
61 // Throw an exception here
62 throw new InvalidArgumentException('Parameter "fullClassName" is empty');
63 } elseif (!self::isClassCounted($fullClassName) && !class_exists($fullClassName)) {
64 // First get an instance of this factory
65 $factoryInstance = new ObjectFactory();
67 // Then throw an exception
68 throw new NoClassException(array($factoryInstance, $fullClassName), self::EXCEPTION_CLASS_NOT_FOUND);
71 // Split class name on backslash to check naming-convention
72 $classNameParts = explode("\\", $fullClassName);
74 // Okay, does it follow naming-convention?
75 if (count($classNameParts) < 4) {
76 // Namespaces are missing
77 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $fullClassName));
81 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName));
83 // Run the user function
84 $objectInstance = call_user_func_array([$fullClassName, $methodName], $args);
87 self::countObject($fullClassName);
89 // Return the prepared instance
90 return $objectInstance;
94 * Creates an object by it's configured name
96 * @param $configKey Configuration key 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 (string $configKey, array $args = []) {
101 // Read the configuration entry
102 $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey);
104 // Send this to the other factory...
105 $objectInstance = self::createObjectByName($fullClassName, $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)
119 private static function stripNamespaceFromClassName (string $fullClassName) {
120 // The class name should contain at least 2 back-slashes, so split at them
121 $classNameParts = explode("\\", $fullClassName);
124 $shortClassName = array_pop($classNameParts);
127 return $shortClassName;