webservice/library/WsScore/WrappedServiceProxy.php
2010-08-30 07:49:44 +00:00

79 lines
2.3 KiB
PHP

<?php
class WrappedService_Proxy
{
protected $_className;
protected $_classInstance = null;
protected $_wrappedParts = false;
/**
* WrappedService_Proxy creates an intermediate (proxy) class between the SOAP server
* and the actual handling class, allowing pre-processing of function arguments and return values.
*
* The idea is to have a proxy class between SoapServer and the actual service class.
* This proxy is be able to intercept calls via the __call() magic method, to pre-process arguments
* and the return value appropriately (wrap/unwrap). Instead of using setClass() on Zend_Soap_Server,
* the user would have to do the following:
* $proxy = new WrappedService_Proxy('WrappedService', array(), array('wrappedParts' => true));
* $server->setObject($proxy);
*
* @param string $className name of the handling class to proxy.
* @param array $classArgs arguments used to instantiate the handling class.
* @param array $options proxy options.
*/
public function __construct($className, $classArgs = array(), $options = array())
{
$class = new ReflectionClass($className);
$constructor = $class->getConstructor();
if ($constructor === null) {
$this->_classInstance = $class->newInstance();
} else {
$this->_classInstance = $class->newInstanceArgs($classArgs);
}
$this->_className = $className;
$this->_setOptions($options);
}
protected function _setOptions($options)
{
foreach ($options as $key => $value) {
switch ($key) {
case 'wrappedParts':
$this->_wrappedParts = $value;
break;
default:
break;
}
}
}
protected function _getOptions()
{
$options = array();
$options['wrappedParts'] = $this->_wrappedParts;
return $options;
}
protected function _preProcessArguments($name, $arguments)
{
if ($this->_wrappedParts && count($arguments) == 1 && is_object($arguments[0])) {
return get_object_vars($arguments[0]);
} else {
return $arguments;
}
}
protected function _preProcessResult($name, $result)
{
if ($this->_wrappedParts) {
return array($name.'Result' => $result);
} else {
return $result;
}
}
public function __call($name, $arguments)
{
$result = call_user_func_array(array($this->_classInstance, $name), $this->_preProcessArguments($name, $arguments));
return $this->_preProcessResult($name, $result);
}
}