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

Revision 5158, 9.1 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus. Ultimas sincronizacoes

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