source: trunk/API/class.servicelocator.php @ 5124

Revision 5124, 9.6 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo API.

  • Property svn:executable set to *
Line 
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/////////////////////////////////////////////////////
43if(!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
54define_once( 'ROOT', $_SESSION['rootPath'].'/' );
55
56define_once( 'SERVICES', ROOT.'/services/' );
57
58define_once( 'LIBRARY', ROOT.'/library/' );
59
60define_once( 'API', ROOT.'/API/' );
61
62if( !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*/
73class 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        if(isset(self::$cache[$many]))
253        $proxy = self::$cache[$many];
254
255       
256        if( !isset($proxy) || !$proxy )
257        {
258            $params = array();
259
260            for( $i = 0; $i < $many; $params[] = '$params['.$i++.']' );
261
262            $proxy = create_function( '$method, $params, $obj', 'return $obj->$method('.implode( ', ', $params ).');' );
263
264            self::$cache[$many] = $proxy;
265        }
266
267        if( !isset( $arguments ) ) return( $proxy );
268
269        return $proxy( $method, $arguments, $this->service );
270    }
271
272       
273        /**
274        * call
275        *
276        * @license    http://www.gnu.org/copyleft/gpl.html GPL
277        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
278        */
279    function call()
280    {
281        $arguments = func_get_args();
282
283        $service = array_pop( $arguments );
284
285        //handle here dispatch with invalid services
286        if( $this )
287        {
288            $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service;
289        }
290
291        return self::locate( $service, $arguments );
292    }
293
294       
295        /**
296        * dispatch
297        *
298        * @license    http://www.gnu.org/copyleft/gpl.html GPL
299        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
300        * @param      $service
301        * @param      $arguments
302        */
303    function dispatch( $service, $arguments )
304    {
305        //handle here dispatch with invalid services
306        if( $this )
307        {
308            $service = strrpos( $service, "." ) ? $service : $this->serviceName.".".$service;
309        }
310
311        return self::locate( $service, $arguments );
312    }
313
314       
315        /**
316        * call
317        *
318        * @license    http://www.gnu.org/copyleft/gpl.html GPL
319        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
320        * @param      $method
321        * @param      $arguments
322        */
323    function __call( $method, $arguments )
324    {
325        return self::locate( $method, $arguments, $this->serviceName );
326    }
327
328       
329        /**
330        * get
331        *
332        * @license    http://www.gnu.org/copyleft/gpl.html GPL
333        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
334        * @param      $name
335        */
336    function __get( $name )
337    {
338        return $this->service->$name;
339    }
340
341       
342        /**
343        * call static
344        *
345        * @license    http://www.gnu.org/copyleft/gpl.html GPL
346        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
347        * @param      $method
348        * @param      $arguments
349        */
350    static function __callStatic( $method, $arguments )
351    {
352        return self::locate( $method, $arguments );
353    }
354
355       
356        /**
357        * get service
358        *
359        * @license    http://www.gnu.org/copyleft/gpl.html GPL
360        * @author     Consórcio Expresso Livre - 4Linux (www.4linux.com.br) e Prognus Software Livre (www.prognus.com.br)
361        * @param      $service
362        */
363    static function getService( $service )
364    {
365        if( !isset(self::$locators[ $service ]) )
366        {
367            self::deploy( $service );
368        }
369
370        return( clone self::$locators[ $service ] );
371    }
372}
373
374}
375
376//estudar uma forma mais elegante de carregar os servicos
377//ServiceLocator::load( "services.conf" );
378
379?>
Note: See TracBrowser for help on using the repository browser.