/** * Write object to ouput stream * * @param mixed $data * @return Zend_Amf_Parse_Amf3_Serializer */ public function writeObject($object) { if ($this->writeObjectReference($object)) { return $this; } $className = ''; //Check to see if the object is a typed object and we need to change switch (true) { // the return class mapped name back to actionscript class name. case $className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): break; // Check to see if the user has defined an explicit Action Script type. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // Check if user has defined a method for accessing the Action Script type case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; // No return class name is set make it a generic object // No return class name is set make it a generic object case $object instanceof stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } $writeTraits = true; //check to see, if we have a corresponding definition if (array_key_exists($className, $this->_referenceDefinitions)) { $traitsInfo = $this->_referenceDefinitions[$className]['id']; $encoding = $this->_referenceDefinitions[$className]['encoding']; $propertyNames = $this->_referenceDefinitions[$className]['propertyNames']; $traitsInfo = $traitsInfo << 2 | 0x1; $writeTraits = false; } else { $propertyNames = array(); if ($className == '') { //if there is no className, we interpret the class as dynamic without any sealed members $encoding = Zend_Amf_Constants::ET_DYNAMIC; } else { $encoding = Zend_Amf_Constants::ET_PROPLIST; foreach ($object as $key => $value) { if ($key[0] != "_") { $propertyNames[] = $key; } } } $this->_referenceDefinitions[$className] = array('id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames); $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; $traitsInfo |= count($propertyNames) << 4; } $this->writeInteger($traitsInfo); if ($writeTraits) { $this->writeString($className); foreach ($propertyNames as $value) { $this->writeString($value); } } try { switch ($encoding) { case Zend_Amf_Constants::ET_PROPLIST: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case Zend_Amf_Constants::ET_DYNAMIC: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } //Write remaining properties foreach ($object as $key => $value) { if (!in_array($key, $propertyNames) && $key[0] != "_") { $this->writeString($key); $this->writeTypeMarker($value); } } //Write an empty string to end the dynamic part $this->writeString($this->_strEmpty); break; case Zend_Amf_Constants::ET_EXTERNAL: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('External Object Encoding not implemented'); break; default: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding); } } catch (Exception $e) { require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage(), 0, $e); } return $this; }
/** * Find if the class name is a class mapped name and return the * respective classname if it is. * * @param object $object * @return false|string $className */ protected function getClassName($object) { // // require_once 'Zend/Amf/Parse/TypeLoader.php'; //Check to see if the object is a typed object and we need to change $className = ''; switch (true) { // the return class mapped name back to actionscript class name. case Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): $className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)); break; // Check to see if the user has defined an explicit Action Script type. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // Check if user has defined a method for accessing the Action Script type case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; // No return class name is set make it a generic object // No return class name is set make it a generic object case $object instanceof stdClass: $className = ''; break; // By default, use object's class name // By default, use object's class name default: $className = get_class($object); break; } if (!$className == '') { return $className; } else { return false; } }
/** * Map from PHP type name to AS type name * * @param string $typename PHP type name * @return string AS type name */ protected function _phpTypeToAS($typename) { if (class_exists($typename)) { $vars = get_class_vars($typename); if (isset($vars['_explicitType'])) { return $vars['_explicitType']; } } if (false !== ($asname = Zend_Amf_Parse_TypeLoader::getMappedClassName($typename))) { return $asname; } return $typename; }
/** * Loads a remote class or method and executes the function and returns * the result * * @param string $method Is the method to execute * @param mixed $param values for the method * @return mixed $response the result of executing the method * @throws Zend_Amf_Server_Exception */ protected function _dispatch($method, $params = null, $source = null) { if ($source) { if (($mapped = Zend_Amf_Parse_TypeLoader::getMappedClassName($source)) !== false) { $source = $mapped; } } $qualifiedName = empty($source) ? $method : $source . '.' . $method; if (!isset($this->_table[$qualifiedName])) { // if source is null a method that was not defined was called. if ($source) { $className = str_replace('.', '_', $source); if (class_exists($className, false) && !isset($this->_classAllowed[$className])) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Can not call "' . $className . '" - use setClass()'); } try { $this->getLoader()->load($className); } catch (Exception $e) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Class "' . $className . '" does not exist: ' . $e->getMessage(), 0, $e); } // Add the new loaded class to the server. $this->setClass($className, $source); } if (!isset($this->_table[$qualifiedName])) { // Source is null or doesn't contain specified method require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Method "' . $method . '" does not exist'); } } $info = $this->_table[$qualifiedName]; $argv = $info->getInvokeArguments(); if (0 < count($argv)) { $params = array_merge($params, $argv); } if ($info instanceof Zend_Server_Reflection_Function) { $func = $info->getName(); $this->_checkAcl(null, $func); $return = call_user_func_array($func, $params); } elseif ($info instanceof Zend_Server_Reflection_Method) { // Get class $class = $info->getDeclaringClass()->getName(); if ('static' == $info->isStatic()) { // for some reason, invokeArgs() does not work the same as // invoke(), and expects the first argument to be an object. // So, using a callback if the method is static. $this->_checkAcl($class, $info->getName()); $return = call_user_func_array(array($class, $info->getName()), $params); } else { // Object methods try { $object = $info->getDeclaringClass()->newInstance(); } catch (Exception $e) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Error instantiating class ' . $class . ' to invoke method ' . $info->getName() . ': ' . $e->getMessage(), 621, $e); } $this->_checkAcl($object, $info->getName()); $return = $info->invokeArgs($object, $params); } } else { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Method missing implementation ' . get_class($info)); } return $return; }
/** * Add a class mapping and lookup the mapping to make sure * the mapping succeeds */ public function testClassMap() { $this->_server->setClassMap('controller.test', 'Zend_Amf_testclass'); $className = Zend_Amf_Parse_TypeLoader::getMappedClassName('Zend_Amf_testclass'); $this->assertEquals('controller.test', $className); }
public function writeObject($object) { if ($this->writeObjectReference($object)) { return $this; } $className = ''; switch (true) { case $className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): break; case isset($object->_explicitType): $className = $object->_explicitType; break; case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; case $object instanceof stdClass: $className = ''; break; default: $className = get_class($object); break; } $writeTraits = true; if (array_key_exists($className, $this->_referenceDefinitions)) { $traitsInfo = $this->_referenceDefinitions[$className]['id']; $encoding = $this->_referenceDefinitions[$className]['encoding']; $propertyNames = $this->_referenceDefinitions[$className]['propertyNames']; $traitsInfo = $traitsInfo << 2 | 0x1; $writeTraits = false; } else { $propertyNames = array(); if ($className == '') { $encoding = Zend_Amf_Constants::ET_DYNAMIC; } else { $encoding = Zend_Amf_Constants::ET_PROPLIST; foreach ($object as $key => $value) { if ($key[0] != "_") { $propertyNames[] = $key; } } } $this->_referenceDefinitions[$className] = array('id' => count($this->_referenceDefinitions), 'encoding' => $encoding, 'propertyNames' => $propertyNames); $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; $traitsInfo |= count($propertyNames) << 4; } $this->writeInteger($traitsInfo); if ($writeTraits) { $this->writeString($className); foreach ($propertyNames as $value) { $this->writeString($value); } } try { switch ($encoding) { case Zend_Amf_Constants::ET_PROPLIST: foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case Zend_Amf_Constants::ET_DYNAMIC: foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } foreach ($object as $key => $value) { if (!in_array($key, $propertyNames) && $key[0] != "_") { $this->writeString($key); $this->writeTypeMarker($value); } } $this->writeString(''); break; case Zend_Amf_Constants::ET_EXTERNAL: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('External Object Encoding not implemented'); break; default: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding); } } catch (Exception $e) { require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage(), 0, $e); } return $this; }
/** * Loads a remote class or method and executes the function and returns * the result * * @param string $method Is the method to execute * @param mixed $param values for the method * @return mixed $response the result of executing the method * @throws Zend_Amf_Server_Exception */ protected function _dispatch($method, $params = null, $source = null) { if ($source) { if (($mapped = Zend_Amf_Parse_TypeLoader::getMappedClassName($source)) !== false) { $source = $mapped; } } $qualifiedName = empty($source) ? $method : $source . "." . $method; if (!isset($this->_table[$qualifiedName])) { // if source is null a method that was not defined was called. if ($source) { $classPath = array(); $path = explode('.', $source); $className = array_pop($path); $uriclasspath = implode('/', $path); // Take the user supplied directories and add the unique service path to the end. foreach ($this->_directories as $dir) { $classPath[] = $dir . $uriclasspath; } require_once 'Zend/Loader.php'; try { Zend_Loader::loadClass($className, $classPath); } catch (Exception $e) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Class "' . $className . '" does not exist'); } // Add the new loaded class to the server. $this->setClass($className, $source); } else { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Method "' . $method . '" does not exist'); } } $info = $this->_table[$qualifiedName]; $argv = $info->getInvokeArguments(); if (0 < count($argv)) { $params = array_merge($params, $argv); } if ($info instanceof Zend_Server_Reflection_Function) { $func = $info->getName(); $this->_checkAcl(null, $func); $return = call_user_func_array($func, $params); } elseif ($info instanceof Zend_Server_Reflection_Method) { // Get class $class = $info->getDeclaringClass()->getName(); if ('static' == $info->isStatic()) { // for some reason, invokeArgs() does not work the same as // invoke(), and expects the first argument to be an object. // So, using a callback if the method is static. $this->_checkAcl($class, $info->getName()); $return = call_user_func_array(array($class, $info->getName()), $params); } else { // Object methods try { $object = $info->getDeclaringClass()->newInstance(); } catch (Exception $e) { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Error instantiating class ' . $class . ' to invoke method ' . $info->getName(), 621); } $this->_checkAcl($object, $info->getName()); $return = $info->invokeArgs($object, $params); } } else { require_once 'Zend/Amf/Server/Exception.php'; throw new Zend_Amf_Server_Exception('Method missing implementation ' . get_class($info)); } return $return; }
/** * Test that adding our own mappping will result in it being added to the classMap * */ public function testSetMappingClass() { Zend_Amf_Parse_TypeLoader::setMapping('com.example.vo.Contact', 'Contact'); $class = Zend_Amf_Parse_TypeLoader::getMappedClassName('com.example.vo.Contact'); $this->assertEquals('Contact', $class); }
/** * Write object to ouput stream * * @param mixed $data * @return Zend_Amf_Parse_Amf3_Serializer */ public function writeObject($object) { $encoding = Zend_Amf_Constants::ET_PROPLIST; $className = ''; //Check to see if the object is a typed object and we need to change switch (true) { // the return class mapped name back to actionscript class name. case $className = Zend_Amf_Parse_TypeLoader::getMappedClassName(get_class($object)): break; // Check to see if the user has defined an explicit Action Script type. // Check to see if the user has defined an explicit Action Script type. case isset($object->_explicitType): $className = $object->_explicitType; break; // Check if user has defined a method for accessing the Action Script type // Check if user has defined a method for accessing the Action Script type case method_exists($object, 'getASClassName'): $className = $object->getASClassName(); break; // No return class name is set make it a generic object // No return class name is set make it a generic object default: break; } $traitsInfo = Zend_Amf_Constants::AMF3_OBJECT_ENCODING; $traitsInfo |= $encoding << 2; try { switch ($encoding) { case Zend_Amf_Constants::ET_PROPLIST: $count = 0; foreach ($object as $key => $value) { if ($key[0] != "_") { $count++; } } $traitsInfo |= $count << 4; // Write the object ID $this->writeInteger($traitsInfo); // Write the classname $this->writeString($className); // Write the object Key's to the output stream foreach ($object as $key => $value) { if ($key[0] != "_") { $this->writeString($key); } } //Write the object values to the output stream. foreach ($object as $key => $value) { if ($key[0] != "_") { $this->writeTypeMarker($value); } } break; case Zend_Amf_Constants::ET_EXTERNAL: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('External Object Encoding not implemented'); break; default: require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unknown Object Encoding type: ' . $encoding); } } catch (Exception $e) { require_once 'Zend/Amf/Exception.php'; throw new Zend_Amf_Exception('Unable to writeObject output: ' . $e->getMessage()); } return $this; }