Continued:
[core.git] / framework / main / classes / factories / objects / class_ObjectFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory\Object;
4
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;
10
11 // Import SPL stuff
12 use \InvalidArgumentException;
13
14 /**
15  * An general object factory
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
36 class ObjectFactory extends BaseFactory {
37         /**
38          * Protected constructor
39          *
40          * @param       $className      Name of this class
41          * @return      void
42          */
43         protected function __construct (string $className = __CLASS__) {
44                 // Call parent constructor
45                 parent::__construct($className);
46         }
47
48         /**
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
51          * supported.
52          *
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
58          */
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();
67
68                         // Then throw an exception
69                         throw new NoClassException([$factoryInstance, $fullClassName], FrameworkInterface::EXCEPTION_CLASS_NOT_FOUND);
70                 }
71
72                 // Split class name on backslash to check naming-convention
73                 $classNameParts = explode("\\", $fullClassName);
74
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));
79                 }
80
81                 // Create method name
82                 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName));
83
84                 // Run the user function
85                 $objectInstance = call_user_func_array([$fullClassName, $methodName], $args);
86
87                 // Count this one up
88                 self::countObject($fullClassName);
89
90                 // Return the prepared instance
91                 return $objectInstance;
92         }
93
94         /**
95          * Creates an object by it's configured name
96          *
97          * @param       $configKey              Configuration key to read
98          * @param       $args                           Arguments in an indexed array
99          * @return      $objectInstance         An instance of the requested object
100          */
101         public static final function createObjectByConfiguredName (string $configKey, array $args = []) {
102                 // Read the configuration entry
103                 $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey);
104
105                 // Send this to the other factory...
106                 $objectInstance = self::createObjectByName($fullClassName, $args);
107
108                 // Return the instance
109                 return $objectInstance;
110         }
111
112         /**
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.
116          *
117          * @param       $fullClassName  Class name with namespace
118          * @return      $shortClassName Stripped class name (no namespace)
119          */
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);
123
124                 // Get last element
125                 $shortClassName = array_pop($classNameParts);
126
127                 // Return it
128                 return $shortClassName;
129         }
130
131 }