source: sandbox/workflow/flumen/lib/Factory.php @ 1293

Revision 1293, 6.2 KB checked in by gbisotto, 15 years ago (diff)

Ticket #609 - Modificada a classe Factory e ProcessFactory? para automatizaro acesso ao objetos

  • 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
12require_once 'BaseFactory.php';
13
14/**
15 * Implementa o design pattern factory para o módulo Workflow
16 * @author Carlos Eduardo Nogueira Gonçalves
17 * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
18 * @version 1.1
19 * @package Workflow
20 * @license http://www.gnu.org/copyleft/gpl.html GPL
21 */
22class Factory extends BaseFactory {
23
24        /**
25         * Importa os arquivos da classe
26         * @param string $input Nome da classe
27         * @access private
28         * @return void
29         */
30        protected function _import($input) {
31
32                $config = array(
33                        'classpath'     => WF_INC,
34                        'preffix'       => 'class.',
35                        'suffix'        => '.inc.php'
36                        );
37
38                $file = $config['classpath'] . '/' . $config['preffix'] . $input . $config['suffix'];
39
40                if (@file_exists($file)){
41                        require_once $file;
42                } else {
43                        trigger_error("O arquivo " . $file . " não existe.", E_USER_ERROR);
44                }
45        }
46
47        /**
48         * Armazena referências aos objetos recém criados
49         * @param string $key O nome da classe
50         * @param object &$item A instância da classe
51         * @return boolean True se for salvo. False caso contrário, ou $item não é um objeto
52         * @access private
53         */
54        protected function _setEntry($key, &$item) {
55                if(!is_object($item))
56                        return false;
57
58                if(!is_array($GLOBALS['workflow'])){
59                        $GLOBALS['workflow'] = array();
60                }
61
62                $GLOBALS['workflow'][$key] =& $item;
63                return true;
64        }
65
66        /**
67         * Retorna a referência ao objeto armazenado
68         * @param string $key Nome da classe
69         * @return object
70         * @access private
71         */
72        protected function &_getEntry($key) {
73                return $GLOBALS['workflow'][$key];
74        }
75
76        /**
77         * Verifica se uma determinada classe possui objeto armazenado no cache da Factory
78         * @param string $key Nome da classe
79         * @return boolean
80         * @access protected
81         */
82        protected function _isEntry($key) {
83                return (self::_getEntry($key) !== null);
84        }
85
86        /**
87         * Importa os arquivos da classe externa
88         * @param string $input Nome da classe
89         * @access private
90         * @return void
91         */
92        private function _importForeign($packageName, $className){
93
94                $config = array(
95                                'classpath' => dirname(__FILE__)."/../../{$packageName}/inc",
96                                'preffix'       => 'class.',
97                                'suffix'        => '.inc.php'
98                        );
99
100                $file = $config['classpath'] . '/' . $config['preffix'] . $className . $config['suffix'];
101
102                if (@file_exists($file)){
103                        require_once $file;
104                } else {
105                        trigger_error("O arquivo " . $file . " não existe.", E_USER_ERROR);
106                }
107        }
108
109        /**
110         * Salva um objeto externo
111         * @params string $key Nome da classe
112         * @params object &$item Instancia da classe
113         * @return boolean True caso seja salvo. False caso contrário ou $item não é um objeto
114         * @access private
115         */
116        private function _setExtEntry($key, &$item) {
117                if(!is_object($item))
118                        return false;
119
120                if(is_object($GLOBALS[$key])){
121                        return (get_class($GLOBALS[$key]) == get_class($item));
122                } else {
123                        $GLOBALS[$key] =& $item;
124                        return true;
125                }
126        }
127
128        /**
129         * Retorna a referência ao objeto armazenado
130         * @param string $key Nome da classe
131         * @return object
132         * @access private
133         */
134        private function &_getExtEntry($key) {
135                return $GLOBALS[$key];
136        }
137
138        /**
139         * Verifica se uma determinada classe possui objeto armazenado no cache da Factory
140         * @param string $key Nome da classe
141         * @return boolean
142         * @access private
143         */
144        private function _isExtEntry($key) {
145                return (self::_getExtEntry($key) !== null);
146        }
147
148        /**
149         * Retorna objetos armazenados ou cria novos objetos utilizando o pattern singleton
150         * @param string $className Nome da classe
151         * @return object
152         * @access public
153         */
154        public function &getInstance() {
155                if (func_num_args() > 0) {
156                        $args = func_get_args();
157                        if (self::_isEntry($args[0])) {
158                                return(self::_getEntry($args[0]));
159                        } else {
160                                $instance = call_user_func_array(array('Factory', 'newInstance'), $args);
161                                self::_setEntry($args[0], &$instance);
162                                return $instance;
163                        }
164                }
165        }
166
167        /**
168         * Cria os objetos sem importar o arquivo que contem a classe
169         * @params string $className Nome da classe
170         * @return object
171         * @access private
172         */
173        private function &_newInstance(){
174                if (func_num_args() > 0){
175                        $args = func_get_args();
176                        $className = array_shift($args);
177                        $reflectionObj = new ReflectionClass($className);
178                        return $reflectionObj->newInstanceArgs($args);
179                }
180        }
181
182        /**
183         * Cria novos objetos
184         * @param string $className Nome da classe
185         * @return object
186         * @access public
187         */
188        public function &newInstance() {
189                if (func_num_args() > 0) {
190                        $args = func_get_args();
191                        $className = $args[0];
192                        self::_import($className);
193                        return call_user_func_array(array('Factory', '_newInstance'), $args);
194                }
195        }
196
197        /**
198         * Retorna objetos armazenados ou cria novos objetos utilizando o pattern singleton (somente para módulos externos)
199         * @param string $moduleName O nome do módulo
200         * @param string $className Nome da classe
201         * @return object
202         * @access public
203         */
204        public function &getForeignInstance() {
205                if (func_num_args() > 1) {
206                        $args = func_get_args();
207
208                        if(self::_isExtEntry($args[1])){
209                                return self::_getExtEntry($args[1]);
210                        } else {
211
212                                $packageName = array_shift($args);
213                                self::_importForeign($packageName, $args[0]);
214
215                                $instance = call_user_func_array(array('Factory', '_newInstance'), $args);
216                                self::_setExtEntry($args[0], &$instance);
217                                return $instance;
218                        }
219                }
220        }
221
222        /**
223         * Cria novos objetos (somente para módulos externos)
224         * @param string $moduleName O nome do módulo
225         * @param string $className Nome da classe
226         * @return object
227         * @access public
228         */
229        public function &newForeignInstance() {
230                if (func_num_args() > 1) {
231                        $args = func_get_args();
232
233                        $packageName = array_shift($args);
234                        self::_importForeign($packageName, $args[0]);
235                        return call_user_func_array(array('Factory', '_newInstance'), $args);
236                }
237        }
238}
239?>
Note: See TracBrowser for help on using the repository browser.