Skip to content
Browse files

Fix struct serialization problem

  • Loading branch information...
1 parent d874a0a commit 561f52b1c82105b253cd618604bebc94e7224804 @divoxx committed Oct 5, 2009
View
5 lib/php_serialization/serializer.rb
@@ -23,6 +23,11 @@ def run(object)
"a:#{object.length}:{#{items}}"
else
klass_name = object.class.name
+
+ if klass_name =~ /^Struct::/ && php_klass = object.instance_variable_get("@_php_class")
+ klass_name = php_klass
+ end
+
attributes = object.instance_variables.map { |var_name| "#{run(var_name.gsub(/^@/, ''))}#{run(object.instance_variable_get(var_name))}" }
"O:#{klass_name.length}:\"#{klass_name}\":#{object.instance_variables.length}:{#{attributes}}"
end
View
18 lib/php_serialization/unserializer.rb
@@ -11,7 +11,7 @@
module PhpSerialization
class Unserializer < Racc::Parser
-module_eval(<<'...end unserializer.y/module_eval...', 'unserializer.y', 66)
+module_eval(<<'...end unserializer.y/module_eval...', 'unserializer.y', 68)
def initialize(tokenizer_klass = Tokenizer)
@tokenizer_klass = tokenizer_klass
end
@@ -267,49 +267,51 @@ def _reduce_12(val, _values, result)
module_eval(<<'.,.,', 'unserializer.y', 29)
def _reduce_13(val, _values, result)
- if Object.const_defined?(val[4])
+ if eval("defined?(#{val[4]})")
result = Object.const_get(val[4]).new
val[9].each do |(attr_name, value)|
result.instance_variable_set("@#{attr_name.gsub(/(^\*)|\0/, '')}", value)
end
else
- result = Struct.new(val[4], *val[9].map { |(k,v)| k.gsub(/(^\*)|\0/, '').to_sym }).new(*val[9].map { |(k,v)| v })
+ klass_name = val[4].gsub(/^Struct::/, '')
+ result = Struct.new(klass_name, *val[9].map { |(k,v)| k.gsub(/(^\*)|\0/, '').to_sym }).new(*val[9].map { |(k,v)| v })
+ result.instance_variable_set("@_php_class", klass_name)
end
result
end
.,.,
-module_eval(<<'.,.,', 'unserializer.y', 41)
+module_eval(<<'.,.,', 'unserializer.y', 43)
def _reduce_14(val, _values, result)
result = val[0] << val[1]
result
end
.,.,
-module_eval(<<'.,.,', 'unserializer.y', 42)
+module_eval(<<'.,.,', 'unserializer.y', 44)
def _reduce_15(val, _values, result)
result = []
result
end
.,.,
-module_eval(<<'.,.,', 'unserializer.y', 45)
+module_eval(<<'.,.,', 'unserializer.y', 47)
def _reduce_16(val, _values, result)
@numeric_array = false unless val[0].is_a?(Integer); result = [val[0], val[1]]
result
end
.,.,
-module_eval(<<'.,.,', 'unserializer.y', 48)
+module_eval(<<'.,.,', 'unserializer.y', 50)
def _reduce_17(val, _values, result)
@numeric_array = true
result
end
.,.,
-module_eval(<<'.,.,', 'unserializer.y', 50)
+module_eval(<<'.,.,', 'unserializer.y', 52)
def _reduce_18(val, _values, result)
if @numeric_array
result = []
View
6 lib/php_serialization/unserializer.y
@@ -27,14 +27,16 @@ rule
object : 'O' ':' NUMBER ':' STRING ':' NUMBER ':' '{' attribute_list '}'
{
- if Object.const_defined?(val[4])
+ if eval("defined?(#{val[4]})")
result = Object.const_get(val[4]).new
val[9].each do |(attr_name, value)|
result.instance_variable_set("@#{attr_name.gsub(/(^\*)|\0/, '')}", value)
end
else
- result = Struct.new(val[4], *val[9].map { |(k,v)| k.gsub(/(^\*)|\0/, '').to_sym }).new(*val[9].map { |(k,v)| v })
+ klass_name = val[4].gsub(/^Struct::/, '')
+ result = Struct.new(klass_name, *val[9].map { |(k,v)| k.gsub(/(^\*)|\0/, '').to_sym }).new(*val[9].map { |(k,v)| v })
+ result.instance_variable_set("@_php_class", klass_name)
end
}
;

0 comments on commit 561f52b

Please sign in to comment.
Something went wrong with that request. Please try again.