AmfSerializer manages the job of translating PHP objects into the actionscript equivalent via Amf.
The main method of the serializer is the serialize method which takes and AmfObject as it's argument and builds the resulting Amf Message.
| package | Amfphp_Core_Amf |
|---|
getOutput() : string
stringserialize(\Amfphp_Core_Amf_Packet $data) : String
The return type is noted as a String, but is a binary stream. echo it to the output buffer
\Amfphp_Core_Amf_Packet
StringgetAmf3Int(int $d) : string
note: There does not seem to be a way to distinguish between signed and unsigned integers. This method just sends the lowest 29 bit as-is, and the receiver is responsible to interpret the result as signed or unsigned based on some context.
note: The limit imposed by Amf3 is 29 bit. So in case the given integer is longer than 29 bit, only the lowest 29 bits will be serialised. No error will be logged!
| TODO | refactor into writeAmf3Int |
|---|
intthe integer to serialise
stringhandleReference(mixed $obj, array $references)
If it does, write it, and return true. If not, add it to the references array. Depending on whether or not the spl_object_hash function can be used ( available (PHP >= 5.2), and can only be used on an object) things are handled a bit differently: - if possible, objects are hashed and the hash is used as a key to the references array. So the array has the structure hash => reference - if not, the object is pushed to the references array, and array_search is used. So the array has the structure reference => object. maxing out the number of stored references improves performance(tested with an array of 9000 identical objects). This may be because isset's performance is linked to the size of the array. weird... note on using $references[$count] = &$obj; rather than $references[] = &$obj; the first one is right, the second is not, as with the second one we could end up with the following: some object hash => 0, 0 => array. (it should be 1 => array)
This also means that 2 completely separate instances of a class but with the same values will be written fully twice if we can't use the hash system
mixed
array
resetReferences()
Call before writing a body or a header, as the indices are local to each message body or header
writeAmf3AnonymousObject(\stdClass $d, \doReference $doReference)
In this way a reference to a class trait will have the right id.
| todo | it would seem that to create only one traits entry for an anonymous object would be the way to go. this however messes things up in both Flash and Charles Proxy. For testing call discovery service using AMF. investigate. |
|---|
\doReferenceBoolean This is used by writeAmf3Array, where the reference has already been taken care of, so there this method is called with false
writeAmf3Array(array $d)
array
writeAmf3Bool(bool $d) : \nothing
boolthe boolean to serialise
\nothingwriteAmf3ByteArray(\Amfphp_Core_Amf_Types_ByteArray $d)
writeAmf3Data(mixed $d)
| todo | no type markers ("\6', for example) in this method! |
|---|
mixed
writeAmf3Date(\Amfphp_Core_Amf_Types_Date $d)
writeAmf3Int(int $d) : \nothing
writeAmf3Null() : \nothing
\nothingwriteAmf3Number(\number $d)
\number
writeAmf3String(string $d) : \The
Strings are stored in a cache and in case the same string is written again, a reference to the string is sent instead of the string itself.
note: Sending strings larger than 268435455 (2^28-1 byte) will (silently) fail!
note: The string marker is NOT sent here and has to be sent before, if needed.
stringthe string to send
\Thereference index inside the lookup table is returned. In case of an empty
string which is sent in a special way, NULL is returned.writeAmf3TypedObject(object $d)
Type is determined by having an "explicit type" field. If this field is not set, call writeAmf3AnonymousObject write all properties as sealed members.
object
writeAmf3Undefined() : \nothing
\nothingwriteAmf3Xml(\Amfphp_Core_Amf_Types_Xml $d)
writeAmf3XmlDocument(\Amfphp_Core_Amf_Types_XmlDocument $d)
writeAnonymousObject(\stdClass $d)
writeArrayOrObject(array $d)
Then it either writes the array code (0x0A) or the object code (0x03) and then the associated data.
arrayThe php array
writeBoolean(bool $d)
boolThe boolean value
writeByte(int $b)
intAn int that can be converted to a byte
writeData(mixed $d)
mixedThe data
writeDate(\Amfphp_Core_Amf_Types_Date $d)
writeDouble(double $d)
Then if the system is big-endian, it reverses the bytes order because all doubles passed via remoting are passed little-endian.
doubleThe double to add to the output buffer
writeInt(int $n)
intAn integer to convert to a 2 byte binary string
writeLong(\long $l)
\longA long to convert to a 4 byte binary string
writeLongUtf(string $s)
It works exactly as writeUTF does except uses a long for the length flag.
stringA string to add to the byte stream
writeNull()
writeNumber(int $d)
intThe numeric data
writeObjectEnd()
writeObjectFromArray(array $d)
It does not write the object code as that is handled by the writeArrayOrObject and this method is shared with the CustomClass writer which doesn't use the object code.
arrayThe php array with string keys
writeReference(int $num)
int
writeString(string $d)
Note: strings are truncated to 64k max length. Use XML as type to send longer strings
stringThe string data
writeTypedObject(object $d)
To accomplish this we just blanket grab all of the object vars with get_object_vars, minus the Amfphp_Core_Amf_Constants::FIELD_EXPLICIT_TYPE field, whiuch is used as class name
objectThe object to serialize the properties. The deserializer looks for Amfphp_Core_Amf_Constants::FIELD_EXPLICIT_TYPE on this object and writes it as the class name.
writeUndefined()
writeUtf(string $s)
stringThe string less than 65535 characters to add to the stream
writeXML(\Amfphp_Core_Amf_Types_Xml $d)
$Amf0StoredObjects : array
$className2TraitsInfo : array
key: class name. value: array(reference id, array(property names))
$outBuffer : String
$packet : \Amfphp_Core_Amf_Packet
$storedObjects : array
$storedStrings : array
MAX_STORED_OBJECTS