All pastes #182800 Raw Edit

dexen

public text v1 · immutable
#182800 ·published 2006-09-26 11:17 UTC
rendered paste body
<?php



/** Class providing simplified Transactions which default to cleanup on destruction.
	Performs cleanup on both Exception and function return. */
class Trans {


	protected $onCommit;
	protected $onCParms;
	protected $onDestruct;
	protected $onDestructStr;
	protected $onDParms;
	
	
	function puts( $str ) {
		$this->onDestructStr = $str;
	}


	function __destruct() {
		if ( $onDestructStr )
			print $onDestructStr;
		else
			call_user_func_array( $this->onDestruct, $this->onDParms );
	}


	function rollback() {
		call_user_func_array( $this->onDestruct, $this->onDParms );
	}


	function commit() {
		if ( $this->onCommit )
			call_user_func_array( $this->onCommit, $this->onCParms );
		else
			throw new InvalidArgumentException( 'No rule to commit.' );
	}


	/** Cancels pending cleanup. */
	function disarm( $a = NULL ) {
		if ( $a !== NULL ) {
			foreach ( $a as $c )
				$c->disarm();
			return; 	}

		$this->onDestruct = NULL;
		$this->onDestructStr = NULL;
		$this->onDParms = array();
	}


	/** DB::Q( \p $q ) */
	function sql( $q, $p = NULL ) {
		if ( $p !== NULL )
			return new Trans( array('DB','q'), array( $q ), array('DB','q'), (array)$p );
		return new Trans( array('DB','q'), array( $q ) );
	}


	/** hm */
	protected function __construct( $callback, $parms = array(), $commitCB=NULL, $commitParms = array() ) {
		$this->onDestruct = $callback;
		$this->onDParms = $parms;
		$this->onCommit = $commitCB;
		$this->onCParms = $commitParms;
	}


}



?>