/** * 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) { //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 Parser\TypeLoader::getMappedClassName(get_class($object)): $className = Parser\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; } }
/** * Add a class mapping and lookup the mapping to make sure * the mapping succeeds */ public function testClassMap() { $this->_server->setClassMap('controller.test', 'ZendTest\\Amf\\TestAsset\\Server\\testclass'); $className = Parser\TypeLoader::getMappedClassName('ZendTest\\Amf\\TestAsset\\Server\\testclass'); $this->assertEquals('controller.test', $className); }
/** * Test that adding our own mappping will result in it being added to the classMap * */ public function testSetMappingClass() { Parser\TypeLoader::setMapping('com.example.vo.Contact', 'ZendAmfTest\\TestAsset\\Contact'); $class = Parser\TypeLoader::getMappedClassName('com.example.vo.Contact'); $this->assertEquals('ZendAmfTest\\TestAsset\\Contact', $class); }
/** * 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 Exception\ExceptionInterface */ protected function _dispatch($method, $params = null, $source = null) { if ($source) { if (($mapped = Parser\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])) { throw new Exception\RuntimeException('Can not call "' . $className . '" - use setClass()'); } try { $this->getBroker()->getClassLoader()->load($className); } catch (\Exception $e) { throw new Exception\RuntimeException('Class "' . $className . '" does not exist: ' . $e->getMessage(), 0, $e); } // Add the new loaded class to the server. $this->setClass($className, $source); } else { throw new Exception\BadMethodCallException('Method "' . $method . '" does not exist'); } } $info = $this->table[$qualifiedName]; $argv = $info->getInvokeArguments(); if (0 < count($argv)) { $params = array_merge($params, $argv); } if ($info instanceof Reflection\ReflectionFunction) { $func = $info->getName(); $this->_checkAcl(null, $func); $return = call_user_func_array($func, $params); } elseif ($info instanceof Reflection\ReflectionMethod) { // 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) { throw new Exception\RuntimeException('Error instantiating class ' . $class . ' to invoke method ' . $info->getName() . ': ' . $e->getMessage(), 621, $e); } $this->_checkAcl($object, $info->getName()); $return = $info->invokeArgs($object, $params); } } else { throw new Exception\BadMethodCallException('Method missing implementation ' . get_class($info)); } return $return; }
/** * Write object to ouput stream * * @param mixed $data * @return Serializer * @throws Parser\Exception\OutOfBoundsException */ 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 = Parser\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 = Amf\Constants::ET_DYNAMIC; } else { $encoding = 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 = 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 Amf\Constants::ET_PROPLIST: //Write the sealed values to the output stream. foreach ($propertyNames as $key) { $this->writeTypeMarker($object->{$key}); } break; case 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 Amf\Constants::ET_EXTERNAL: throw new Parser\Exception\OutOfBoundsException('External Object Encoding not implemented'); break; default: throw new Parser\Exception\OutOfBoundsException('Unknown Object Encoding type: ' . $encoding); } } catch (\Exception $e) { throw new Parser\Exception\OutOfBoundsException('Unable to writeObject output: ' . $e->getMessage(), 0, $e); } return $this; }
/** * 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 = \ZendAmf\Parser\TypeLoader::getMappedClassName($typename))) { return $asname; } return $typename; }