[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 | * |
---|
[5158] | 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. |
---|
[4458] | 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 | */ |
---|
[4445] | 29 | |
---|
| 30 | //Helper function and defines that must be moved to a common area |
---|
| 31 | ///////////////////////////////////////////////////// |
---|
| 32 | if(!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 | |
---|
[5399] | 43 | define_once( 'ROOT', dirname(__FILE__).'/..' ); |
---|
[4445] | 44 | |
---|
| 45 | define_once( 'SERVICES', ROOT.'/services/' ); |
---|
| 46 | |
---|
| 47 | define_once( 'LIBRARY', ROOT.'/library/' ); |
---|
| 48 | |
---|
| 49 | define_once( 'API', ROOT.'/API/' ); |
---|
| 50 | |
---|
| 51 | if( !class_exists('ServiceLocator') ){ |
---|
| 52 | |
---|
| 53 | ///////////////////////////////////////////////////// |
---|
[4458] | 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 | */ |
---|
[4445] | 62 | class 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 | |
---|
[4458] | 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 ) |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 144 | static function unregister( $service ) |
---|
| 145 | { |
---|
| 146 | $old = self::$locators[ $service ]; |
---|
| 147 | |
---|
| 148 | unset( self::$locators[ $service ] ); |
---|
| 149 | |
---|
| 150 | return( $old ); |
---|
| 151 | } |
---|
| 152 | |
---|
[4458] | 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 | */ |
---|
[4445] | 160 | static function configure( $service ) |
---|
| 161 | { |
---|
| 162 | return( null ); |
---|
| 163 | } |
---|
| 164 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 219 | function __construct( $serviceName, $object ) |
---|
| 220 | { |
---|
| 221 | $this->service = $object; |
---|
| 222 | $this->serviceName = $serviceName; |
---|
| 223 | } |
---|
| 224 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[5124] | 241 | if(isset(self::$cache[$many])) |
---|
[4445] | 242 | $proxy = self::$cache[$many]; |
---|
| 243 | |
---|
[5124] | 244 | |
---|
| 245 | if( !isset($proxy) || !$proxy ) |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 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 | |
---|
[4458] | 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 | */ |
---|
[4445] | 312 | function __call( $method, $arguments ) |
---|
| 313 | { |
---|
| 314 | return self::locate( $method, $arguments, $this->serviceName ); |
---|
| 315 | } |
---|
| 316 | |
---|
[4458] | 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 | */ |
---|
[4445] | 325 | function __get( $name ) |
---|
| 326 | { |
---|
| 327 | return $this->service->$name; |
---|
| 328 | } |
---|
| 329 | |
---|
[4458] | 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 | */ |
---|
[4445] | 339 | static function __callStatic( $method, $arguments ) |
---|
| 340 | { |
---|
| 341 | return self::locate( $method, $arguments ); |
---|
| 342 | } |
---|
| 343 | |
---|
[4458] | 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 | */ |
---|
[4445] | 352 | static function getService( $service ) |
---|
| 353 | { |
---|
[5124] | 354 | if( !isset(self::$locators[ $service ]) ) |
---|
[4445] | 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 | ?> |
---|