Continued:
[core.git] / framework / main / classes / factories / objects / class_ObjectFactory.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Factory;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Loader\NoClassException;
8
9 // Import SPL stuff
10 use \InvalidArgumentException;
11
12 /**
13  * An general object factory
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
18  * @license             GNU GPL 3.0 or any newer version
19  * @link                http://www.shipsimu.org
20  *
21  * This program is free software: you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation, either version 3 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program. If not, see <http://www.gnu.org/licenses/>.
33  */
34 class ObjectFactory extends BaseFactory {
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of this class
39          * @return      void
40          */
41         protected function __construct ($className = __CLASS__) {
42                 // Call parent constructor
43                 parent::__construct($className);
44         }
45
46         /**
47          * Creates a new object given by the name or throws an exception if
48          * the class was not found. No parameters for the object are currently
49          * supported.
50          *
51          * @param       $className                      Name of the class we shall construct
52          * @param       $args                           Arguments in an indexed array
53          * @return      $objectInstance         An instance of the requested object
54          * @throws      NoClassException        If the requested class was not found
55          * @throws      InvalidArgumentException        If className is empty or the name not following naming-convention
56          */
57         public static final function createObjectByName ($className, array $args = array()) {
58                 // Is the class name valid and is the class there?
59                 if (empty($className)) {
60                         // Throw an exception here
61                         throw new InvalidArgumentException('Parameter "className" is empty');
62                 } elseif (!class_exists($className)) {
63                         // First get an instance of this factory
64                         $factoryInstance = new ObjectFactory();
65
66                         // Then throw an exception
67                         throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
68                 }
69
70                 // Split class name on backslash to check naming-convention
71                 $classNameParts = explode("\\", $className);
72
73                 // Okay, does it follow naming-convention?
74                 if (count($classNameParts) < 4) {
75                         // Namespaces are missing
76                         throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $className));
77                 } // END - if
78
79                 // Create method name
80                 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
81
82                 // Run the user function
83                 $objectInstance = call_user_func_array(array($className, $methodName), $args);
84
85                 // Count this one up
86                 self::countObject($className);
87
88                 // Return the prepared instance
89                 return $objectInstance;
90         }
91
92         /**
93          * Creates an object by it's configured name
94          *
95          * @param       $configEnttry           Configuration entry to read
96          * @param       $args                           Arguments in an indexed array
97          * @return      $objectInstance         An instance of the requested object
98          */
99         public static final function createObjectByConfiguredName ($configEntry, array $args = array()) {
100                 // Read the configuration entry
101                 $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
102
103                 // Send this to the other factory...
104                 $objectInstance = self::createObjectByName($className, $args);
105
106                 // Return the instance
107                 return $objectInstance;
108         }
109
110         /**
111          * Strips of namespace from given class name. An exception is thrown when
112          * there is no namespace found as this is the now the norm for any objects
113          * being created with this object factory.
114          *
115          * @param       $fullClassName  Class name with namespace
116          * @return      $shortClassName Stripped class name (no namespace)
117          * @throws      InvalidArgumentException        If the class name does not follow naming convention
118          */
119         private static function stripNamespaceFromClassName ($fullClassName) {
120                 // The class name should contain at least 2 back-slashes, so split at them
121                 $classNameParts = explode("\\", $fullClassName);
122
123                 // At least 4 parts should be there
124                 if (count($classNameParts) < 4) {
125                         // Namespace scheme is: Tld\Project\Package[\SubPackage...]
126                         throw new InvalidArgumentException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
127                 } // END - if
128
129                 // Get last element
130                 $shortClassName = array_pop($classNameParts);
131
132                 // Return it
133                 return $shortClassName;
134         }
135
136 }