[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionWebService/Clients/ -> AkXmlRpcClient.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionWebservice
  13   * @subpackage Client
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  require_once(AK_VENDOR_DIR.DS.'incutio'.DS.'IXR_Library.inc.php');
  20  
  21  defined('AK_ACTION_WEBSERVICE_CACHE_REMOTE_METHODS') ? null : define('AK_ACTION_WEBSERVICE_CACHE_REMOTE_METHODS', AK_ENVIRONMENT == 'production');
  22  
  23  class AkXmlRpcClient extends IXR_Client
  24  {
  25      var $_RemoteObjects;
  26      var $options = array();
  27      var $errors = array();
  28      var $_IxrParameters = array();
  29      var $WebServiceClient;
  30      var $error_handler;
  31  
  32  
  33      function AkXmlRpcClient(&$WebServiceClient)
  34      {
  35          $this->WebServiceClient =& $WebServiceClient;
  36      }
  37  
  38      function init()
  39      {
  40          $options = func_get_args();
  41          $num_args = count($options);
  42  
  43          if(!empty($options[$num_args-1]) && is_array($options[$num_args-1])){
  44              $this->options = array_pop($options);
  45          }
  46  
  47          $default_options = array(
  48          'user_agent' => 'Akelos XML-RPC Client',
  49          'build' => true,
  50          'remote_object_prefix' => 'AkRemote_',
  51          'debug' => false,
  52          /**
  53           * @todo add a better cache system for remote methods
  54           */
  55           'cache_remote_methods' => isset($_SESSION) && AK_ACTION_WEBSERVICE_CACHE_REMOTE_METHODS
  56           );
  57  
  58           $this->options = array_merge($default_options, $this->options);
  59           $this->_setIxrParameters($options);
  60           $this->_getIxrInstance();
  61           $this->_addOptionsToIxrInstance();
  62  
  63           $this->options['class_name'] = empty($this->options['class_name']) ?
  64           'Remote_'.$this->_getIdForRequest() : $this->options['class_name'];
  65  
  66           if($this->options['build']){
  67               $this->_buildRemoteObjects();
  68           }
  69      }
  70  
  71      function _getIxrInstance()
  72      {
  73          call_user_func_array(array(&$this, 'IXR_Client'), $this->_getIxrOptions());
  74      }
  75  
  76      function _addOptionsToIxrInstance()
  77      {
  78          foreach ($this->options as $k=>$v){
  79              $k = strtolower(str_replace('_','',$k));
  80              $this->$k = $v;
  81          }
  82      }
  83  
  84      function _setIxrParameters($parameters = array())
  85      {
  86          $this->_IxrParameters = $parameters;
  87      }
  88  
  89      function _getIxrOptions()
  90      {
  91          return !empty($this->_IxrParameters) && is_array($this->_IxrParameters) ? $this->_IxrParameters : array();
  92      }
  93  
  94      function _buildRemoteObjects()
  95      {
  96          $methods = $this->_getRemoteMethods();
  97          $objects = array_keys($methods);
  98          foreach ($methods as $class=>$methods){
  99              $class_code = $this->_buildClass($class, $methods);
 100              eval(' ?>'.$class_code.'<?php ');
 101          }
 102  
 103          foreach ($objects as $object){
 104              $class_name = $this->options['remote_object_prefix'].$object;
 105              $remote_attribute_name = $object;
 106              $remote_attribute_name_camelized = ucfirst($object);
 107              $this->WebServiceClient->$remote_attribute_name =& new $class_name($this);
 108              $this->WebServiceClient->$remote_attribute_name_camelized =& $this->WebServiceClient->$remote_attribute_name;
 109          }
 110      }
 111  
 112      function _buildClass($class_name, $methods)
 113      {
 114          $reserved_words = array('and', 'as', 'break', 'case', 'cfunction', 'class', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'false', 'for', 'foreach', 'function', 'global', 'if', 'include', 'include_once', 'list', 'new', 'not', 'null', 'old_function', 'or', 'parent', 'print', 'require', 'require_once', 'return', 'static', 'stdclass', 'switch', 'true', 'var', 'virtual', 'while', 'xor');
 115  
 116          $class_methods = '';
 117          foreach ($methods as $method){
 118              $function_name = in_array(strtolower($method),$reserved_words) ? 'get'.ucfirst($method) : $method;
 119              $method_implementation = "
 120      \$args = func_num_args() > 0 ? func_get_args() : array();
 121      array_unshift(\$args, '$class_name.$method');
 122      if(!call_user_func_array(array(&\$this->XmlRpcClient,'query'), \$args)){
 123          \$this->addError(\$this->XmlRpcClient->getErrorCode().' : '.\$this->XmlRpcClient->getErrorMessage());
 124      }
 125      return !\$this->hasErrors() ? \$this->XmlRpcClient->getResponse() : false;
 126      ";
 127              $class_methods .= ' function '.$function_name."()\n    { $method_implementation \n    } ";
 128          }
 129  
 130          $ixr_options = $this->_getIxrOptions();
 131          foreach ($ixr_options as $option){
 132              $ixr_vars[] = var_export($option, true);
 133          }
 134  
 135  
 136          return "<?php \nclass {$this->options['remote_object_prefix']}$class_name \n{
 137          var \$XmlRpcClient;
 138          var \$errors;
 139      function {$this->options['remote_object_prefix']}$class_name(&\$XmlRpcClient)\n    { 
 140      \$this->XmlRpcClient =& new IXR_Client(".join(',', $ixr_vars).");
 141  
 142       foreach (\$XmlRpcClient->options as \$k=>\$v){
 143           \$k = strtolower(str_replace('_','',\$k));
 144           \$this->XmlRpcClient->\$k = \$v;
 145       }
 146      }
 147          function addError(\$error)\n    {\n        \$this->errors[\$error] = '';\n    }
 148          function hasErrors(){\n        return !empty(\$this->errors);\n    }
 149          function getErrors(){\n        return array_keys(\$this->errors);\n    } 
 150          function getMethods(){\n        return ".var_export($methods,true).";\n    }
 151          $class_methods 
 152  }\n?>";
 153      }
 154  
 155      function _getRemoteMethods()
 156      {
 157          if(isset($this->options['remote_methods'])){
 158              return $this->options['remote_methods'];
 159          }
 160          if($this->options['cache_remote_methods'] && isset($_SESSION['__XML-RPC_methods_for_'.$this->options['class_name']])){
 161              return $_SESSION['__XML-RPC_methods_for_'.$this->options['class_name']];
 162          }
 163  
 164          if (!$this->query('system.listMethods')) {
 165              $this->addError('Something went wrong - '.$this->getErrorCode().' : '.
 166              $this->getErrorMessage());
 167          }
 168  
 169          $_remote_methods = $this->getResponse();
 170  
 171          $remote_methods = array();
 172          if(is_array($_remote_methods)){
 173              foreach ($_remote_methods as $method){
 174                  $parts = explode('.', $method);
 175                  $remote_methods[array_shift($parts)][] = array_shift($parts);
 176              }
 177  
 178              if($this->options['cache_remote_methods']){
 179                  $_SESSION['__XML-RPC_methods_for_'.$this->options['class_name']] = $remote_methods;
 180              }
 181          }
 182          
 183          return $remote_methods;
 184      }
 185  
 186      function call()
 187      {
 188          $args = func_num_args() > 0 ? func_get_args() : array();
 189  
 190          if(!call_user_func_array(array(&$this,'query'), $args)){
 191              $this->addError($this->getErrorCode().' : '.$this->getErrorMessage());
 192          }
 193          return !$this->hasErrors() ? $this->getResponse() : false;
 194      }
 195  
 196  
 197      function addError($error)
 198      {
 199          $this->errors[$error] = '';
 200      }
 201  
 202      function hasErrors()
 203      {
 204          return !empty($this->errors);
 205      }
 206  
 207      function getErrors()
 208      {
 209          return array_keys($this->errors);
 210      }
 211  
 212      function _getIdForRequest()
 213      {
 214          return md5($this->server.$this->port.$this->path);
 215      }
 216  }
 217  
 218  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6