Example #1
0
 /**
  * IPugin interface impl.
  *
  * setup() must be called before load
  *
  * @error 15404
  * @return integer Returns error code due to the initialization.
  */
 function setup()
 {
     //extract service configuration
     $this->serviceConfig = xapp_get_option(self::SERVICE_CONF, $this);
     //logging
     if (xapp_is_option(self::LOGGING_CONF, $this) && $this->serviceConfig) {
         $logConfig = xapp_get_option(self::SERVICE_CONF);
         if ($logConfig && $logConfig[XC_CONF_LOGGER] != null) {
             $this->logger = $logConfig[XC_CONF_LOGGER];
         } else {
             //setup logger
         }
     }
     //cache
     if (xapp_is_option(self::CACHE_CONF, $this) && $this->serviceConfig) {
         $cacheConfig = xapp_get_option(self::CACHE_CONF);
         if ($cacheConfig) {
             $this->cache = Xapp_Cache::instance($this->CACHE_NS, "file", array(Xapp_Cache_Driver_File::PATH => xapp_get_option(XC_CONF_CACHE_PATH, $this->serviceConfig), Xapp_Cache_Driver_File::CACHE_EXTENSION => $this->CACHE_NS, Xapp_Cache_Driver_File::DEFAULT_EXPIRATION => 200));
         }
     }
 }
Example #2
0
 /**
  * factory method for creating cache driver instances. this method is the only way, besides the instance method,
  * to create cache driver instances since concrete driver classes dont permit instantiation directly.
  * first parameter expects the driver string like "file" or "apc" and the second parameter
  * an driver options xapp array or option object. the third parameter can contain a namespace
  * identifier when if set will allow for unlimited instance creation. will throw error if
  * the driver does not exist
  *
  * @error 15302
  * @param string $driver expects the cache driver string
  * @param null|mixed $options expects xapp option array or object
  * @param null|string $ns expects optional ns string identifier
  * @return Xapp_Cache_Driver concrete xapp cache driver implementation instance
  * @throws Xapp_Cache_Exception
  */
 public static function factory($driver, $options = null, $ns = null)
 {
     $class = __CLASS__ . '_Driver_' . ucfirst(trim((string) $driver));
     if (class_exists($class, true)) {
         if ($ns !== null) {
             return self::$_instance = self::$_instances[trim((string) $ns)] = new $class($options);
         } else {
             return self::$_instance = new $class($options);
         }
     } else {
         throw new Xapp_Cache_Exception("cache driver: {$driver} does not exist", 1530201);
     }
 }
Example #3
0
 /**
  * invoke class/method or function via rpc or outside rpc functionality for testing purposes. the first parameter
  * thus can be a valid callable when used outside of rpc service or the internal array containing all call parameters
  * from concrete rpc server implementation. throws exception with extended exception properties if in debug mode.
  * summarizes all caught applications exceptions when invoking callable to a general application error so sensitive
  * error messages can be omitted. when invoked with named parameters will reorder parameters to reflect
  * order of method/function call since call_user_func_array needs to pass parameter named or unnamed in
  * correct order
  *
  * @error 14211
  * @param array|callable $call expects either valid callable or array with all call parameters
  * @param array $params expects the parameters to pass to function/method to invoke
  * @return mixed
  * @throws Xapp_Rpc_Server_Exception
  * @throws Xapp_Rpc_Fault
  * @throws Exception
  */
 public function invoke($call, $params = null)
 {
     $key = null;
     $hash = null;
     $class = null;
     $return = null;
     $result = null;
     $callable = null;
     try {
         //invoke from outside of rpc service
         if (is_callable($call)) {
             $callable = $call;
             if (is_array($call)) {
                 $call = array(null, $call[1], is_object($call[0]) ? get_class($call[0]) : $call[0]);
                 $class = new ReflectionClass($call[2]);
             } else {
                 if (strpos((string) $call, '::') !== false) {
                     $call = array(null, substr($call, strpos($call, '::') + 2), substr($call, 0, strpos($call, '::')));
                     $class = new ReflectionClass($call[2]);
                 } else {
                     $call = array(null, $call, $call);
                     $class = null;
                 }
             }
         } else {
             if (is_array($call)) {
                 if ($call[2] !== null) {
                     if (xapp_get_option(self::NAMESPACE_IDENTIFIER, $this) === NAMESPACE_SEPARATOR) {
                         if (strpos($call[2], NAMESPACE_SEPARATOR) !== false) {
                             $call[2] = NAMESPACE_SEPARATOR . str_replace(array('\\', '/', '_', '.'), xapp_get_option(self::NAMESPACE_IDENTIFIER, $this), trim($call[2], ' ' . NAMESPACE_SEPARATOR));
                         } else {
                             $call[2] = str_replace(array('/', '_', '.'), xapp_get_option(self::NAMESPACE_IDENTIFIER, $this), trim($call[2]));
                         }
                     } else {
                         $call[2] = str_replace(array('\\', '/', '_', '.'), xapp_get_option(self::NAMESPACE_IDENTIFIER, $this), trim($call[2]));
                     }
                     $key = "{$call[2]}.{$call[1]}";
                     try {
                         if (array_key_exists(strtolower($call[2]), $this->_objects)) {
                             $class = new ReflectionClass($this->_objects[strtolower($call[2])]);
                         } else {
                             $class = new ReflectionClass($call[2]);
                         }
                         if ($class->hasMethod($call[1])) {
                             $method = $class->getMethod($call[1]);
                             if ($method->isPublic()) {
                                 if ($method->isStatic()) {
                                     $callable = array($class->getName(), $call[1]);
                                 } else {
                                     $callable = array(array_key_exists(strtolower($call[2]), $this->_objects) ? $this->_objects[strtolower($call[2])] : $class->newInstance(), $call[1]);
                                 }
                             } else {
                                 Xapp_Rpc_Fault::t("method: {$call[1]} of class: {$call[2]} is not public", array(1421105, -32601));
                             }
                         } else {
                             Xapp_Rpc_Fault::t("method: {$call[1]} of class: {$call[2]} does not exist", array(1421104, -32601));
                         }
                     } catch (ReflectionException $e) {
                         throw new Xapp_Rpc_Server_Exception(xapp_sprintf(_("unable to initialize class due to reflection error: %d, %s"), $e->getCode(), $e->getMessage()), 1421103);
                     }
                 } else {
                     $key = $call[1];
                     $callable = $call[1];
                 }
             } else {
                 throw new Xapp_Rpc_Server_Exception(_("invalid callable passed to invoke method"), 1421106);
             }
         }
         if (is_callable($callable)) {
             if (is_array($callable) && is_object($callable[0])) {
                 $this->_class = $callable[0];
             }
             if (!is_null($key) && !is_null($params) && array_values((array) $params) !== (array) $params) {
                 $tmp = array();
                 $arr = (array) $params;
                 $map = $this->smd()->get($key);
                 if (!is_null($map)) {
                     foreach ((array) $map->parameters as $p) {
                         if (array_key_exists($p->name, $arr)) {
                             $tmp[$p->name] = $arr[$p->name];
                         }
                     }
                 }
                 $params = $tmp;
                 $tmp = null;
                 $arr = null;
                 $map = null;
             }
             if (!is_null($key) && xapp_is_option(self::CACHE, $this) && preg_match(Xapp_Rpc::regex(xapp_get_option(self::CACHE_SERVICES, $this)), $call[0])) {
                 if (xapp_get_option(self::CACHE_BY_TRANSACTION_ID, $this)) {
                     $hash = $call[3]['id'];
                 } else {
                     $hash = Xapp_Cache::hash(serialize($call[0]) . serialize($params));
                 }
                 if (xapp_get_option(self::CACHE, $this)->has($hash)) {
                     return xapp_get_option(self::CACHE, $this)->get($hash);
                 }
             }
             if (!xapp_get_option(self::PARAMS_AS_ARRAY, $this)) {
                 $params = xapp_array_to_object($params);
             }
             xapp_event('xapp.rpc.server.beforeCall', array($this, array(&$result, $call[1], $call[2], $params)));
             if ($this->hasClass() && $class->implementsInterface('Xapp_Rpc_Interface_Callable')) {
                 $return = $this->getClass()->onBeforeCall($this, array(&$result, $call[1], $call[2], &$params));
             }
             if ($return !== false && $result === null) {
                 $this->response()->result($result = call_user_func_array($callable, (array) $params));
             }
             if ($return === false && $this->hasClass() && $class->implementsInterface('Xapp_Rpc_Interface_Callable')) {
                 $return = $this->getClass()->onAbort($this, array(&$result, $call[1], $call[2], &$params));
             } else {
                 $return = null;
             }
             if ($return !== null) {
                 $result = $return;
             }
             if ($this->hasClass() && $class->implementsInterface('Xapp_Rpc_Interface_Callable')) {
                 $return = $this->getClass()->onAfterCall($this, array(&$result));
             }
             if ($return !== null) {
                 $result = $return;
             }
             xapp_event('xapp.rpc.server.afterCall', array($this, array(&$result)));
             if (!is_null($hash)) {
                 if (!xapp_get_option(self::CACHE, $this)->has($hash)) {
                     xapp_get_option(self::CACHE, $this)->set($hash, $result);
                 }
             }
             $callable = null;
             $return = null;
             $params = null;
             $class = null;
             $hash = null;
             return $result;
         } else {
             throw new Xapp_Rpc_Server_Exception(_("unable to invoke function since first argument is not a callable"), 1421102);
         }
     } catch (Exception $e) {
         if ($this->hasClass() && $class->implementsInterface('Xapp_Rpc_Interface_Callable')) {
             $error = $this->getClass()->onError($this, $e);
             if ($error instanceof Exception) {
                 $e = $error;
                 $error = null;
             }
         }
         if (!$e instanceof Xapp_Rpc_Server_Exception) {
             $data = array();
             $debug = xapp_get_option(self::DEBUG, $this);
             if (is_bool($debug) && $debug === true || is_int($debug) && $debug === 1) {
                 $data['message'] = $e->getMessage();
                 $data['code'] = $e->getCode();
                 $data['class'] = get_class($e);
                 $data['file'] = $e->getFile();
                 $data['line'] = $e->getLine();
                 if ($e instanceof ErrorException) {
                     $data['severity'] = $e->getSeverity();
                 }
                 if ($e instanceof Xapp_Rpc_Fault && $e->hasData()) {
                     $data['data'] = $e->getData();
                 }
             } else {
                 if (is_int($debug) && $debug === 2) {
                     $data['message'] = $e->getMessage();
                     $data['code'] = $e->getCode();
                     if ($e instanceof ErrorException) {
                         $data['severity'] = $e->getSeverity();
                     }
                     if ($e instanceof Xapp_Rpc_Fault && $e->hasData()) {
                         $data['data'] = $e->getData();
                     }
                 }
             }
             if (xapp_is_option(self::APPLICATION_ERROR, $this)) {
                 if ($e instanceof Xapp_Rpc_Fault && $e->hasFault()) {
                     $f = $e->getFault();
                 } else {
                     $f = -32500;
                 }
                 if (($code = (int) $e->getCode()) !== 0) {
                     Xapp_Rpc_Fault::t(xapp_sprintf("application error: %d", array($code)), array(1421101, $f), XAPP_ERROR_IGNORE, $data);
                 } else {
                     Xapp_Rpc_Fault::t("application error", array(1421101, $f), XAPP_ERROR_IGNORE, $data);
                 }
             } else {
                 if ((bool) $debug) {
                     Xapp_Rpc_Fault::t($e->getMessage(), $e->getCode(), XAPP_ERROR_IGNORE, $data);
                 } else {
                     throw $e;
                 }
             }
         } else {
             throw $e;
         }
     }
     return null;
 }
Example #4
0
 private function setup()
 {
     $this->serviceConfig = xapp_get_option(self::SERVICE_CONF, $this);
     //cache
     if (xapp_is_option(self::CACHE_CONF, $this) && $this->serviceConfig) {
         $cacheConfig = xapp_get_option(self::CACHE_CONF);
         if ($cacheConfig) {
             $this->cache = Xapp_Cache::instance("PluginManager", "file", array(Xapp_Cache_Driver_File::PATH => xapp_get_option(XC_CONF_CACHE_PATH, $this->serviceConfig), Xapp_Cache_Driver_File::CACHE_EXTENSION => "pmmanager", Xapp_Cache_Driver_File::DEFAULT_EXPIRATION => 200));
         }
     }
 }
Example #5
0
 /**
  * xapp cache short cut function for using the caching capabilities of xapp in a intuitive way. this function is
  * expecting at least one cache instance create with xapp cache class in order to work. if xapp cache class is
  * not xapped and has no instance registered will return null or false depending on get or set way of calling.
  * this function will combines the three stages of caching has, get, set. check if a cache entry exists, get the
  * entry and if not set it. the shortcut function will work with or without namespace identifier. if you want to
  * use the current instance use the function like:
  * <code>
  *      //set
  *      xapp_cache($key, $value);
  *      //get
  *      xapp_cache($key);
  * </code>
  *
  * if you want to refer to your instances create with namespace identifiers do it like:
  * <code>
  *      //set
  *      xapp_cache('ns1', $key, $value);
  *      //get
  *      xapp_cache('ns1', $key);
  * </code>
  *
  * the third parameter is the crucial one. if you use anything other then null as value will set cache values
  * regardless of your intention! the best way to use the cache short cut function is like:
  * <code>
  *      //check and get
  *      if(($result = xapp_cache('ns1', 'test')) === false)
  *      {
  *          //set since not cached yet
  *          $result = xapp_cache('ns1', 'test', array("foo" => "bar"), 60);
  *      }
  * </code>
  *
  * you can also use the shortcut function to purge/remove all expired cache items calling the function like:
  * <code>
  *      xapp_cache();
  * </code>
  *
  * overwrite like:
  * <code>
  *      function xapp_cache($with, $key, $value = null, $lifetime = null)
  *      {
  *          //your custom code here
  *      }
  * </code>
  *
  * @param null|string $with expects a namespace identifier or null for targeting current instance
  * @param string $key expects the cache key string
  * @param null|mixed $value expects the to be cached value when setting
  * @param null|int $lifetime expects the optional lifetime value
  * @return null|mixed
  */
 function xapp_cache($with, $key, $value = null, $lifetime = null)
 {
     if (xapped() && xapped('Xapp_Cache', false)) {
         if (Xapp_Cache::hasInstance()) {
             if (func_num_args() > 0) {
                 if (func_num_args() >= 3 && $value !== null) {
                     Xapp_Cache::set($with, $key, $value, $lifetime);
                     return $value;
                 } else {
                     return Xapp_Cache::get($with, $key, func_num_args() === 2 ? false : null);
                 }
             } else {
                 return Xapp_Cache::purge();
             }
         }
     }
     return func_num_args() === 2 ? false : null;
 }