wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.ObjectFactory.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.ObjectFactory.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/util/class.Message.php");
20 require_once(BASE."wcmf/lib/util/class.InifileParser.php");
21 
22 /**
23  * @class ObjectFactory
24  * @ingroup Util
25  * @brief ObjectFactory loads class definitions and instantiates classes.
26  *
27  * @author ingo herwig <ingo@wemove.com>
28  */
30 {
31  var $_errorMsg = '';
32 
33  /**
34  * Get last error message.
35  * @return The error string
36  */
37  function getErrorMsg()
38  {
39  return $this->_errorMsg;
40  }
41  /**
42  * Returns an instance of the class.
43  * @return A reference to the only instance of the Singleton object
44  */
45  function &getInstance()
46  {
47  static $instance = null;
48 
49  if (!isset($instance))
50  $instance = new ObjectFactory();
51 
52  return $instance;
53  }
54  /**
55  * Get the class file for a given class name.
56  * This method looks up the class definition from 'classmapping' section.
57  * @param className The name of the class.
58  * @return Classfile on success, False else / error string provided by getErrorMsg()
59  */
60  function getClassfileFromConfig($className)
61  {
62  // find class file
63  $parser = &InifileParser::getInstance();
64  if (($classFile = $parser->getValue($className, 'classmapping', false)) === false)
65  {
66  $this->_errorMsg = $parser->getErrorMsg();
67  return false;
68  }
69  return $classFile;
70  }
71  /**
72  * Load a class definition from a configuration entry.
73  * This method looks up the class name as value of $classEntry in $section of the main
74  * configuration file and takes class definition from 'classmapping' section.
75  * With this information it includes the desired class definition.
76  * @param section The name of the section, where the class is defined.
77  * @param classEntry The name of the key in the section, where the class is defined.
78  * @return Classname on success, False else / error string provided by getErrorMsg()
79  */
80  function loadClassDefinitionFromConfig($section, $classEntry)
81  {
82  // find class name
83  $parser = &InifileParser::getInstance();
84  if (($className = $parser->getValue($classEntry, $section)) === false)
85  {
86  $this->_errorMsg = $parser->getErrorMsg();
87  return false;
88  }
89  // find class file
90  if (($classFile = $this->getClassfileFromConfig($className)) === false)
91  return false;
92 
93  // include class definition
94  if (file_exists(BASE.$classFile))
95  {
96  require_once(BASE.$classFile);
97  return $className;
98  }
99  else
100  {
101  $this->_errorMsg = "Classfile ".$classFile." not found.";
102  return false;
103  }
104  }
105  /**
106  * Create an object from a configuration entry.
107  * This method looks up the class name as value of $classEntry in $section of the main
108  * configuration file and takes - if given - constructor parameters from the 'initparams'
109  * section and class definition from 'classmapping' section. With this information it
110  * constructs the desired object.
111  * @param section The name of the section, where the class is defined.
112  * @param classEntry The name of the key in the section, where the class is defined.
113  * @return A reference to an instance of the class, null if failed (error string provided by getErrorMsg()).
114  */
115  function &createInstanceFromConfig($section, $classEntry)
116  {
117  $obj = null;
118 
119  // load class definition
120  if (($className = $this->loadClassDefinitionFromConfig($section, $classEntry)) !== false)
121  {
122  // find init parameters
123  $initParams = null;
124  $parser = &InifileParser::getInstance();
125  if (($initSection = $parser->getValue($className, 'initparams')) !== false)
126  if (($initParams = $parser->getSection($initSection)) === false)
127  $initParams = null;
128 
129  if ($initParams != null)
130  $obj = new $className($initParams);
131  else if (class_exists($className))
132  $obj = new $className;
133  else
134  $this->_errorMsg = "Class ".$className." is not found defined.";
135  }
136  return $obj;
137  }
138 }
139 ?>
getClassfileFromConfig($className)
loadClassDefinitionFromConfig($section, $classEntry)
ObjectFactory loads class definitions and instantiates classes.
& createInstanceFromConfig($section, $classEntry)