package com.ericfeminella.rpc
{
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import mx.rpc.IResponder;
/**
*
* Provides a generic base class API for creating AMFPHP
* gateway connections and performing remote procedure
* calls (RPC) on remote PHP objects.
*
* <p>
* Classes which extend <code>AMFPHPRemoteGateway</code>
* implement <code>mx.rpc.IResponder</code> in order to
* handle both result and fault events from RPC responses
* </p>
*
* @example Below is a basic usage example:
*
* <listing version="3.0">
* package
* {
* import com.ericfeminella.rpc.AMFPHPRemoteGateway;
* import mx.rpc.IResponder;
*
* public class TestGateway extends AMFPHPRemoteGateway implements IResponder
* {
* public function TestGateway(gateway:String)
* {
* super(gateway, this);
* }
*
* public function result(data:Object) : void
* {
* // handle result
* }
*
* public function fault(info:Object) : void
* {
* // handle fault
* }
* }
* }
*
* </listing>
*
*/
public class AMFPHPRemoteGateway extends NetConnection
{
/**
*
* Defines the gateway URI in which remote objects
* are to be resolved
*
*/
protected var gateway:String;
/**
*
* Defines the remote class object in which RPC invocations
* are to be executed
*
*/
protected var remoteClass:String;
/**
*
* Defines the <code>IResponder</code> instance from which
* remote procedure call (RPC) results or faults are forwarded
*
* @see mx.rpc.IResponder
*
*/
protected var responder:IResponder;
/**
*
* Instantiates a new instance of <code>AMFPHPRemoteGateway</code>
* and sets objectEncoding to ActionScript Message Format 3 (AMF3).
*
* @example
* <listing verison="3.0">
*
* var gateway:String = "http://localhost/amfphp/gateway.php";
*
* </listing>
*
* @param AMFPHP gateway.php URI
*
*/
public function AMFPHPRemoteGateway(gatewayURI:String, responder:IResponder)
{
this.objectEncoding = ObjectEncoding.AMF3;
this.gateway = gatewayURI;
this.responder = responder;
connect( gatewayURI );
}
/**
*
* Retrieves the class in which remote method invocations
* are invoked
*
* @return remote class which has been registered
*
*/
public function get remoteServiceClass() : String
{
return remoteClass;
}
/**
*
* Sets the class in which remote method invocation is to be
* made. You must include the fully qualified classpath.
*
* <p>
* For example, if a remote class EmailDelegate.php resides
* in a package data.business, you must set remoteServiceClass
* to the exact same path:
* </p>
*
* <p>
* The remoteServiceClass can be set at runtime, therefore a
* single instance of AMFPHPRemoteGateway may call methods on
* various objects
* </p>
*
* @param The remote class which is to be invoked
*
*/
public function set remoteServiceClass(remoteClass:String) : void
{
this.remoteClass = remoteClass;
}
/**
*
* Retrieves the current gateway URI for this instance of
* <code>AMFPHPRemoteGateway</code>
*
* @return the gateway URI
*
*/
public function get gatewayURI() : String
{
return gateway;
}
/**
*
* Performs an rpc method invocation on a remote PHP object
*
* @param the method to invoke on the remote object
* @param a parameter of arbitrary length
*
*/
public function invoke(method:String, ...args) : void
{
if ( connected )
{
var responder:Responder = new Responder( responder.result, responder.fault );
var method:String = remoteClass + "." + method;
super.call.apply( null, [method, responder].concat(args) );
}
else
{
throw new Error("Object is disconnected: Remote Procedure Calls can not be issued");
}
}
}
}