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