[4445] | 1 | <?php |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | ///////////////////////////////////////////////////// |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 ) |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 155 | static function unregister( $service ) |
---|
| 156 | { |
---|
| 157 | $old = self::$locators[ $service ]; |
---|
| 158 | |
---|
| 159 | unset( self::$locators[ $service ] ); |
---|
| 160 | |
---|
| 161 | return( $old ); |
---|
| 162 | } |
---|
| 163 | |
---|
[4458] | 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 | */ |
---|
[4445] | 171 | static function configure( $service ) |
---|
| 172 | { |
---|
| 173 | return( null ); |
---|
| 174 | } |
---|
| 175 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 230 | function __construct( $serviceName, $object ) |
---|
| 231 | { |
---|
| 232 | $this->service = $object; |
---|
| 233 | $this->serviceName = $serviceName; |
---|
| 234 | } |
---|
| 235 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[5124] | 252 | if(isset(self::$cache[$many])) |
---|
[4445] | 253 | $proxy = self::$cache[$many]; |
---|
| 254 | |
---|
[5124] | 255 | |
---|
| 256 | if( !isset($proxy) || !$proxy ) |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 323 | function __call( $method, $arguments ) |
---|
| 324 | { |
---|
| 325 | return self::locate( $method, $arguments, $this->serviceName ); |
---|
| 326 | } |
---|
| 327 | |
---|
[4458] | 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 | */ |
---|
[4445] | 336 | function __get( $name ) |
---|
| 337 | { |
---|
| 338 | return $this->service->$name; |
---|
| 339 | } |
---|
| 340 | |
---|
[4458] | 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 | */ |
---|
[4445] | 350 | static function __callStatic( $method, $arguments ) |
---|
| 351 | { |
---|
| 352 | return self::locate( $method, $arguments ); |
---|
| 353 | } |
---|
| 354 | |
---|
[4458] | 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 | */ |
---|
[4445] | 363 | static function getService( $service ) |
---|
| 364 | { |
---|
[5124] | 365 | if( !isset(self::$locators[ $service ]) ) |
---|
[4445] | 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 | ?> |
---|