cvs: pear /XML_RPC2/XML/RPC2 CachedServer.php
| From: | Fabien MARTY | Date: | Mon, 23 Jan 2006 22:30:48 +0000 |
| Subject: | cvs: pear /XML_RPC2/XML/RPC2 CachedServer.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-37732@lists.php.net to get a copy of this message | ||
fab Mon Jan 23 22:30:48 2006 UTC
Modified files:
/pear/XML_RPC2/XML/RPC2 CachedServer.php
Log:
lot of fixes and improvements
http://cvs.php.net/viewcvs.cgi/pear/XML_RPC2/XML/RPC2/CachedServer.php?r1=1.4&r2=1.5&diff_format=u
Index: pear/XML_RPC2/XML/RPC2/CachedServer.php
diff -u pear/XML_RPC2/XML/RPC2/CachedServer.php:1.4 pear/XML_RPC2/XML/RPC2/CachedServer.php:1.5
--- pear/XML_RPC2/XML/RPC2/CachedServer.php:1.4 Sat Jan 14 14:22:45 2006
+++ pear/XML_RPC2/XML/RPC2/CachedServer.php Mon Jan 23 22:30:48 2006
@@ -33,14 +33,14 @@
* @author Fabien MARTY <fab@php.net>
* @copyright 2005-2006 Fabien MARTY
* @license http://www.gnu.org/copyleft/lesser.html LGPL
License 2.1
-* @version CVS: $Id: CachedServer.php,v 1.4 2006/01/14 14:22:45 fab Exp $
+* @version CVS: $Id: CachedServer.php,v 1.5 2006/01/23 22:30:48 fab Exp $
* @link http://pear.php.net/package/XML_RPC2
*/
// }}}
// dependencies {{{
-require_once('Cache/Lite/Output.php');
+require_once('Cache/Lite.php');
// }}}
/**
@@ -114,6 +114,20 @@
* @var array
*/
private $_options = array();
+
+ /**
+ * "cache debug" flag (for debugging the caching process)
+ *
+ * @var boolean
+ */
+ private $_cacheDebug = false;
+
+ /**
+ * encoding
+ *
+ * @var string
+ */
+ private $_encoding = 'iso-8859-1';
// }}}
// {{{ setCacheOptions()
@@ -142,7 +156,7 @@
$array['lifetime'] = 3600; // we need a default lifetime
}
$this->_cacheOptions = $array;
- $this->_cacheObject = new Cache_Lite_Output($this->_cacheOptions);
+ $this->_cacheObject = new Cache_Lite($this->_cacheOptions);
}
// }}}
@@ -160,8 +174,18 @@
$this->_setCacheOptions($cacheOptions);
unset($options['cacheOptions']);
}
+ if (isset($options['cacheDebug'])) {
+ $this->_cacheDebug = $options['cacheDebug'];
+ unset($options['cacheDebug']); // 'cacheDebug' is not a standard
option for XML/RPC2/Server
+ }
$this->_options = $options;
$this->_callTarget = $callTarget;
+ if (isset($this->_options['encoding'])) {
+ $this->_encoding = $this->_options['encoding'];
+ }
+ if (isset($this->_options['prefix'])) {
+ $this->_prefix = $this->_options['prefix'];
+ }
}
// }}}
@@ -190,6 +214,23 @@
*/
public function handleCall()
{
+ $response = $this->getResponse();
+ $encoding = 'iso-8859-1';
+ if (isset($this->_options['encoding'])) {
+ $encoding = $this->_options['encoding'];
+ }
+ header('Content-type: text/xml; charset=' . $encoding);
+ header('Content-length: '.strlen($response));
+ print $response;
+ }
+
+ /**
+ * get the XML response of the XMLRPC server
+ *
+ * @return string the XML response
+ */
+ public function getResponse()
+ {
if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
$methodName = $this->_parseMethodName($GLOBALS['HTTP_RAW_POST_DATA']);
} else {
@@ -197,26 +238,52 @@
}
$weCache = $this->_cacheByDefault;
$lifetime = $this->_cacheOptions['lifetime'];
+ if ($this->_cacheDebug) {
+ if ($weCache) {
+ print "CACHE DEBUG : default values => weCache=true,
lifetime=$lifetime\n";
+ } else {
+ print "CACHE DEBUG : default values => weCache=false,
lifetime=$lifetime\n";
+ }
+ }
if ($methodName) {
// work on reflection API to search for @xmlrpc.caching tags into PHPDOC comments
list($weCache, $lifetime) = $this->_reflectionWork($methodName);
+ if ($this->_cacheDebug) {
+ if ($weCache) {
+ print "CACHE DEBUG : phpdoc comments => weCache=true,
lifetime=$lifetime\n";
+ } else {
+ print "CACHE DEBUG : phpdoc comments => weCache=false,
lifetime=$lifetime\n";
+ }
+ }
}
- if ($weCache) {
+ if (($weCache) and ($lifetime!=-1)) {
if (isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
$cacheId = $this->_makeCacheId($GLOBALS['HTTP_RAW_POST_DATA']);
} else {
$cacheId = 'norawpostdata';
}
- $this->_cacheObject = new Cache_Lite_Output($this->_cacheOptions);
+ $this->_cacheObject = new Cache_Lite($this->_cacheOptions);
$this->_cacheObject->setLifetime($lifetime);
- if (!($this->_cacheObject->start($cacheId, $this->_defaultCacheGroup))) {
+ if ($data = $this->_cacheObject->get($cacheId, $this->_defaultCacheGroup)) {
+ // cache id hit
+ if ($this->_cacheDebug) {
+ print "CACHE DEBUG : cache is hit !\n";
+ }
+ } else {
// cache is not hit
- $this->_workWithoutCache();
- $this->_cacheObject->end();
+ if ($this->_cacheDebug) {
+ print "CACHE DEBUG : cache is not hit !\n";
+ }
+ $data = $this->_workWithoutCache();
+ $this->_cacheObject->save($data);
}
} else {
- $this->_workWithoutCache();
+ if ($this->_cacheDebug) {
+ print "CACHE DEBUG : we don't cache !\n";
+ }
+ $data = $this->_workWithoutCache();
}
+ return $data;
}
// }}}
@@ -237,7 +304,7 @@
$className = get_class($this->_callTarget);
}
$class = new ReflectionClass($className);
- $method = $class->getMethod($methodName); // FIXME : problem with prefix ?
+ $method = $class->getMethod($methodName);
$docs = explode("\n", $method->getDocComment());
foreach ($docs as $i => $doc) {
$doc = trim($doc, " \r\t/*");
@@ -267,13 +334,15 @@
/**
* Parse the method name from the raw XMLRPC client request
*
+ * NB : the prefix is removed from the method name
+ *
* @param string $request raw XMLRPC client request
* @return string method name
*/
private function _parseMethodName($request)
{
// TODO : change for "simplexml"
- $res = ereg('<methodName>([a-zA-Z0-9\.,\/]*)</methodName>', $request,
$results);
+ $res = ereg('<methodName>' . $this->_prefix .
'([a-zA-Z0-9\.,\/]*)</methodName>', $request, $results);
if ($res>0) {
return $results[1];
}
@@ -285,12 +354,14 @@
/**
* Do the real stuff if no cache available
+ *
+ * @return string the response of the real XML/RPC2 server
*/
private function _workWithoutCache()
{
require_once('XML/RPC2/Server.php');
$this->_serverObject = XML_RPC2_Server::create($this->_callTarget,
$this->_options);
- $this->_serverObject->handleCall();
+ return $this->_serverObject->getResponse();
}
// }}}