Zend_Amf addDirectory() / Namespace issues

Zend_Amf addDirectory() / Namespace issues

I decided to convert one of my projects from AMFPHP to Zend_Amf.   I ran into one problem.  If you use $server->addDirectory(), this function expects your service class names to conform to the Zend class naming convention.  If you’re not familiar to Zend, then for example, I had to rename one of my classes from ‘Authentication’ to ‘Com_Domain_Authentication’ (because it resides in the com/domain/ directory) to get it to work.  This is because Zend_Amf_Server class uses the Zend_Loader_PluginLoader class to load your service classes.

I should also note that this problem also occurs because I’m using namespaces in my actionScript RemoteOjbect source (source=’com.domain.Authentication’).

Rather than try to modify/extend the Zend_Loader_PluginLoader, I wrote a quick hack on the Zend_Amf_Server class.   Here it is:

 try {
      $sourceArray = explode('.',$source);
      $className = array_pop($sourceArray);
      $stdDir = implode(DIRECTORY_SEPARATOR,$sourceArray);
      $prefix = $this->getLoader()->getPaths();
      Zend_Loader::loadClass($className, $prefix[''][0] . $stdDir);
 } catch (Exception $e) {

As you can see, I commented out the original line $this->getLoader()->load($className) and added the hack below. This was done on roughly line 308 in Server.php (version 1.8.4 patch 1) of the Zend_Amf_Server class. Please note that this hack only works for adding a single service diretory using ‘$server->addDirectory()’. Look at ‘$prefix[”][0]’ and you’ll see why (I didn’t say it was pretty). A simple loop should fix that if you need to expand. You’ll now be able to use $server->addDirectory() w/o having to modify your service class names or namespaces.