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