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 - 2017 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                 // First get an instance of this factory
59                 $factoryInstance = new ObjectFactory();
60
61                 // Split class name on backslash to check naming-convention
62                 $classNameParts = explode("\\", $className);
63
64                 // Is the class name valid and is the class there?
65                 if (empty($className)) {
66                         // Throw an exception here
67                         throw new InvalidArgumentException('Parameter "className" is empty');
68                 } elseif (!class_exists($className)) {
69                         // Then throw an exception
70                         throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
71                 } elseif (count($classNameParts) < 3) {
72                         // Namespaces are missing
73                         throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
74                 }
75
76                 // Create method name
77                 $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
78
79                 // Run the user function
80                 $objectInstance = call_user_func_array(array($className, $methodName), $args);
81
82                 // Count this one up
83                 self::countObject($className);
84
85                 // Return the prepared instance
86                 return $objectInstance;
87         }
88
89         /**
90          * Creates an object by it's configured name
91          *
92          * @param       $configEnttry           Configuration entry to read
93          * @param       $args                           Arguments in an indexed array
94          * @return      $objectInstance         An instance of the requested object
95          */
96         public static final function createObjectByConfiguredName ($configEntry, array $args = array()) {
97                 // Read the configuration entry
98                 $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
99
100                 // Send this to the other factory...
101                 $objectInstance = self::createObjectByName($className, $args);
102
103                 // Return the instance
104                 return $objectInstance;
105         }
106
107         /**
108          * Strips of namespace from given class name. An exception is thrown when
109          * there is no namespace found as this is the now the norm for any objects
110          * being created with this object factory.
111          *
112          * @param       $fullClassName  Class name with namespace
113          * @return      $shortClassName Stripped class name (no namespace)
114          * @throws      InvalidClassNameException       If the class name does not follow naming convention
115          */
116         private static function stripNamespaceFromClassName ($fullClassName) {
117                 // The class name should contain at least 2 back-slashes, so split at them
118                 $classNameParts = explode("\\", $fullClassName);
119
120                 // At least 3 parts should be there
121                 if (count($classNameParts) < 3) {
122                         // Namespace scheme is: Project\Package[\SubPackage...]
123                         throw new InvalidClassNameException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
124                 } // END - if
125
126                 // Get last element
127                 $shortClassName = array_pop($classNameParts);
128
129                 // Return it
130                 return $shortClassName;
131         }
132
133 }