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\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Loader\NoClassException;
12 use \InvalidArgumentException;
15 * An general object factory
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class ObjectFactory extends BaseFactory {
38 * Protected constructor
40 * @param $className Name of this class
43 protected function __construct (string $className = __CLASS__) {
44 // Call parent constructor
45 parent::__construct($className);
49 * Creates a new object given by the name or throws an exception if
50 * the class was not found. No parameters for the object are currently
53 * @param $fullClassName Name of the class we shall construct
54 * @param $args Arguments in an indexed array
55 * @return $objectInstance An instance of the requested object
56 * @throws NoClassException If the requested class was not found
57 * @throws InvalidArgumentException If className is empty or the name not following naming-convention
59 public static final function createObjectByName (string $fullClassName, array $args = []) {
60 // Is the class name valid and is the class there?
61 if (empty($fullClassName)) {
62 // Throw an exception here
63 throw new InvalidArgumentException('Parameter "fullClassName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
64 } elseif (!self::isClassCounted($fullClassName) && !class_exists($fullClassName)) {
65 // First get an instance of this factory
66 $factoryInstance = new ObjectFactory();
68 // Then throw an exception
69 throw new NoClassException([$factoryInstance, $fullClassName], FrameworkInterface::EXCEPTION_CLASS_NOT_FOUND);
72 // Split class name on backslash to check naming-convention
73 $classNameParts = explode("\\", $fullClassName);
75 // Okay, does it follow naming-convention?
76 if (count($classNameParts) < 4) {
77 // Namespaces are missing
78 throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $fullClassName));
82 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName));
84 // Run the user function
85 $objectInstance = call_user_func_array([$fullClassName, $methodName], $args);
88 self::countObject($fullClassName);
90 // Return the prepared instance
91 return $objectInstance;
95 * Creates an object by it's configured name
97 * @param $configKey Configuration key to read
98 * @param $args Arguments in an indexed array
99 * @return $objectInstance An instance of the requested object
101 public static final function createObjectByConfiguredName (string $configKey, array $args = []) {
102 // Read the configuration entry
103 $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey);
105 // Send this to the other factory...
106 $objectInstance = self::createObjectByName($fullClassName, $args);
108 // Return the instance
109 return $objectInstance;
113 * Strips of namespace from given class name. An exception is thrown when
114 * there is no namespace found as this is the now the norm for any objects
115 * being created with this object factory.
117 * @param $fullClassName Class name with namespace
118 * @return $shortClassName Stripped class name (no namespace)
120 private static function stripNamespaceFromClassName (string $fullClassName) {
121 // The class name should contain at least 2 back-slashes, so split at them
122 $classNameParts = explode("\\", $fullClassName);
125 $shortClassName = array_pop($classNameParts);
128 return $shortClassName;