source: sandbox/workflow/branches/609/lib/factory/BaseFactory.php @ 2233

Revision 2233, 4.3 KB checked in by pedroerp, 14 years ago (diff)

Ticket #609 - Migração das classes do módulo workflow para a nova factory.

  • Property svn:executable set to *
Line 
1<?php
2/**************************************************************************\
3* eGroupWare                                                               *
4* http://www.egroupware.org                                                *
5* --------------------------------------------                             *
6*  This program is free software; you can redistribute it and/or modify it *
7*  under the terms of the GNU General Public License as published by the   *
8*  Free Software Foundation; either version 2 of the License, or (at your  *
9*  option) any later version.                                              *
10\**************************************************************************/
11
12/**
13 * Base Factory abstract class.
14 * This class implements all the common behaviour of
15 * it's specialized classes (ProcessFactory and WorkflowFactory).
16 *
17 * @package Factory
18 * @license http://www.gnu.org/copyleft/gpl.html GPL
19 * @author Pedro Eugênio Rocha - pedro.eugenio.rocha@gmail.com
20 */
21abstract class BaseFactory {
22
23        /**
24         * @var array $_fileInfo Store what classes we can instantiate.
25         * @access private
26         */
27        private $_fileInfo;
28
29
30        /**
31         * Registers classes into '$_fileInfo' private
32         * atribute.
33         *
34         * @todo Maybe here we should receive more parameters...
35         * @access protected
36         * @return void
37         */
38        protected final function registerFileInfo($className, $fileName, $relativePath, $basePath = WF_SERVER_ROOT) {
39
40                /* default is to override */
41                $this->_fileInfo[$className] = array(   'filename' => $fileName,
42                                                                                                'path' => $relativePath,
43                                                                                                'basePath' => $basePath,
44                                                                                                'instance' => null);
45        }
46
47
48        /**
49         * Here we should do all the factory stuff.
50         * The classes instatiated here will be stored
51         * into our private object cache. If there is no
52         * object of the given type into the cache, 'newInstance'
53         * will be called.
54         *
55         * @access public
56         * @return object
57         */
58        public function &getInstance(){
59
60                /* at least the class name */
61                if (func_num_args() <= 0)
62                        return null;
63
64                $args = func_get_args();
65
66                /* recovering class data */
67                if ($entry = $this->_getEntry($args[0])) {
68
69                        if (is_null($entry['instance'])) {
70
71                                /* must instantiate it */
72                if (($obj = call_user_func_array(array($this, "newInstance"), $args)) == null)
73                                        throw new Exception("Unable to instantiate '".$args[0]."'");
74
75                                /* saving the object reference */
76                                $this->_setEntryInstance($args[0], $obj);
77                                return $obj;
78                        }
79                        return $entry['instance'];
80
81                }
82                /* class not allowed */
83                else
84                        throw new Exception("You are not allowed to instantiate class '".$args[0]."'.");
85        }
86
87        /**
88         * Instantiate classes.
89         *
90         * @todo I think that we don't need to use the reflection
91         *               class here. Future work...
92         * @access public
93         * @return object
94         */
95        public function &newInstance(){
96
97                /* at least the class name */
98                if (func_num_args() <= 0)
99                        return null;
100
101                $args = func_get_args();
102
103                $className = array_shift($args);
104                if(!$this->_import($className))
105                        return null;
106
107                /* I dont know.. maybe we could do better here.. */
108                $reflectionObj = new ReflectionClass($className);
109
110                if (count($args) == 0)
111                        return $reflectionObj->newInstance();
112                return $reflectionObj->newInstanceArgs($args);
113        }
114
115
116        /**
117         * Private stuff. Handles the cache and information
118         * '$_fileInfo' array.
119         *
120         * @access private
121         * @return array
122         */
123        private function _getEntry($className){
124                return $this->_fileInfo[$className];
125        }
126
127
128        /**
129         * Stores the given object into the internal cache
130         * for upcoming requests.
131         *
132         * @access private
133         * @return boolean
134         */
135        private function _setEntryInstance($className, $obj){
136
137                if (is_array($this->_fileInfo[$className])) {
138                        $this->_fileInfo[$className]['instance'] = $obj;
139                        return true;
140                }
141                return false;
142        }
143
144
145        /**
146         * Including (requiring_once ;P ) the file(s) itself. Ideally,
147         * it could be the only place to include files.
148         *
149         * @access private
150         * @return boolean
151         */
152        private function _import($className){
153
154                /* not found */
155                if (!($entry = $this->_getEntry($className)))
156                        throw new Exception('You are not allowed to instantiate \''.$className.'\' class.');
157
158                $fullPath = $entry['basePath'] . '/' . $entry['path'] . '/' . $entry['filename'];
159
160                /* file not found */
161                if (!file_exists($fullPath))
162                        throw new Exception("File '".$fullPath."' not found.");
163
164                /* including file */
165                require_once $fullPath;
166                return true;
167        }
168}
169?>
Note: See TracBrowser for help on using the repository browser.