Ignore:
Timestamp:
03/13/12 18:23:02 (12 years ago)
Author:
natan
Message:

Ticket #2434 - Implementacao da repeticao de eventos na agenda e adicao de suporte a relacionamentos 1x1 na API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/prototype/api/newcontroller.php

    r5437 r5715  
    44    define( 'ROOTPATH', dirname(__FILE__).'/..' ); 
    55 
    6 require_once(ROOTPATH.'/app/config.php'); 
     6require_once(ROOTPATH.'/api/config.php'); 
    77 
    88/** 
     
    2424        static $config = array(); 
    2525        static $includes = array(); 
     26        static $tx = array(); 
     27        static $txID = 0; 
    2628        static $wallet; 
    2729 
     
    4345 
    4446        public static function clearAll() 
    45         { 
     47            { 
    4648            return self::$cache->clearAll(); 
    47         } 
     49            } 
    4850 
    4951        public static function clear( $id ) 
     
    5254        } 
    5355 
    54         public static function get( $id ) 
     56        public static function check( $id ) 
    5557        { 
    5658            return self::$cache->get( $id ); 
    5759        } 
    5860 
    59         public static function put( $id, $data, $expires, $compressed ) 
     61        public static function store( $id, $data, $expires, $compressed ) 
    6062        { 
    6163            return self::$cache->put( $id, $data, $expires, $compressed ); 
    6264        } 
    6365 
    64         public static function find( $concept, $filter, $options = false ) 
    65         { 
    66             if( isset($URI['id']) && $URI['id'] ) 
    67                 return self::read( $URI, $params, $options ); 
    68  
    69             return self::call( 'find', $URI, $params, $options ); 
    70         } 
    71  
    72         public static function read( $concept, $id, $options = false ) 
    73         { 
    74             if( !is_string( $id ) ) 
    75                 return self::find( $concept, $id, $options ); 
    76  
    77             return self::call( 'read', $concept, $id, $options ); 
    78         } 
    7966         
     67 
     68//      public static function read( $concept, $id = false, $options = false ) 
     69//      { 
     70//          if( !isset($URI['id']) || !$URI['id'] ) 
     71//              return self::find( $URI, $params, $criteria ); 
     72//  
     73//          return self::call( 'read', $URI, $params, $criteria ); 
     74//      } 
     75 
    8076//      public static function deleteAll( $URI, $params = false, $criteria = false ) 
    8177//      { 
     
    8682//      } 
    8783 
    88         public static function delete( $concept, $id = false, $options = false ) 
    89         { 
    90 //          if( !isset($URI['id']) || !$URI['id'] ) 
    91 //              return self::deleteAll( $URI, $params, $criteria ); 
    92  
    93             return self::call( 'delete', $URI, $params, $criteria ); 
    94         } 
    95          
    96 //      public static function replace( $URI, $params, $criteria = false ) 
     84        //      public static function replace( $URI, $params, $criteria = false ) 
    9785//      { 
    9886//          if( isset($URI['id']) && $URI['id'] ) 
     
    10290//      } 
    10391 
    104         public static function update( $concept, $params, $filter = false, $options = false ) 
    105         { 
    106             if( !isset($URI['id']) || !$URI['id'] ) 
    107                 return self::replace( $URI, $params, $criteria ); 
    108  
    109             return self::call( 'update', $URI, $params, $criteria ); 
    110         } 
    111  
    112         public static function create( $concept, $params, $filter = false, $options = false ) 
    113         { 
    114             return self::call( 'create', $concept, $params, $filter, $options ); 
    115         } 
    116  
    117         public static function begin( $concept, $id, $options = false ) 
    118         { 
    119             return self::call( 'begin', $concept, $id, $options ); 
    120         } 
    121  
    122         public static function commit( $concept, /*$id,*/ $params = false, $options = false ) 
    123         { 
    124             return self::call( 'commit', $concept, $id, $params, $options ); 
    125         } 
    126  
    127         public static function rollback($concept, /*$id,*/ $options = false ) 
    128         { 
    129             return self::call( 'rollback', $URI, $params, $options ); 
    130         } 
    131  
    132         public static function format( $type, $data, $options = false ) 
    133         { 
    134             return self::call( 'format', $type, $data, $options ); 
    135         } 
    136  
    137         public static function parse( $type, $data, $options = false ) 
    138         { 
    139             return self::call( 'parse', $type, $data, $options ); 
    140         } 
    141  
    142         public static function URI( $className, $id = false, $service = false ) 
    143         { 
    144             return array( 'concept' => $className, 
    145                           'service' => $service ? $service : false,  
    146                           'id' => $id ? $id : '' ); 
    147         } 
    148  
    149         public static function links( $concept ) 
    150         { 
    151             if( !isset(self::$config[ $concept ]) ) 
    152               self::$config[ $concept ] = self::loadConfig( $concept ); 
    153  
    154             return( isset(self::$config[ $concept ]['links']) ?  
    155                           self::$config[ $concept ]['links'] : array() ); 
    156         } 
    157  
    158         public static function isConcept( $concept ) 
    159         { 
    160             if( isset( self::$config[ $concept ] ) &&  
    161                 self::$config[ $concept ] ) 
    162                 return( true ); 
    163             else 
    164                 return file_exists( ROOTPATH."/config/$concept.ini" ); 
    165         } 
    166  
    167         public static function getConcept( $concept ) 
    168         { 
    169             if( isset( self::$config[ $concept ] ) ) 
    170                 return( self::$config[ $concept ] ); 
    171  
    172             return( self::$config[ $concept ] = self::loadConfig( $concept ) ); 
    173         } 
     92        public static function find( $concept, $options ) 
     93        {     
     94            return self::get( $options['filter'],  self::context( $options, array( 'concept' => $concept ) ) ); 
     95        } 
     96 
     97        public static function delete( $concept, $id = false, $options = array() ) 
     98        { 
     99            return self::put( false, self::context( $options, array( 'concept' => $concept, 'id' => $id ) ) ); 
     100        } 
     101 
     102        public static function update( $concept, $data, $id = false, $options = array() ) 
     103        { 
     104            return self::put( $data, self::context( $options, array( 'concept' => $concept, 'id' => $id ) ) ); 
     105        } 
     106 
     107        public static function create( $concept, $data, $options = array() ) 
     108        { 
     109            return self::put( $data, self::context( $options, array( 'concept' => $concept ) ) ); 
     110        } 
     111 
     112        public static function put( $data, $options ) 
     113        { 
     114            try 
     115            { 
     116                $context = self::context( $options ); 
     117 
     118                $txId = self::begin( $context['service'] ); 
     119 
     120                if( $context['format'] ) 
     121                    $data = self::parse( $data, $context['format'], $options ); 
     122 
     123                if( !isset($options['concept']) ) 
     124                { 
     125                    $return = array(); 
     126 
     127                    for( $data as $concept => $dt ) 
     128                         $return[] = self::put( $dt, array_merge( array( 'concept' => $concept ), $options ) ); 
     129                         
     130                    return $return; 
     131                } 
     132 
     133 
     134                $model = self::$models[ $options['concept'] ]; 
     135 
     136                $postpone = array(); 
     137 
     138                if( $data ) 
     139                { 
     140                    foreach( $model['hasMany'] as $linkName => $linkTarget ) 
     141                    { 
     142                        $postpone[$linkTarget] = $dt[$linkName]; 
     143                    } 
     144                    foreach( $model['hasOne'] as $linkName => $linkTarget ) 
     145                    { 
     146                        if( isset( $dt[$linkName] ) && is_array( $dt[$linkName] ) ) 
     147                            $dt[$linkName] = self::put( $dt[$linkName],  array_merge( array( 'concept' => $linkTarget ), $options ) ); 
     148                    } 
     149                } 
     150 
     151                $method =        $dt ? isset( $dt['id'] ) ? 
     152                                'update' : 'create' : 'delete'; 
     153 
     154                $context['id'] = $dt ? isset( $dt['id'] ) ? 
     155                                 $dt['id'] : false : $context['id']; 
     156 
     157                self::before( $concept.':'.$method, &$context, $options ); 
     158                self::call( $method, $options, $dt ); 
     159                self::after( $concept.':'.$method, &$context, $options ); 
     160 
     161                $result = $context['result']; 
     162 
     163                if( !is_bool( $result ) && !is_string( $result ) && isset( $result['id'] ) ) 
     164                      $context['id'] = $result['id']; 
     165 
     166                foreach( $postpone as $linkTarget => $dt ) 
     167                      foreach( $dt as $ii => $value ) 
     168                      { 
     169                          if( !is_array( $value ) ) 
     170                            $value = array( 'id' => $value ); 
     171 
     172                          $value[ $options['concept'] ] = $options['id']; 
     173 
     174                          self::put( $value, array_merge( array( 'concept' => $linkTarget ), $options ) ); 
     175                      } 
     176 
     177                if( $txId ) 
     178                    return self::commit( $options['service'], $txId ); 
     179            } 
     180            catch( Exception $e ) 
     181            { 
     182                if( !self::fallback( $e ) ) 
     183                    self::closeAll(); 
     184 
     185                return( false ); 
     186            } 
     187         
     188            return( $options['id'] ); 
     189        } 
     190 
     191        public static function context( $options, $custom = false ) 
     192        { 
     193            if( $service ) 
     194                $options['service'] = $service; 
     195 
     196            return $options; 
     197        } 
     198 
     199        public static function get( $filter, $options = false ) 
     200        { 
     201            return self::call( 'find', self::context( $options, array( 'filter' => $filter ) ) ); 
     202        } 
     203 
     204        public static function connect( $service, $options ) 
     205        { 
     206            $result = self::call( 'open', self::context( $options, array( 'service' => $service ) ) ); 
     207 
     208            if( is_string( $result ) ) 
     209                throw new Exception( $result ); 
     210 
     211            return( true ); 
     212        } 
     213 
     214        public static function begin( $service, $txId = false, $options = false ) 
     215        { 
     216            $context = self::context( $options ); 
     217 
     218            $result = self::call( 'begin', $options ); 
     219 
     220            if( !$txId ) 
     221                $txId = $result ? $result : self::$txID++; 
     222 
     223            if( isset( self::$transactions[ $txId ] ) ) 
     224                return( false ); 
     225 
     226            self::$transactions[ $txId ] = array( 'txID' => $txId ); 
     227            self::$transactions[ $service ][] =& self::$transactions[ $txId ]; 
     228 
     229            return( $txId ); 
     230        } 
     231 
     232        public static function commit( $service, $txId = false, $options = false ) 
     233        { 
     234            $context = self::context( $options ); 
     235 
     236            $txs = self::$transactions[ $service . ( $txId ? '.'.$txId : '' ) ]; 
     237 
     238            if( !is_array( $txs ) ) $txs = array( $txs ); 
     239 
     240            $return = array(); 
     241 
     242            foreach( $txs as $tx ) 
     243            { 
     244                $txID = $tx['txID']; 
     245 
     246                $result = false; 
     247 
     248                if( !$options || !$options['rollback'] ) 
     249                    $result = self::call( 'commit', $context, $tx ); 
     250 
     251                if( !$result ) 
     252                    $result = self::call( 'rollback', $context, $tx ); 
     253 
     254                $return[ $txID ] = $result; 
     255 
     256                unset( self::$transactions[ $txID ] ); 
     257            } 
     258 
     259            return( $txId ? $return[ $txId ] : $return ); 
     260        } 
     261 
     262        public static function rollback( $service, $txId = false, $options = array() ) 
     263        { 
     264            return self::commit( $service, $txId, self::context( $options, array( 'rollback' => true ) ) ); 
     265        } 
     266 
     267        public static function fallback( $exception, $context ) // ver a melhor forma de tratar exceptions nesse caso 
     268        { 
     269            if( !self::emmit( 'fallback', self::context( $context, array( 'exception' => $exception ) ), $exception ) ) 
     270                error_log( $exception->getMessage() ); 
     271  
     272            return( true ); 
     273        } 
     274 
     275        public static function format( $data, $service = false, $options = array() ) 
     276        { 
     277            return self::call( 'format', self::context( $options, array( 'service' => $service ) ), $data ); 
     278        } 
     279 
     280        public static function parse( $data, $service = false, $options = array() ) 
     281        { 
     282            return self::call( 'parse', self::context( $options, array( 'service' => $service ) ), $data ); 
     283        } 
     284 
     285        public static function before( $eventName, &$context, $extra = false ) 
     286        { 
     287            return self::emmit( 'before.'.$eventName, $context, $extra ); 
     288        } 
     289 
     290        public static function after( $eventName, &$context, $extra = false ) 
     291        { 
     292            return self::emmit( 'after.'.$eventName, $context, $extra ); 
     293        } 
     294 
     295        public static function emmit( $eventName, &$context, $extra = false ) 
     296        { 
     297            if( self::$listeners[ $eventName ] ) 
     298                return( false ); 
     299 
     300            foreach( self::$listeners[ $eventName ] as $listen => $listener ) 
     301            { 
     302                 $return = $listener->$listen( $context, $extra ); 
     303 
     304                 if( $return === false ) 
     305                    return( true ); 
     306 
     307                 $context = self::context( $context, array( 'return' => $return ) ); 
     308            } 
     309 
     310            return( $return ); 
     311        } 
     312 
     313        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmos nas options passadas 
     314        public static function call( $method, $options, $data = false ) //see how data fit in it 
     315        { 
     316            try 
     317            { 
     318                $context = self::context( $options, array( 'data' => $data ) ); 
     319 
     320                $service = $context['service']; 
     321 
     322                if( $context['config'] ) 
     323                    self::connect( $service, $context['config'] ); 
     324 
     325                self::before( $service.'.'.$method, &$context, $options ); 
     326 
     327                if( self::$services[ $service ] ) 
     328                    switch( $method ) 
     329                    {  
     330                        case 'find': $return = self::$services[ $service ]->find( $context['URI'], $context['properties'], $context['criteria'] ); break; 
     331 
     332                        case 'read': $return = self::$services[ $service ]->read( $context['URI'], $context['properties']/*, $criteria*/ ); break; 
     333 
     334                        case 'create': $return = self::$services[ $service ]->create( $context['URI'], $context['properties']/*, $criteria*/ ); break; 
     335 
     336                        case 'delete': $return = self::$services[ $service ]->delete( $context['URI'], $context['properties']/*, $criteria*/ ); break; 
     337 
     338                        case 'deleteAll': $return = self::$services[ $service ]->deleteAll( $context['URI'], $context['properties'], $context['criteria'] ); break; 
     339 
     340                        case 'update': $return = self::$services[ $service ]->update( $context['URI'], $context['properties']/*, $criteria*/ ); break; 
     341 
     342                        case 'replace': $return = self::$services[ $service ]->replace( $context['URI'], $context['properties'], $context['criteria'] ); break; 
     343 
     344                        case 'begin': $return = self::$services[ $service ]->begin( $context['URI'] ); break; 
     345 
     346                        case 'commit': $return = self::$services[ $service ]->commit( $context['URI'], $context['criteria'] ); break; 
     347 
     348                        case 'rollback': $return = self::$services[ $service ]->rollback( $context['URI'], $context['criteria'] ); break; 
     349 
     350                        case 'parse': $return = self::$services[ $service ]->parse( $context['properties'], $context['criteria'] ); break; 
     351 
     352                        case 'analize': $return = self::$services[ $service ]->analize( $context['properties'], $context['criteria'] ); break; 
     353 
     354                        case 'format': $return = self::$services[ $service ]->format( $context['properties'], $context['criteria'] ); break; 
     355 
     356                        default : $return = self::$services[ $service ]->$method( $context['properties'], $context['criteria'] ); 
     357                    } 
     358 
     359                $context['return'] = $return; 
     360 
     361                self::after( $service.'.'.$method, &$context, $options ); 
     362            } 
     363            catch( Exception $e ) 
     364            { 
     365                if( !self::fallback( $e ) ) 
     366                    self::closeAll(); 
     367 
     368                return( false ); 
     369            } 
     370 
     371            return( $context['return'] );  
     372        } 
     373 
     374//      public static function URI( $className, $id = false, $service = false ) 
     375//      { 
     376//          return array( 'concept' => $className, 
     377//                        'service' => $service ? $service : false,  
     378//                        'id' => $id ? $id : '' ); 
     379//      } 
     380 
     381        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados 
     382//      public static function links( $concept = false ) 
     383//      { 
     384//          if( !isset(self::$config[ $concept ]) ) 
     385//            self::$config[ $concept ] = self::loadConfig( $concept ); 
     386//  
     387//          return( isset(self::$config[ $concept ]['links']) ?  
     388//                        self::$config[ $concept ]['links'] : array() ); 
     389//      } 
     390 
     391//      public static function isConcept( $concept ) 
     392//      {  
     393//          if( isset( self::$config[ $concept ] ) &&  
     394//              self::$config[ $concept ] ) 
     395//              return( true ); 
     396//              else 
     397//              return file_exists( ROOTPATH."/config/$concept.ini" ); 
     398//      } 
     399 
     400//      public static function getConcept( $concept, $moduleName = false ) 
     401//      { 
     402//          if( isset( self::$config[ $concept ] ) ) 
     403//              return( self::$config[ $concept ] ); 
     404//  
     405//          return( self::$config[ $concept ] = self::loadConfig( $concept, $moduleName ) ); 
     406//      } 
    174407 
    175408        public static function loadCache( $cacheType = 'Memory' ) 
     
    178411            return new MemoryCache(); 
    179412        } 
    180          
    181         public static function loadConfig( $className, $isService = false ) 
     413 
     414        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados 
     415        public static function loadConfig( $className, $isService = false) 
    182416        { 
    183417            $fileName = $className.'.'.($isService ? 'srv' : 'ini'); 
    184418 
    185419            $config = self::$cache->get( $fileName ); 
    186  
     420         
    187421            if( !$config ) 
    188422            { 
    189                 $config = parse_ini_file( ROOTPATH."/config/$fileName", true ); 
     423                $config = parse_ini_file( ROOTPATH.'/config/'.$fileName, true ); 
    190424 
    191425                self::$cache->put( $fileName, $config ); 
     
    196430 
    197431        public static function import( $path, $ext = ".php" ) 
    198         { 
    199             
     432        {  
    200433            if( !isset(self::$includes[$path]) ) 
    201             { 
     434        { 
    202435                require_once( ROOTPATH.'/'.$path.$ext ); 
    203436                self::$includes[$path] = false; 
     
    208441 
    209442        public static function load( $path, $class = false ) 
    210         { 
    211              
     443            {  
    212444            if( $return = self::import( $path, "" ) ) 
    213445                return( $return ); 
    214              
     446 
    215447            if( !$class ){ 
    216448                preg_match( '/^\/?.*\/([^\/]+).php$/', $path, $class ); 
     
    236468            { 
    237469                //// Hack //// TODO: passar o init da sessão no login do expresso 
    238                 if ( isset( $_COOKIE[ 'sessionid' ] ) )  
    239                       session_id( $_COOKIE[ 'sessionid' ] );  
    240                  
    241                 @session_start(); 
    242  
    243 //              if( !isset( $_SESSION['wallet'] ) ) 
    244 //              { 
    245 //                  if( isset( $GLOBALS['phpgw_info'] ) ) 
    246 //                  { 
    247 //                              $_SESSION['wallet']['Sieve']['user']          = $GLOBALS['phpgw_info']['user']['account_lid']; 
    248 //                              $_SESSION['wallet']['Sieve']['password']      = $GLOBALS['phpgw_info']['user']['passwd']; 
    249 //  
    250 //                              $_SESSION['wallet']['Cyrus']['user']          = $GLOBALS['phpgw_info']['user']['account_lid']; 
    251 //                              $_SESSION['wallet']['Cyrus']['password']      = $GLOBALS['phpgw_info']['user']['passwd']; 
    252 //  
    253 //                              $_SESSION['wallet']['user']                   = $GLOBALS['phpgw_info']['user']['account_lid']; 
    254 //                      //                      $_SESSION['wallet']['me']['password']      = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; 
    255 //  
    256 //                              $_SESSION['wallet']['PostgreSQL']['user']     = $GLOBALS['phpgw_info']['server']['db_user']; 
    257 //                              $_SESSION['wallet']['PostgreSQL']['password'] = $GLOBALS['phpgw_info']['server']['db_pass']; 
    258 //                              $_SESSION['wallet']['PostgreSQL']['dbname']   = $GLOBALS['phpgw_info']['server']['db_name']; 
    259 //                  } 
    260 //                  else 
    261 //                      $_SESSION['wallet'] = array(); 
    262 //              } 
    263                  
     470                Config::init(); 
     471 
    264472                if(isset($_SESSION['wallet'])) 
    265473                    self::$wallet = $_SESSION['wallet']; 
     
    269477            return isset( self::$wallet[ $serviceName ] )? self::$wallet[ $serviceName ] : false; 
    270478        } 
     479                 
     480         
     481 
     482        public static function configure( $config, $newConfig ) 
     483        { 
     484            foreach( $newConfig as $key => $value ) 
     485                $config[$key] = $value; 
     486 
     487            return( $config ); 
     488            } 
    271489 
    272490        public static function dispatch( $dispatcher, $data, $optionsMap = false ) 
     
    283501        } 
    284502 
    285         public static function loadAll( $configurable, $isService = false ) 
    286         { 
    287         } 
    288  
    289         public static function connect( $service, $config ) 
    290         { 
    291             $result = $service->open( $config ); 
    292  
    293             if( is_string( $result ) ) 
    294                 throw new Exception( $result ); 
    295  
    296             return( true ); 
    297         } 
    298  
    299         public static function open( $serviceName, $config = false ) 
    300         { 
    301             $service = self::$services[ $serviceName ]; 
    302  
    303             $srvConfig = array(); 
     503        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados 
     504        public static function service( $serviceName, $concept = false ) 
     505        { 
     506            if( isset( self::$services[ $serviceName ] ) ) 
     507                return self::$services[ $serviceName ]; 
     508 
     509            if( !isset(self::$config[ $serviceName ]) ) 
     510                 self::$config[ $serviceName ] = self::loadConfig( $serviceName, true ); 
     511 
     512            if( !isset(self::$config[ $serviceName ]) ) 
     513                return( false ); 
     514 
     515            if( !isset(self::$config[ $serviceName ]['type']) ) 
     516                self::$config[ $serviceName ]['type'] = 'service'; 
     517 
     518            self::import( 'api/'.self::$config[ $serviceName ]['type'] );   //TODO: Item 4 
     519 
     520            $service = self::load( self::$config[ $serviceName ]['path'], 
     521                                   self::$config[ $serviceName ]['class'] ); 
     522 
     523              $srvConfig = array(); 
    304524 
    305525            if( isset(self::$config[ $serviceName ][ 'config' ]) ) 
     
    307527            if( $wallet = self::wallet( $serviceName ) ) 
    308528                $srvConfig = self::configure( $srvConfig, $wallet ); 
    309             if( $config ) 
    310                 $srvConfig = self::configure( $srvConfig, $config ); 
     529            if( $concept && isset(self::$config[ $concept ]['service.config']) ) 
     530                $srvConfig = self::configure( $srvConfig, self::$config[ $concept ]['service.config'] ); 
    311531 
    312532            if( empty( $srvConfig ) ) 
     
    315535            if( $service && self::$config[ $serviceName ]['type'] === 'service' ) 
    316536                self::connect( $service, $srvConfig ); 
    317         } 
    318  
    319         public static function configure( $config, $newConfig ) 
    320         { 
    321             foreach( $newConfig as $key => $value ) 
    322                 $config[$key] = $value; 
    323  
    324             return( $config ); 
    325         } 
    326  
    327         public static function service( $serviceName, $concept = false ) 
    328         { 
    329             if( isset( self::$services[ $serviceName ] ) ) 
    330                 return self::$services[ $serviceName ]; 
    331  
    332             if( !isset(self::$config[ $serviceName ]) ) 
    333                  self::$config[ $serviceName ] = self::loadConfig( $serviceName, true ); 
    334  
    335             if( !isset(self::$config[ $serviceName ]) ) 
    336                 return( false ); 
    337  
    338             if( !isset(self::$config[ $serviceName ]['type']) ) 
    339                 self::$config[ $serviceName ]['type'] = 'service'; 
    340  
    341             self::import( 'app/'.self::$config[ $serviceName ]['type'] );   //TODO: Item 4 
    342  
    343             self::$services[ $serviceName ] = self::load( self::$config[ $serviceName ]['path'], 
    344                                                           self::$config[ $serviceName ]['class'] ); 
    345  
    346             if( $concept && isset(self::$config[ $concept ]['service.config']) ) 
    347                 self::open( $serviceName, $self::$config[ $concept ]['service.config'] ); 
    348             else 
    349                 self::open( $serviceName ); 
    350537 
    351538            return( self::$services[ $serviceName ] = $service ); 
    352539        } 
    353540 
     541        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados 
    354542        public static function interceptor( $method, $concept = false, $serviceName = false, $isService = false ) 
    355543        { 
     
    390578        } 
    391579 
    392         public static function fire( $eventType, $method, $params, $original, $isService = false ) 
    393         { 
    394             $interceptors = self::interceptor( $method, 
    395                                                isset($original['URI']['concept']) ? $original['URI']['concept'] : false, 
    396                                                isset($params['URI']['service']) ? $params['URI']['service'] : false, $isService ); 
    397  
    398             if( $interceptors && isset($interceptors[ $eventType ]) ) 
    399                 foreach( $interceptors[ $eventType ] as $intercept => $interceptor ) 
    400                 { 
    401                     $return = $interceptor->$intercept( $params['URI'], $params['properties'], $params['criteria'], $original, $params['service'] ); 
    402  
    403                     if( $return === false ) 
    404                         return( false ); 
    405  
    406                     if( isset($return) ) 
    407                         $params['properties'] = $return; 
    408                 } 
    409  
    410               return( $params ); 
    411         } 
     580//      public static function interceptorCommit( $eventType, $commitList, $isService = false ) 
     581//      { 
     582//          $result = array( $eventType => array() ); 
     583//          
     584//          if( is_array( $commitList ) ) 
     585//              foreach( $commitList as $i => $tx ) 
     586//              { 
     587//                  $interceptors = self::interceptor( 'commit', $tx['concept'], $tx['service'], $isService ); 
     588//        
     589//                  $result[$eventType] = array_merge( $result[$eventType], $interceptors[$eventType] ); 
     590//              } 
     591//  
     592//          return( $result ); 
     593//      } 
     594 
     595//      public static function fire( $eventType, $method, &$params, $original, $isService = false ) 
     596//      { 
     597//          if( $method === 'commit' ) 
     598//              $interceptors = self::interceptorCommit( $eventType, $params['criteria'], $isService ); 
     599//  
     600//          else 
     601//              $interceptors = self::interceptor( $method, 
     602//                                                 isset($original['URI']['concept']) ? $original['URI']['concept'] : false, 
     603//                                                 isset($params['URI']['service']) ? $params['URI']['service'] : false, $isService ); 
     604//  
     605//          if( $interceptors && isset($interceptors[ $eventType ]) ) 
     606//              foreach( $interceptors[ $eventType ] as $intercept => $interceptor ) 
     607//              { 
     608//                  $return = $interceptor->$intercept( $params['URI'], $params['properties'], $params['criteria'], $original/*, $params['service']*/ ); 
     609//  
     610//                  if( $return === false ) 
     611//              return( false ); 
     612//  
     613//                  if( isset($return) ) 
     614//                      $params['properties'] = $return; 
     615//              } 
     616//  
     617//            return( $params ); 
     618//      } 
    412619 
    413620        /* 
     
    424631        */ 
    425632 
    426         public static function call( $method, $URI, $properties, $criteria = false, $service = false ) 
    427         { 
    428             try 
    429             { 
    430                 $original = $params = array( 'properties' => $properties,  
    431                                              'criteria' => $criteria,  
    432                                              'URI' => $URI, 
    433                                              'service' => $service ); 
    434  
    435                 if( isset($params['URI']['concept']) ) 
    436                     $params = self::fire( 'before', $method, $params, $original ); 
    437  
    438                 if( !$params['service'] ) 
    439                 { 
    440                     $isInternal = true; 
    441  
    442                     if( ( !isset($params['URI']['service']) || !$params['URI']['service'] ) && isset($params['URI']['concept']) ) 
    443                     { 
    444                         if( !isset(self::$config[ $URI['concept'] ]) ) 
    445                             self::$config[ $URI['concept'] ] = self::loadConfig( $URI['concept'] ); 
    446  
    447                         $params['URI']['service'] = self::$config[ $URI['concept'] ][ 'service' ]; 
    448                     } 
    449  
    450                     if( !isset($params['URI']['service']) ) 
    451                         return( false ); 
    452  
    453                     $params['service'] = self::service( $params['URI']['service'], isset($params['URI']['concept']) ? $params['URI']['concept'] : false ); 
    454                 } 
    455  
    456                 if( isset($params['URI']['service']) ) 
    457                     $params = self::fire( 'before', $method, $params, $original, true ); 
    458  
    459                 if( $params['service'] ) 
    460                     switch( $method ) 
    461                     { 
    462                         case 'find': $return = $params['service']->find( $params['URI'], $params['properties'], $params['criteria'] ); break; 
    463  
    464                         case 'read': $return = $params['service']->read( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
    465  
    466                         case 'create': $return = $params['service']->create( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
    467  
    468                         case 'delete': $return = $params['service']->delete( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
    469  
    470                         case 'deleteAll': $return = $params['service']->deleteAll( $params['URI'], $params['properties'], $params['criteria'] ); break; 
    471  
    472                         case 'update': $return = $params['service']->update( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
    473  
    474                         case 'replace': $return = $params['service']->replace( $params['URI'], $params['properties'], $params['criteria'] ); break; 
    475  
    476                         case 'begin': $return = $params['service']->begin( $params['URI'] ); break; 
    477  
    478                         case 'commit': $return = $params['service']->commit( $params['URI'] ); break; 
    479  
    480                         case 'rollback': $return = $params['service']->rollback( $params['URI'] ); break; 
    481  
    482                         case 'parse': $return = $params['service']->parse( $params['properties'], $params['criteria'] ); break; 
    483  
    484                         case 'format': $return = $params['service']->format( $params['properties'], $params['criteria'] ); break; 
    485                     } 
    486      
    487                 if( isset($return) && $return !== false ) 
    488                     $params['properties'] = $return; 
    489  
    490                 if( isset($params['URI']['service']) ) 
    491                     $params = self::fire( 'after', $method, $params, $original, true ); 
    492  
    493                 if( isset($URI['concept']) ) 
    494                     $params = self::fire( 'after', $method, $params, $original ); 
    495             } 
    496             catch( Exception $e ) 
    497             { 
    498                 if( !self::fallback( $e ) ) 
    499                     self::closeAll(); 
    500  
    501                 return( false ); 
    502             } 
    503  
    504             return( empty($params['properties']) ? false : $params['properties'] ); 
    505         } 
    506  
    507         public static function fallback( $exception ) 
    508         { 
    509             error_log( $exception->getMessage() ); 
    510             return( true ); 
     633        //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmo nos parametros passados 
     634        public static function serviceName( $URI, $original = false ) 
     635        { 
     636             $concept = ""; 
     637 
     638            if( $original && isset($original['concept']) && $original['concept'] ) 
     639                $concept = $original['concept']; 
     640            elseif( isset($URI['concept']) && $URI['concept'] ) 
     641                $concept = $URI['concept']; 
     642 
     643            if( ( !isset($URI['service']) || !$URI['service'] ) && $concept ) 
     644            { 
     645                if( !isset(self::$config[ $concept ]) ) 
     646                    self::$config[ $concept ] = self::loadConfig( $concept ); 
     647 
     648                $URI['service'] = self::$config[ $concept ][ 'service' ]; 
     649            } 
     650 
     651            if( !isset($URI['service']) ) 
     652                throw new Exception( "CONFIGURATION ERROR: service name from concept '$concept' not found" ); 
     653 
     654            return( $URI ); 
    511655        } 
    512656} 
Note: See TracChangeset for help on using the changeset viewer.