source: sandbox/workflow/flumen/inc/local/lib/ProcessFactory.php @ 1293

Revision 1293, 3.9 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 WF_LIB . SEP . 'BaseFactory.php';
13
14/**
15 * Implementa o design pattern factory para os processos Workflow
16 * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
17 * @author Carlos Eduardo Nogueira Gonçalves
18 * @version 1.0
19 * @package Workflow
20 * @license http://www.gnu.org/copyleft/gpl.html GPL
21 */
22class ProcessFactory extends BaseFactory {
23
24        /**
25         * @var array $_cache Array de cache dos objetos gerados
26         * @access private
27         */
28        private $_cache;
29
30        /**
31         * @var string $_classPath Armazena o caminho para onde estão as classes a serem instanciadas
32         * @access private
33         */
34        private $_classPath;
35
36        /**
37         * @var string $_preffix Armazena o prefixo do nome dos arquivos das classes
38         * @access private
39         */
40        private $_preffix;
41
42        /**
43         * @var string $_suffix Armazena o sufixo do nome dos arquivos das classes
44         * @access private
45         */
46        private $_suffix;
47
48        /**
49         * Construtora da class ProcessFactory
50         * @return object
51         * @access public
52         */
53        public function ProcessFactory(){
54                $this->_cache           = array();
55                $this->_classPath       = PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'inc' . SEP . 'local' . SEP . 'classes';
56                $this->_preffix         = 'class.';
57                $this->_suffix          = '.php';
58        }
59
60        /**
61         * Importa os arquivos da classe
62         * @param string $input Nome da classe
63         * @access private
64         * @return void
65         */
66        protected function _import($input) {
67
68                $file = $this->_classPath . '/' . $this->_preffix . $input . $this->_suffix;
69
70                if (@file_exists($file)){
71                        require_once $file;
72                } else {
73                        trigger_error("O arquivo " . $file . " não existe.", E_USER_ERROR);
74                }
75
76        }
77
78        /**
79         * Armazena referências aos objetos recém criados
80         * @param string $key O nome da classe
81         * @param object &$item A instância da classe
82         * @return boolean True se for salvo. False caso contrário, ou $item não é um objeto
83         * @access private
84         */
85        protected function _setEntry($key, &$item) {
86                if(!is_object($item))
87                        return false;
88
89                $this->_cache[] =& $item;
90                return true;
91        }
92
93        /**
94         * Retorna a referência ao objeto armazenado
95         * @param string $key Nome da classe
96         * @return object
97         * @access private
98         */
99        protected function &_getEntry($key) {
100                return $this->_cache[$key];
101        }
102
103        /**
104         * Verifica se uma determinada classe possui objeto armazenado no cache da Factory
105         * @param string $key Nome da classe
106         * @return boolean
107         * @access protected
108         */
109        protected function _isEntry($key) {
110                return ($this->_getEntry($key) !== null);
111        }
112
113        /**
114         * Retorna objetos armazenados ou cria novos objetos utilizando o pattern singleton
115         * @param string $className Nome da classe
116         * @return object
117         * @access public
118         */
119        public function &getInstance() {
120                if (func_num_args() > 0) {
121                        $args = func_get_args();
122                        if ($this->_isEntry($args[0])) {
123                                return($this->_getEntry($args[0]));
124                        } else {
125                                $instance = call_user_func_array(array(&$this, 'newInstance'), $args);
126                                $this->_setEntry($args[0], &$instance);
127                                return $instance;
128                        }
129                }
130        }
131
132        /**
133         * Cria novos objetos
134         * @param string $className Nome da classe
135         * @return object
136         * @access public
137         */
138        public function &newInstance() {
139                if (func_num_args() > 0) {
140
141                        $args = func_get_args();
142                        $className = array_shift($args);
143
144                        $this->_import($className);
145
146                        $reflectionObj = new ReflectionClass($className);
147                        return $reflectionObj->newInstanceArgs($args);
148                }
149        }
150}
151?>
Note: See TracBrowser for help on using the repository browser.