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