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