Changeset 5437 for trunk/prototype/api/controller.php
- Timestamp:
- 01/27/12 15:37:21 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/prototype/api/controller.php
r5415 r5437 63 63 return self::$cache->put( $id, $data, $expires, $compressed ); 64 64 } 65 66 public static function put( $URI, $data, $txIds = false )67 {68 try69 {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 else101 $postpone[$linkTarget] = $data[$linkName];102 }103 }104 }105 else106 $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 65 168 66 public static function find( $URI, $params = false, $criteria = false ) … … 462 360 $result = array( $eventType => array() ); 463 361 464 362 if( is_array( $commitList ) ) 465 363 foreach( $commitList as $i => $tx ) 466 364 { … … 536 434 537 435 //TODO: Compatibilizar as configs relativas aos modulos, adicionando os mesmos nas options passadas 538 public static function call( $method, $URI, $properties = false, $options = false, $service = false , $noTX = false)436 public static function call( $method, $URI, $properties = false, $options = false, $service = false ) 539 437 { 540 438 try … … 550 448 return( empty($params['properties']) ? false : $params['properties'] ); 551 449 450 451 552 452 if( $params && !$params['service'] ) 553 453 { … … 559 459 if( isset($params['URI']['service']) ) 560 460 { 561 if( $commit = (!isset(self::$tx[ $params['URI']['service'] ]) && ( $method === 'create' || 562 $method === 'update' || 563 $method === 'delete' )) ) 461 if( $method === 'create' || $method === 'update' || $method === 'delete' ) 564 462 { 565 self::call( 'begin', $params['URI'] ); 566 self::$tx[ $params['URI']['service'] ] = true; 463 if( $commit = !isset(self::$tx[ $params['URI']['service'] ]) ) 464 { 465 self::call( 'begin', $params['URI'] ); 466 } 467 468 $TX = array(); 567 469 } 568 470 … … 573 475 if( $params['service'] ) 574 476 switch( $method ) 575 {477 { 576 478 case 'find': $return = $params['service']->find( $params['URI'], $params['properties'], $params['criteria'] ); break; 577 479 … … 596 498 case 'parse': $return = $params['service']->parse( $params['properties'], $params['criteria'] ); break; 597 499 500 case 'analize': $return = $params['service']->analize( $params['properties'], $params['criteria'] ); break; 501 598 502 case 'format': $return = $params['service']->format( $params['properties'], $params['criteria'] ); break; 599 600 default: $return = $params['service']->$method( $params['properties'], $params['criteria'] ); break;601 }503 504 default : $return = $params['service']->$method( $params['properties'], $params['criteria'] ); 505 } 602 506 603 507 if( isset($return) && $return !== false ) … … 605 509 606 510 if( isset($params['URI']['service']) ) 607 {608 511 if( !self::fire( 'after', $method, $params, $original, true ) ) 609 512 return( empty($params['properties']) ? false : $params['properties'] ); 610 513 611 if( $commit ) 514 if( isset($URI['concept']) ) 515 self::fire( 'after', $method, $params, $original ); 516 517 if( empty($params['properties']) ) 518 $params['properties'] = false; 519 520 if( isset( $TX ) ) 521 { 522 $TX['rollback'] = !!!$params['properties']; 523 524 if( $params['properties'] && isset($params['properties']['id']) ) 525 $TX['id'] = $params['properties']['id']; 526 527 self::$tx[ $params['URI']['service'] ][] = array_merge( $TX, $original['URI'], array( 'service' => $params['URI']['service'], 'method' => $method ) ); 528 529 if( isset($commit) && $commit ) 612 530 { 613 531 if( !self::call( 'commit', $params['URI'], false, self::$tx[ $params['URI']['service'] ] ) ) … … 617 535 } 618 536 } 619 620 if( isset($URI['concept']) && !self::fire( 'after', $method, $params, $original ) )621 return( empty($params['properties']) ? false : $params['properties'] );622 537 } 623 538 catch( Exception $e ) … … 629 544 } 630 545 631 return( empty($params['properties']) ? false : $params['properties'] );546 return( $params['properties'] ); 632 547 } 633 548 … … 637 552 return( true ); 638 553 } 554 555 public static function put( $URI, $data, $txIds = false ) 556 { 557 try 558 { 559 $URI = self::serviceName( $URI ); 560 561 if( $commit = !$txIds ) 562 $txIds = array(); 563 564 if( !isset( self::$tx[ $URI['service'] ] ) ) 565 { 566 self::call( 'begin', $URI ); 567 self::$tx[ $txIds[] = $URI['service'] ] = array(); 568 } 569 570 $method = $data ? isset( $data['id'] ) ? 571 'update' : 'create' : 'delete'; 572 573 $links = self::links( $URI['concept'] ); 574 575 $order = self::$txID++; 576 577 $postpone = array(); 578 579 if( $data ) 580 { 581 $URI['id'] = isset( $data['id'] ) ? $data['id'] : false; 582 583 foreach( $links as $linkName => $linkTarget ) 584 { 585 if( isset( $data[$linkName] ) && is_array( $data[$linkName] ) ) 586 { 587 if( self::isConcept( $linkName ) ) 588 $data[$linkName] = self::put( array( 'concept' => $linkTarget ), $data[$linkName], &$txIds ); 589 else 590 $postpone[$linkTarget] = $data[$linkName]; 591 } 592 } 593 } 594 else 595 $URI['id'] = isset( $data['id'] ) ? $data['id'] : $URI['id']; 596 597 $result = Controller::call( $method, $URI, $data, false, false, true ); 598 599 if( !is_bool( $result ) && !is_string( $result ) && isset( $result['id'] ) ) 600 $URI['id'] = $result['id']; 601 602 self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['order'] = $order; 603 self::$tx[ $URI['service'] ][ count(self::$tx[ $URI['service'] ]) - 1 ]['id'] = $URI['id']; 604 605 foreach( $postpone as $linkTarget => $dt ) 606 foreach( $dt as $ii => $value ) 607 { 608 if( !is_array( $value ) ) 609 $value = array( 'id' => $value ); 610 611 $value[ $URI['concept'] ] = $URI['id']; 612 613 self::put( array( 'concept' => $linkTarget ), $value, &$txIds ); 614 } 615 616 if( $commit ) 617 { 618 $result = array(); 619 620 for( $i = count( $txIds ) - 1; $i >= 0; $i-- ) 621 { 622 $currentTx = self::$tx[ $txIds[$i] ]; 623 unset( self::$tx[ $txIds[$i] ] ); 624 625 if( !self::commit( array( 'service' => $txIds[$i] ), $currentTx ) ) 626 { 627 self::rollback( array( 'service' => $txIds[$i] ), $currentTx ); 628 629 foreach( $currentTx as $i => $st ) 630 $currentTx[$i][ 'rollback' ] = true; 631 } 632 633 $result = array_merge( $result, $currentTx ); 634 } 635 636 self::$txID = 0; 637 638 return( $result ); 639 } 640 641 } 642 catch( Exception $e ) 643 { 644 if( !self::fallback( $e ) ) 645 self::closeAll(); 646 647 return( false ); 648 } 649 650 return( $URI['id'] ); 651 } 652 653 public static function get() 654 { 655 656 } 639 657 } 640 658
Note: See TracChangeset
for help on using the changeset viewer.