* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ class ObjectFactory extends BaseFactory { /** * Protected constructor * * @param $className Name of this class * @return void */ protected function __construct (string $className = __CLASS__) { // Call parent constructor parent::__construct($className); } /** * Creates a new object given by the name or throws an exception if * the class was not found. No parameters for the object are currently * supported. * * @param $fullClassName Name of the class we shall construct * @param $args Arguments in an indexed array * @return $objectInstance An instance of the requested object * @throws NoClassException If the requested class was not found * @throws InvalidArgumentException If className is empty or the name not following naming-convention */ public static final function createObjectByName (string $fullClassName, array $args = []) { // Is the class name valid and is the class there? if (empty($fullClassName)) { // Throw an exception here throw new InvalidArgumentException('Parameter "fullClassName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (!self::isClassCounted($fullClassName) && !class_exists($fullClassName)) { // First get an instance of this factory $factoryInstance = new ObjectFactory(); // Then throw an exception throw new NoClassException([$factoryInstance, $fullClassName], FrameworkInterface::EXCEPTION_CLASS_NOT_FOUND); } // Split class name on backslash to check naming-convention $classNameParts = explode("\\", $fullClassName); // Okay, does it follow naming-convention? if (count($classNameParts) < 4) { // Namespaces are missing throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $fullClassName)); } // Create method name $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName)); // Run the user function $objectInstance = call_user_func_array([$fullClassName, $methodName], $args); // Count this one up self::countObject($fullClassName); // Return the prepared instance return $objectInstance; } /** * Creates an object by it's configured name * * @param $configKey Configuration key to read * @param $args Arguments in an indexed array * @return $objectInstance An instance of the requested object */ public static final function createObjectByConfiguredName (string $configKey, array $args = []) { // Read the configuration entry $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey); // Send this to the other factory... $objectInstance = self::createObjectByName($fullClassName, $args); // Return the instance return $objectInstance; } /** * Strips of namespace from given class name. An exception is thrown when * there is no namespace found as this is the now the norm for any objects * being created with this object factory. * * @param $fullClassName Class name with namespace * @return $shortClassName Stripped class name (no namespace) */ private static function stripNamespaceFromClassName (string $fullClassName) { // The class name should contain at least 2 back-slashes, so split at them $classNameParts = explode("\\", $fullClassName); // Get last element $shortClassName = array_pop($classNameParts); // Return it return $shortClassName; } }