1 | <?php |
---|
2 | /** |
---|
3 | * |
---|
4 | * Copyright (C) 2011 Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify it under |
---|
7 | * the terms of the GNU Affero General Public License version 3 as published by |
---|
8 | * the Free Software Foundation with the addition of the following permission |
---|
9 | * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED |
---|
10 | * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE |
---|
11 | * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
---|
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
15 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
16 | * details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Affero General Public License |
---|
19 | * along with this program; if not, see www.gnu.org/licenses or write to |
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
---|
21 | * MA 02110-1301 USA. |
---|
22 | * |
---|
23 | * This code is based on the OpenXchange Connector and on the Prognus pSync |
---|
24 | * Connector both developed by the community and licensed under the GPL |
---|
25 | * version 2 or above as published by the Free Software Foundation. |
---|
26 | * |
---|
27 | * You can contact Prognus Software Livre headquarters at Av. Tancredo Neves, |
---|
28 | * 6731, PTI, Bl. 05, Esp. 02, Sl. 10, Foz do Iguaçu - PR - Brasil or at |
---|
29 | * e-mail address prognus@prognus.com.br. |
---|
30 | * |
---|
31 | * |
---|
32 | * Faz a localização dos serviços que serão utilizados pela aplicação e que estão localizados na API |
---|
33 | * |
---|
34 | * |
---|
35 | * @package expressoMail |
---|
36 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
37 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
38 | * @since Arquivo disponibilizado na versão 2.2 |
---|
39 | */ |
---|
40 | |
---|
41 | //Helper function and defines that must be moved to a common area |
---|
42 | ///////////////////////////////////////////////////// |
---|
43 | if(!function_exists('define_once') ) |
---|
44 | { |
---|
45 | function define_once( $constant, $value ) |
---|
46 | { |
---|
47 | if( !defined( $constant ) ) |
---|
48 | { |
---|
49 | define( $constant, $value ); |
---|
50 | } |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | define_once( 'ROOT', $_SESSION['rootPath'].'/' ); |
---|
55 | |
---|
56 | define_once( 'SERVICES', ROOT.'/services/' ); |
---|
57 | |
---|
58 | define_once( 'LIBRARY', ROOT.'/library/' ); |
---|
59 | |
---|
60 | define_once( 'API', ROOT.'/API/' ); |
---|
61 | |
---|
62 | if( !class_exists('ServiceLocator') ){ |
---|
63 | |
---|
64 | ///////////////////////////////////////////////////// |
---|
65 | /** |
---|
66 | * Faz a localização dos serviços que serão utilizados pela aplicação e que estão localizados na API |
---|
67 | * |
---|
68 | * @package expressoMail |
---|
69 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
70 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
71 | * @version 1.0 |
---|
72 | */ |
---|
73 | class ServiceLocator |
---|
74 | { |
---|
75 | static $locators = array(); |
---|
76 | |
---|
77 | static $cache = array(); |
---|
78 | |
---|
79 | //$service = null; |
---|
80 | |
---|
81 | //$serviceName = null; |
---|
82 | |
---|
83 | // static $empty_locator = new ServiceLocator( 'empty' ); |
---|
84 | |
---|
85 | |
---|
86 | /** |
---|
87 | * Carrega a configuração |
---|
88 | * |
---|
89 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
90 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
91 | * @param $configuration |
---|
92 | */ |
---|
93 | static function load( $configuration ) |
---|
94 | { |
---|
95 | $configuration = SERVICES.$configuration; |
---|
96 | |
---|
97 | if( !file_exists ( $configuration ) ) |
---|
98 | return( false ); |
---|
99 | |
---|
100 | $configuration = parse_ini_file( $configuration ); |
---|
101 | |
---|
102 | foreach( $configuration as $serviceType => $serviceName ) |
---|
103 | { |
---|
104 | self::deploy( $serviceType, $serviceName ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | * Deploy |
---|
111 | * |
---|
112 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
113 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
114 | * @param $serviceType |
---|
115 | * @param $serviceName |
---|
116 | */ |
---|
117 | static function deploy( $serviceType, $serviceName = null ) |
---|
118 | { |
---|
119 | require_once( SERVICES."class.".$serviceType.".php" ); |
---|
120 | |
---|
121 | if( $serviceName ) |
---|
122 | { |
---|
123 | self::register( $serviceType, new $serviceName() ); |
---|
124 | } |
---|
125 | |
---|
126 | return( self::$locators[ $serviceType ] ); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * make all the treatment of |
---|
132 | * |
---|
133 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
134 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
135 | * @param $service |
---|
136 | * @param $object |
---|
137 | */ |
---|
138 | static function register( $service, $object ) |
---|
139 | { |
---|
140 | self::$locators[ $service ] = new ServiceLocator( $service, $object ); |
---|
141 | |
---|
142 | self::configure( $service ); |
---|
143 | |
---|
144 | return( self::$locators[ $service ] ); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | * unregister service |
---|
150 | * |
---|
151 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
152 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
153 | * @param $service |
---|
154 | */ |
---|
155 | static function unregister( $service ) |
---|
156 | { |
---|
157 | $old = self::$locators[ $service ]; |
---|
158 | |
---|
159 | unset( self::$locators[ $service ] ); |
---|
160 | |
---|
161 | return( $old ); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * configure |
---|
166 | * |
---|
167 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
168 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
169 | * @param $service |
---|
170 | */ |
---|
171 | static function configure( $service ) |
---|
172 | { |
---|
173 | return( null ); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * locate |
---|
178 | * |
---|
179 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
180 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
181 | * @param $service |
---|
182 | * @param $arguments |
---|
183 | * @param $target |
---|
184 | */ |
---|
185 | static function locate( $service, $arguments = array(), $target = false ) |
---|
186 | { |
---|
187 | if( !$target ) |
---|
188 | { |
---|
189 | list( $target, $service ) = explode( ".", $service ); |
---|
190 | } |
---|
191 | |
---|
192 | if( !is_array( $arguments ) ) |
---|
193 | { |
---|
194 | $arguments = array( $arguments ); |
---|
195 | } |
---|
196 | |
---|
197 | $locator = self::$locators[ $target ]; |
---|
198 | |
---|
199 | if( !$locator ) |
---|
200 | { |
---|
201 | $locator = self::deploy( $target ); |
---|
202 | } |
---|
203 | if( !$locator ) |
---|
204 | { |
---|
205 | return( false ); |
---|
206 | } |
---|
207 | |
---|
208 | try |
---|
209 | { |
---|
210 | return $locator->proxy( $service, $arguments ); |
---|
211 | } |
---|
212 | catch( Exception $e ) |
---|
213 | { |
---|
214 | //Implement the exception stack to the fallback handlers treat correctly |
---|
215 | //by now - fly it |
---|
216 | } |
---|
217 | |
---|
218 | return( null ); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | /** |
---|
223 | * construct |
---|
224 | * |
---|
225 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
226 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
227 | * @param $serviceName |
---|
228 | * @param $object |
---|
229 | */ |
---|
230 | function __construct( $serviceName, $object ) |
---|
231 | { |
---|
232 | $this->service = $object; |
---|
233 | $this->serviceName = $serviceName; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | /** |
---|
238 | * proxy |
---|
239 | * |
---|
240 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
241 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
242 | * @param $method |
---|
243 | * @param $arguments |
---|
244 | */ |
---|
245 | function proxy( $method, $arguments ) |
---|
246 | { |
---|
247 | //handle here cases of miss of methods |
---|
248 | if( !method_exists( $this->service, $method ) ) return( false ); |
---|
249 | |
---|
250 | $many = count( $arguments ); |
---|
251 | |
---|
252 | $proxy = self::$cache[$many]; |
---|
253 | |
---|
254 | if( !$proxy ) |
---|
255 | { |
---|
256 | $params = array(); |
---|
257 | |
---|
258 | for( $i = 0; $i < $many; $params[] = '$params['.$i++.']' ); |
---|
259 | |
---|
260 | $proxy = create_function( '$method, $params, $obj', 'return $obj->$method('.implode( ', ', $params ).');' ); |
---|
261 | |
---|
262 | self::$cache[$many] = $proxy; |
---|
263 | } |
---|
264 | |
---|
265 | if( !isset( $arguments ) ) return( $proxy ); |
---|
266 | |
---|
267 | return $proxy( $method, $arguments, $this->service ); |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | /** |
---|
272 | * call |
---|
273 | * |
---|
274 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
275 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
276 | */ |
---|
277 | function call() |
---|
278 | { |
---|
279 | $arguments = func_get_args(); |
---|
280 | |
---|
281 | $service = array_pop( $arguments ); |
---|
282 | |
---|
283 | //handle here dispatch with invalid services |
---|
284 | if( $this ) |
---|
285 | { |
---|
286 | $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service; |
---|
287 | } |
---|
288 | |
---|
289 | return self::locate( $service, $arguments ); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | /** |
---|
294 | * dispatch |
---|
295 | * |
---|
296 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
297 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
298 | * @param $service |
---|
299 | * @param $arguments |
---|
300 | */ |
---|
301 | function dispatch( $service, $arguments ) |
---|
302 | { |
---|
303 | //handle here dispatch with invalid services |
---|
304 | if( $this ) |
---|
305 | { |
---|
306 | $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service; |
---|
307 | } |
---|
308 | |
---|
309 | return self::locate( $service, $arguments ); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | /** |
---|
314 | * call |
---|
315 | * |
---|
316 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
317 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
318 | * @param $method |
---|
319 | * @param $arguments |
---|
320 | */ |
---|
321 | function __call( $method, $arguments ) |
---|
322 | { |
---|
323 | return self::locate( $method, $arguments, $this->serviceName ); |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | /** |
---|
328 | * get |
---|
329 | * |
---|
330 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
331 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
332 | * @param $name |
---|
333 | */ |
---|
334 | function __get( $name ) |
---|
335 | { |
---|
336 | return $this->service->$name; |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | /** |
---|
341 | * call static |
---|
342 | * |
---|
343 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
344 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
345 | * @param $method |
---|
346 | * @param $arguments |
---|
347 | */ |
---|
348 | static function __callStatic( $method, $arguments ) |
---|
349 | { |
---|
350 | return self::locate( $method, $arguments ); |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | /** |
---|
355 | * get service |
---|
356 | * |
---|
357 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
358 | * @author Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br) |
---|
359 | * @param $service |
---|
360 | */ |
---|
361 | static function getService( $service ) |
---|
362 | { |
---|
363 | if( !self::$locators[ $service ] ) |
---|
364 | { |
---|
365 | self::deploy( $service ); |
---|
366 | } |
---|
367 | |
---|
368 | return( clone self::$locators[ $service ] ); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | //estudar uma forma mais elegante de carregar os servicos |
---|
375 | //ServiceLocator::load( "services.conf" ); |
---|
376 | |
---|
377 | ?> |
---|