Ignore:
Timestamp:
01/10/12 11:25:51 (12 years ago)
Author:
wmerlotto
Message:

Ticket #2434 - Commit inicial do novo módulo de agenda do Expresso - expressoCalendar

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/prototype/app/controller.php

    r5136 r5341  
    11<?php 
    22 
    3 require_once "service.php"; 
     3if( !defined( 'ROOTPATH' ) ) 
     4    define( 'ROOTPATH', dirname(__FILE__).'/..' ); 
     5 
     6require_once(ROOTPATH.'/app/config.php'); 
     7 
     8/** 
     9TODO list: 
     10 
     11  * definir de forma centralizada os caminhos e as constantes necessárias; 
     12  * criar um User Agent detect e um OS server detect para customizações espeçíficas de cada browser / servidor; 
     13  * criar um registrador para fallback handlers; 
     14  * criar um dependency manager na configuração dos serviços, para poder gerenciar os imports corretamente 
     15  * criar um login e a recuperação da sessão; 
     16 
     17*/ 
    418 
    519class Controller { 
    620 
    7         var $config; 
    8         var $className; 
    9         var $parent; 
    10         var $target; 
    11         var $cache; 
    12         var $service; 
    13         var $includes; 
    14  
    15         public static function newInstance( $className, $id = false, $parents = false  ) 
    16         { 
    17             return new Controller( $className, $id, $parents ); 
    18         } 
    19  
    20         public function __construct( $className, $id = false, $parents = false ) 
    21         { 
    22             $this->className = $className; 
    23  
    24             if( is_array( $id ) ) 
    25             { 
    26                 $parents = $id; 
    27                 $id = null; 
    28             } 
    29  
    30             $this->parents = $parents; 
    31             $this->target = $id; 
    32             $this->includes = array(); 
    33  
     21        static $cache; 
     22        static $services = array(); 
     23        static $interceptors = array(); 
     24        static $config = array(); 
     25        static $includes = array(); 
     26        static $tx = array(); 
     27        static $txID = 0; 
     28        static $wallet; 
     29 
     30        public function __destruct() 
     31        { 
     32//          if( $this->service ) 
     33//              $this->service->close(); 
     34//          else 
     35            self::closeAll(); 
     36        } 
     37 
     38        public static function closeAll() 
     39        { 
     40            if( self::$services ) 
     41                foreach( self::$services as $serviceName => $service ) 
     42                    if( self::$config[ $serviceName ]['type'] === 'service' ) 
     43                      $service->close(); 
     44        } 
     45 
     46        public static function clearAll() 
     47            { 
     48            return self::$cache->clearAll(); 
     49            } 
     50 
     51        public static function clear( $id ) 
     52        { 
     53            return self::$cache->clear( $id ); 
     54        } 
     55 
     56        public static function check( $id ) 
     57        { 
     58            return self::$cache->get( $id ); 
     59        } 
     60 
     61        public static function store( $id, $data, $expires, $compressed ) 
     62        { 
     63            return self::$cache->put( $id, $data, $expires, $compressed ); 
     64        } 
     65 
     66        public static function put( $URI, $data, $txIds = false ) 
     67        { 
     68            try 
     69            { 
     70                $URI = self::serviceName( $URI ); 
     71 
     72                if( $commit = !$txIds ) 
     73                            $txIds = array(); 
     74 
     75                if( !isset( self::$tx[ $URI['service'] ] ) ) 
     76                { 
     77                            self::call( 'begin', $URI ); 
     78                            self::$tx[ $txIds[] = $URI['service'] ] = array(); 
     79            } 
     80 
     81                $method = $data ? isset( $data['id'] ) ? 
     82                          'update' : 'create' : 'delete'; 
     83 
     84                $links = self::links( $URI['concept'] ); 
     85 
     86                $Tx = array( 'order' => self::$txID++ ); 
     87 
     88                $postpone = array(); 
     89 
     90                if( $data ) 
     91                { 
     92                    $URI['id'] = isset( $data['id'] ) ? $data['id'] : false; 
     93 
     94                    foreach( $links as $linkName => $linkTarget ) 
     95                    { 
     96                                if( isset( $data[$linkName] ) && is_array( $data[$linkName] ) ) 
     97                                { 
     98                                        if( self::isConcept( $linkName ) ) 
     99                                                $data[$linkName] = self::put( array( 'concept' => $linkTarget ), $data[$linkName], &$txIds ); 
     100                                        else 
     101                                                $postpone[$linkTarget] = $data[$linkName]; 
     102                                } 
     103        } 
     104                } 
     105                else 
     106                  $URI['id'] = isset( $data['id'] ) ? $data['id'] : $URI['id']; 
     107 
     108                $result = Controller::call( $method, $URI, $data, false, false, true ); 
     109 
     110                if( !is_bool( $result ) && !is_string( $result ) && isset( $result['id'] ) ) 
     111                      $URI['id'] = $result['id']; 
     112 
     113                self::$tx[ $URI['service'] ][] = array_merge( $Tx, array( 'id' => $URI['id'], 'concept' => $URI['concept'], 'method' => $method, 'service' => $URI['service'], 'rollback' => !!!$result ) ); 
     114 
     115                foreach( $postpone as $linkTarget => $dt ) 
     116                      foreach( $dt as $ii => $value ) 
     117        { 
     118                          if( !is_array( $value ) ) 
     119                            $value = array( 'id' => $value ); 
     120 
     121                          $value[ $URI['concept'] ] = $URI['id']; 
     122 
     123                          self::put( array( 'concept' => $linkTarget ), $value, &$txIds ); 
     124        } 
     125 
     126                if( $commit ) 
     127        { 
     128                      $result = array(); 
     129 
     130                      for( $i = count( $txIds ) - 1; $i >= 0; $i-- ) 
     131                      { 
     132                              $currentTx = self::$tx[ $txIds[$i] ]; 
     133                              unset( self::$tx[ $txIds[$i] ] ); 
     134 
     135                              if( !self::commit( array( 'service' => $txIds[$i] ), $currentTx ) ) 
     136                              { 
     137                                  self::rollback( array( 'service' => $txIds[$i] ), $currentTx ); 
     138 
     139                                  foreach( $currentTx as $i => $st ) 
     140                                      $currentTx[$i][ 'rollback' ] = true; 
     141        } 
     142 
     143                              $result = array_merge( $result, $currentTx ); 
     144                      } 
     145                           
     146                          self::$txID = 0; 
     147 
     148                      return( $result ); 
     149                } 
     150 
     151            } 
     152            catch( Exception $e ) 
     153        { 
     154                if( !self::fallback( $e ) ) 
     155                    self::closeAll(); 
     156 
     157                return( false ); 
     158            } 
     159         
     160            return( $URI['id'] ); 
     161        } 
     162         
     163        public static function get() 
     164            { 
     165             
     166            } 
     167 
     168        public static function find( $URI, $params = false, $criteria = false ) 
     169        { 
     170            if( isset($URI['id']) && $URI['id'] ) 
     171                return self::read( $URI, $params, $criteria ); 
     172             
     173            return self::call( 'find', $URI, $params, $criteria ); 
     174        } 
     175 
     176        public static function read( $URI, $params = false, $criteria = false ) 
     177        { 
     178            if( !isset($URI['id']) || !$URI['id'] ) 
     179                return self::find( $URI, $params, $criteria ); 
     180 
     181            return self::call( 'read', $URI, $params, $criteria ); 
     182        } 
     183 
     184        public static function deleteAll( $URI, $params = false, $criteria = false ) 
     185        { 
     186            if( isset($URI['id']) && $URI['id'] ) 
     187                return self::delete( $URI, $params, $criteria ); 
     188 
     189            return self::call( 'deleteAll', $URI, $params, $criteria ); 
     190        } 
     191 
     192        public static function delete( $URI, $params = false, $criteria = false ) 
     193        { 
     194            if( !isset($URI['id']) || !$URI['id'] ) 
     195                return self::deleteAll( $URI, $params, $criteria ); 
     196 
     197            return self::call( 'delete', $URI, $params, $criteria ); 
     198        } 
     199 
     200        public static function replace( $URI, $params, $criteria = false ) 
     201        { 
     202            if( isset($URI['id']) && $URI['id'] ) 
     203                return self::update( $URI, $params, $criteria ); 
     204 
     205            return self::call( 'replace', $URI, $params, $criteria ); 
     206        } 
     207 
     208        public static function update( $URI, $params, $criteria = false ) 
     209        { 
     210            if( !isset($URI['id']) || !$URI['id'] ) 
     211                return self::replace( $URI, $params, $criteria ); 
     212 
     213            return self::call( 'update', $URI, $params, $criteria ); 
     214        } 
     215 
     216        public static function create( $URI, $params, $criteria = false ) 
     217        { 
     218            return self::call( 'create', $URI, $params, $criteria ); 
     219        } 
     220 
     221        public static function begin( $URI, $params = false, $criteria = false ) 
     222        { 
     223            return self::call( 'begin', $URI, $params, $criteria ); 
     224        } 
     225 
     226        public static function commit( $URI, $criteria = false ) 
     227            { 
     228            return self::call( 'commit', $URI, false, $criteria ); 
     229        } 
     230 
     231        public static function rollback( $URI, $criteria = false ) 
     232        { 
     233            return self::call( 'rollback', $URI, false, $criteria ); 
     234        } 
     235 
     236        public static function format( $URI, $params, $criteria = false ) 
     237        { 
     238            return self::call( 'format', $URI, $params, $criteria ); 
     239        } 
     240 
     241        public static function parse( $URI, $data, $criteria = false ) 
     242        { 
     243            return self::call( 'parse', $URI, $data, $criteria ); 
     244            } 
     245 
     246        public static function URI( $className, $id = false, $service = false ) 
     247        { 
     248            return array( 'concept' => $className, 
     249                          'service' => $service ? $service : false,  
     250                          'id' => $id ? $id : '' ); 
     251        } 
     252 
     253        public static function links( $concept ) 
     254        { 
     255            if( !isset(self::$config[ $concept ]) ) 
     256              self::$config[ $concept ] = self::loadConfig( $concept ); 
     257 
     258            return( isset(self::$config[ $concept ]['links']) ?  
     259                          self::$config[ $concept ]['links'] : array() ); 
     260        } 
     261 
     262        public static function isConcept( $concept ) 
     263            {  
     264            if( isset( self::$config[ $concept ] ) &&  
     265                self::$config[ $concept ] ) 
     266                return( true ); 
     267                else 
     268                return file_exists( ROOTPATH."/config/$concept.ini" ); 
     269        } 
     270 
     271        public static function getConcept( $concept ) 
     272        { 
     273            if( isset( self::$config[ $concept ] ) ) 
     274                return( self::$config[ $concept ] ); 
     275 
     276            return( self::$config[ $concept ] = self::loadConfig( $concept ) ); 
     277            } 
     278 
     279        public static function loadCache( $cacheType = 'Memory' ) 
     280        { 
    34281            include_once( "cache/MemoryCache.php" ); 
    35             $this->cache = new MemoryCache(); 
    36  
    37             $this->config = $this->cache->get( "$className.ini" ); 
    38  
    39             if( !$this->config ) 
    40             { 
    41                 $this->config = parse_ini_file( "config/$className.ini", true ); 
    42  
    43                 $this->cache->put( "$className.ini", $this->config[$className] ); 
    44             } 
    45  
    46             $this->service = $this->load( "service" ); 
    47  
    48             if( $this->service ) 
    49                 $this->service->connect( $this->config["config"] ); 
    50         } 
    51  
    52         public function __destruct() 
    53         { 
    54             if( $this->service ) 
    55                 $this->service->close(); 
    56         } 
    57  
    58         public function load( $type ) 
    59         { 
    60             if( !$this->config[$type] ) 
     282            return new MemoryCache(); 
     283        } 
     284 
     285        public static function loadConfig( $className, $isService = false ) 
     286        { 
     287            $fileName = $className.'.'.($isService ? 'srv' : 'ini'); 
     288 
     289            $config = self::$cache->get( $fileName ); 
     290 
     291            if( !$config ) 
     292            { 
     293                $config = parse_ini_file( ROOTPATH."/config/$fileName", true ); 
     294 
     295                self::$cache->put( $fileName, $config ); 
     296            } 
     297 
     298            return( $config ); 
     299        } 
     300 
     301        public static function import( $path, $ext = ".php" ) 
     302        {  
     303            if( !isset(self::$includes[$path]) ) 
     304        { 
     305                require_once( ROOTPATH.'/'.$path.$ext ); 
     306                self::$includes[$path] = false; 
     307            } 
     308 
     309            return( self::$includes[$path] ); 
     310        } 
     311 
     312        public static function load( $path, $class = false ) 
     313            {  
     314            if( $return = self::import( $path, "" ) ) 
     315                return( $return ); 
     316 
     317            if( !$class ){ 
     318                preg_match( '/^\/?.*\/([^\/]+).php$/', $path, $class ); 
     319                $class = $class[1]; 
     320            } 
     321 
     322            $object =  self::$cache->get( $class ); 
     323 
     324            if( !$object ) 
     325            { 
     326                $object = new $class(); 
     327                 self::$cache->put( $class, $object ); 
     328            } 
     329 
     330            self::$includes[$path] = $object; 
     331 
     332            return( $object ); 
     333        } 
     334 
     335        public static function wallet( $serviceName ) 
     336        { 
     337            if( !isset( self::$wallet ) ) 
     338            { 
     339                //// Hack //// TODO: passar o init da sessão no login do expresso 
     340                Config::init(); 
     341 
     342                if(isset($_SESSION['wallet'])) 
     343                    self::$wallet = $_SESSION['wallet']; 
     344                ///////////// 
     345            } 
     346 
     347            return isset( self::$wallet[ $serviceName ] )? self::$wallet[ $serviceName ] : false; 
     348        } 
     349                 
     350        public static function connect( $service, $config ) 
     351            { 
     352            $result = $service->open( $config ); 
     353 
     354            if( is_string( $result ) ) 
     355                throw new Exception( $result ); 
     356 
     357            return( true ); 
     358        } 
     359 
     360        public static function configure( $config, $newConfig ) 
     361        { 
     362            foreach( $newConfig as $key => $value ) 
     363                $config[$key] = $value; 
     364 
     365            return( $config ); 
     366            } 
     367 
     368        public static function dispatch( $dispatcher, $data, $optionsMap = false ) 
     369        { 
     370//          if( $mappedTo ) 
     371//              $data = array( $mappedTo => $data ); 
     372//  
     373//          foreach( $data as $method => $params ) 
     374//          { 
     375// //           foreach( $data[ $method ] as $name => $value ) 
     376//          } 
     377//  
     378//          self::import( "$dispatcher.php" ); 
     379        } 
     380 
     381        public static function service( $serviceName, $concept = false ) 
     382        { 
     383            if( isset( self::$services[ $serviceName ] ) ) 
     384                return self::$services[ $serviceName ]; 
     385 
     386            if( !isset(self::$config[ $serviceName ]) ) 
     387                 self::$config[ $serviceName ] = self::loadConfig( $serviceName, true ); 
     388 
     389            if( !isset(self::$config[ $serviceName ]) ) 
    61390                return( false ); 
    62391 
    63             return $this->import( $this->config[$type]["path"], 
    64                                                           $this->config[$type]["class"] ); 
    65         } 
    66  
    67         public function import( $path, $class ) 
    68         { 
    69             if( !$class ){ 
    70                 preg_match( "/^\/?.*\/([^\/]+).php$/", $path, $class ); 
    71                 $class = $class[1]; 
    72             } 
     392            if( !isset(self::$config[ $serviceName ]['type']) ) 
     393                self::$config[ $serviceName ]['type'] = 'service'; 
     394 
     395            self::import( 'app/'.self::$config[ $serviceName ]['type'] );   //TODO: Item 4 
     396 
     397            $service = self::load( self::$config[ $serviceName ]['path'], 
     398                                   self::$config[ $serviceName ]['class'] ); 
     399 
     400              $srvConfig = array(); 
     401 
     402            if( isset(self::$config[ $serviceName ][ 'config' ]) ) 
     403                $srvConfig = self::configure( $srvConfig, self::$config[ $serviceName ][ 'config' ] ); 
     404            if( $wallet = self::wallet( $serviceName ) ) 
     405                $srvConfig = self::configure( $srvConfig, $wallet ); 
     406            if( $concept && isset(self::$config[ $concept ]['service.config']) ) 
     407                $srvConfig = self::configure( $srvConfig, self::$config[ $concept ]['service.config'] ); 
     408 
     409            if( empty( $srvConfig ) ) 
     410                $srvConfig = false; 
     411 
     412            if( $service && self::$config[ $serviceName ]['type'] === 'service' ) 
     413                self::connect( $service, $srvConfig ); 
     414 
     415            return( self::$services[ $serviceName ] = $service ); 
     416        } 
     417 
     418        public static function interceptor( $method, $concept = false, $serviceName = false, $isService = false ) 
     419        { 
     420            if( $concept && !isset(self::$config[ $concept ]) ) 
     421              self::$config[ $concept ] = self::loadConfig( $concept ); 
     422 
     423            if( !$concept ) $concept = 'global'; 
     424            if( !$isService || !$serviceName ) $serviceName = 'global'; 
     425 
     426            if( !isset( self::$interceptors[ $concept ] ) ) 
     427                self::$interceptors[ $concept ] = array(); 
     428 
     429            if( !isset( self::$interceptors[ $concept ][ $serviceName ] ) ) 
     430                self::$interceptors[ $concept ][ $serviceName ] = array(); 
     431 
     432            if( !isset( self::$interceptors[ $concept ][ $serviceName ][ $method ] ) ) 
     433            { 
     434                $events = array( 'before', 'after' ); 
     435                $interceptors = array(); 
     436 
     437                $prefix = ( $isService )? "$serviceName." : ""; 
     438 
     439                foreach( $events as $i => $event ) 
     440                { 
     441                    $interceptors[$event] = array(); 
     442 
     443                    if( !isset(self::$config[$concept]["$prefix$event.$method"]) ) 
     444                      continue; 
     445 
     446                    foreach( self::$config[$concept]["$prefix$event.$method"] as $intercept => $interceptor ) 
     447                            $interceptors[$event][$intercept] = self::load( $interceptor ); 
     448                } 
     449 
     450                self::$interceptors[ $concept ][ $serviceName ][ $method ] = $interceptors; 
     451            } 
     452 
     453            return( self::$interceptors[ $concept ][ $serviceName ][ $method ] ); 
     454        } 
     455 
     456        public static function interceptorCommit( $eventType, $commitList, $isService = false ) 
     457        { 
     458            $result = array( $eventType => array() ); 
     459         
     460        if( is_array( $commitList ) ) 
     461                foreach( $commitList as $i => $tx ) 
     462                { 
     463                    $interceptors = self::interceptor( 'commit', $tx['concept'], $tx['service'], $isService ); 
     464       
     465                    $result[$eventType] = array_merge( $result[$eventType], $interceptors[$eventType] ); 
     466                } 
     467 
     468            return( $result ); 
     469        } 
     470 
     471        public static function fire( $eventType, $method, &$params, $original, $isService = false ) 
     472        { 
     473            if( $method === 'commit' ) 
     474                $interceptors = self::interceptorCommit( $eventType, $params['criteria'], $isService ); 
     475 
     476            else 
     477                $interceptors = self::interceptor( $method, 
     478                                                   isset($original['URI']['concept']) ? $original['URI']['concept'] : false, 
     479                                                   isset($params['URI']['service']) ? $params['URI']['service'] : false, $isService ); 
     480 
     481            if( $interceptors && isset($interceptors[ $eventType ]) ) 
     482                foreach( $interceptors[ $eventType ] as $intercept => $interceptor ) 
     483                { 
     484                    $return = $interceptor->$intercept( $params['URI'], $params['properties'], $params['criteria'], $original/*, $params['service']*/ ); 
     485 
     486                    if( $return === false ) 
     487                return( false ); 
     488 
     489                    if( isset($return) ) 
     490                        $params['properties'] = $return; 
     491                } 
     492 
     493              return( $params ); 
     494        } 
     495 
     496        /* 
     497          * ex: array 
     498          *             ( 
     499          *                     [0] array( 'OR', array( array( '=', 'campo', 'valor' ),  
     500                                                          array( 'OR', array( array( '=', 'campo', 'valor' ) ) ) ) 
     501          *                     [1] array( '=', 'campo' , 'valor' ) 
     502          *                     [2] array( 'OR' , array( array( '=' , campo', 'valor' ) ) ) 
     503          *                     [3] array( 'IN', 'campo', array( '1' , '2' , '3' ) ) 
     504          *             ) 
     505          * OR 
     506          *         array( '=' , 'campo' , 'valor' ) 
     507        */ 
    73508         
    74             if( !$this->includes[$path] ) 
    75             { 
    76                 include_once( $path ); 
    77                 $this->includes[ $path ] = true; 
    78             } 
    79  
    80             $object = $this->cache->get( $class ); 
    81              
    82             if( $object ) 
    83                 return( $object ); 
    84  
    85             $object = new $class(); 
    86             $this->cache->put( $class, $object ); 
    87  
    88             return( $object ); 
    89         } 
    90  
    91         public function clearAll() 
    92         { 
    93             return $this->cache->clearAll(); 
    94         } 
    95  
    96         public function clear( $id ) 
    97         { 
    98             return $this->cache->clear( $id ); 
    99         } 
    100  
    101         public function get( $id ) 
    102         { 
    103             return $this->cache->get( $id ); 
    104         } 
    105  
    106         public function put( $id, $data, $expires, $compressed ) 
    107         { 
    108             return $this->cache->put( $id, $data, $expires, $compressed ); 
    109         } 
    110  
    111         public function fireEvent( $event, $method, $params ) 
    112         { 
    113             if( !$this->config["$event.$method"] ) 
    114                 return( $params ); 
    115  
    116             $original = $params; 
    117  
    118             foreach( $this->config["$event.$method"] as $intercept => $interceptor ) 
    119             { 
    120                   if( is_string( $interceptor ) ) 
    121                       $this->config["$event.$method"][$intercept] = $interceptor = $this->import( $interceptor ); 
    122  
    123                   $return = $interceptor->$intercept( $original, $params ); 
    124  
    125                   if( $return === false ) 
    126                       return( false ); 
    127  
    128                   if( $return ) 
    129                       $params = $return; 
    130             } 
    131  
    132             return( $params ); 
    133         } 
    134  
    135         public function find( $params, $criteria = false ) 
    136         { 
    137  
    138             if( ($params = $this->fireEvent( "before", "find", $params )) === false ) 
    139                  return( false ); 
    140  
    141             if( $this->service ) 
     509        public static function serviceName( $URI, $original = false ) 
     510        { 
     511             $concept = ""; 
     512 
     513            if( $original && isset($original['concept']) && $original['concept'] ) 
     514                $concept = $original['concept']; 
     515            elseif( isset($URI['concept']) && $URI['concept'] ) 
     516                $concept = $URI['concept']; 
     517 
     518            if( ( !isset($URI['service']) || !$URI['service'] ) && $concept ) 
     519            { 
     520                if( !isset(self::$config[ $concept ]) ) 
     521                    self::$config[ $concept ] = self::loadConfig( $concept ); 
     522 
     523                $URI['service'] = self::$config[ $concept ][ 'service' ]; 
     524            } 
     525 
     526            if( !isset($URI['service']) ) 
     527                throw new Exception( "CONFIGURATION ERROR: service name from concept '$concept' not found" ); 
     528 
     529            return( $URI ); 
     530        } 
     531 
     532        public static function call( $method, $URI, $properties = false, $options = false, $service = false, $noTX = false ) 
     533        { 
     534            try 
     535            { 
     536                if( !isset($URI['concept']) ) $URI['concept'] = false; 
     537 
     538                $original = $params = array( 'properties' => $properties,  
     539                                             'criteria' => $options,  
     540                                             'URI' => $URI, 
     541                                             'service' => $service ); 
     542 
     543                if( isset($params['URI']['concept'])  && !self::fire( 'before', $method, $params, $original ) ) 
     544                    return( empty($params['properties']) ? false : $params['properties'] ); 
     545 
     546                if( $params && !$params['service'] ) 
     547                { 
     548                    $params['URI'] = self::serviceName( $params['URI'], $original['URI'] ); 
     549 
     550                    $params['service'] = self::service( $params['URI']['service'], $params['URI']['concept'] ); 
     551                } 
     552 
     553                if( isset($params['URI']['service']) ) 
     554                { 
     555                    if( $commit = (!isset(self::$tx[ $params['URI']['service'] ]) && ( $method === 'create' || 
     556                                                                                       $method === 'update' || 
     557                                                                                       $method === 'delete' )) ) 
     558                    { 
     559                        self::call( 'begin', $params['URI'] ); 
     560                        self::$tx[ $params['URI']['service'] ] = true; 
     561                    } 
     562 
     563                    if( !self::fire( 'before', $method, $params, $original, true ) ) 
     564                        return( empty($params['properties']) ? false : $params['properties'] ); 
     565                } 
     566 
     567                if( $params['service'] ) 
     568                    switch( $method ) 
    142569            {  
    143                 if( $this->target ) 
    144                   $result = $this->service->retrieve( $this->className, $this->target, $this->parents, $params, $criteria ); 
    145                 else 
    146                   $result = $this->service->find( $this->className, $this->parents, $params, $criteria ); 
    147  
    148                 if( $result ) 
    149                     $params = $result; 
    150             } 
    151  
    152             if( ($result = $this->fireEvent( "after", "find", $params )) === false ) 
    153                  return( false ); 
    154  
    155             $result = json_encode( $result ); 
    156  
    157             return( $result ); 
    158         } 
    159  
    160         public function update( $params, $criteria = false ) 
    161         { 
    162  
    163             if( !($params = $this->fireEvent( "before", "update", $params )) ) 
     570                        case 'find': $return = $params['service']->find( $params['URI'], $params['properties'], $params['criteria'] ); break; 
     571 
     572                        case 'read': $return = $params['service']->read( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
     573 
     574                        case 'create': $return = $params['service']->create( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
     575 
     576                        case 'delete': $return = $params['service']->delete( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
     577 
     578                        case 'deleteAll': $return = $params['service']->deleteAll( $params['URI'], $params['properties'], $params['criteria'] ); break; 
     579 
     580                        case 'update': $return = $params['service']->update( $params['URI'], $params['properties']/*, $criteria*/ ); break; 
     581 
     582                        case 'replace': $return = $params['service']->replace( $params['URI'], $params['properties'], $params['criteria'] ); break; 
     583 
     584                        case 'begin': $return = $params['service']->begin( $params['URI'] ); break; 
     585 
     586                        case 'commit': $return = $params['service']->commit( $params['URI'], $params['criteria'] ); break; 
     587 
     588                        case 'rollback': $return = $params['service']->rollback( $params['URI'], $params['criteria'] ); break; 
     589 
     590                        case 'parse': $return = $params['service']->parse( $params['properties'], $params['criteria'] ); break; 
     591 
     592                        case 'format': $return = $params['service']->format( $params['properties'], $params['criteria'] ); break; 
     593            } 
     594 
     595                if( isset($return) && $return !== false ) 
     596                    $params['properties'] = $return; 
     597 
     598                if( isset($params['URI']['service']) ) 
     599                { 
     600                    if( !self::fire( 'after', $method, $params, $original, true ) ) 
     601                        return( empty($params['properties']) ? false : $params['properties'] ); 
     602 
     603                    if( $commit ) 
     604                    { 
     605                        if( !self::call( 'commit', $params['URI'], false, self::$tx[ $params['URI']['service'] ] ) ) 
     606                            self::call( 'rollback', $params['URI'] , false, self::$tx[ $params['URI']['service'] ] ); 
     607 
     608                        unset( self::$tx[ $params['URI']['service'] ] ); 
     609                    } 
     610                } 
     611 
     612                if( isset($URI['concept']) && !self::fire( 'after', $method, $params, $original ) ) 
     613                    return( empty($params['properties']) ? false : $params['properties'] ); 
     614            } 
     615            catch( Exception $e ) 
     616            { 
     617                if( !self::fallback( $e ) ) 
     618                    self::closeAll(); 
     619 
    164620                return( false ); 
    165  
    166             if( $this->service ) 
    167             {  
    168                 if( is_string( $idOrfilter ) ) 
    169                     $result = $this->service->update( $this->className, $this->target, $this->parents, $params, $criteria ); 
    170                 else 
    171                     $result = $this->service->replace( $this->className, $this->parents, $params, $criteria ); 
    172  
    173                 if( $result ) 
    174                     $params = $result; 
    175             } 
    176  
    177             if( !($result = $this->fireEvent( "after", "update", $params )) ) 
    178                 return( false ); 
    179  
    180             $result = json_encode( $result ); 
    181  
    182             return( $result ); 
    183         } 
    184  
    185         public function delete( $params, $criteria = false ) 
    186         { 
    187             if( ($params = $this->fireEvent( "before", "delete", $params )) === false ) 
    188                 return( false ); 
    189                  
    190             if( $this->service ) 
    191             { 
    192                 if( is_string( $idOrfilter ) ) 
    193                     $result = $this->service->delete( $this->className, $this->target, $this->parents, $params, $criteria ); 
    194                 else 
    195                     $result = $this->service->deleteAll( $this->className, $this->parents, $params, $criteria ); 
    196  
    197                 if( $result ) 
    198                     $params = $result; 
    199             } 
    200  
    201             if( ($result = $this->fireEvent( "after", "delete", $params )) === false ) 
    202                 return( false ); 
    203  
    204             $result = json_encode( $result ); 
    205  
    206             return( $result ); 
    207         } 
    208  
    209         public function create( $params ) 
    210         { 
    211             if( ($params = $this->fireEvent( "before", "create", $params )) === false ) 
    212                 return( false ); 
    213  
    214             if( $this->service ) 
    215             {  
    216                 $result = $this->service->create( $this->className, $this->parents, $params ); 
    217  
    218                 if( $result ) 
    219                     $params = $result; 
    220             } 
    221  
    222             if( ($result = $this->fireEvent( "after", "create", $params )) === false ) 
    223                 return( false ); 
    224  
    225             $result = json_encode( $result ); 
    226  
    227             return( $result ); 
     621            } 
     622 
     623            return( empty($params['properties']) ? false : $params['properties'] ); 
     624        } 
     625 
     626        public static function fallback( $exception ) 
     627        { 
     628            error_log( $exception->getMessage() ); 
     629            return( true ); 
    228630        } 
    229631} 
     632 
     633Controller::$cache = Controller::loadCache(); 
    230634?> 
Note: See TracChangeset for help on using the changeset viewer.