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