divoxx-ruby-php-serialization 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,39 @@
2
2
 
3
3
  Ruby implementation of PHP's serialization. This is special useful for reading PHP session files.
4
4
 
5
- == Examples
5
+ == Unserialization examples
6
6
 
7
+ Primitive values
8
+
9
+ RubyPhpSerialization.php_unserialize('i:10;') # => 10
10
+ RubyPhpSerialization.php_unserialize('s:4:"Name";') # => "Name"
11
+ RubyPhpSerialization.php_unserialize('b:1;') # => true
12
+ RubyPhpSerialization.php_unserialize('N;') # => nil
13
+
14
+ Array
15
+
16
+ RubyPhpSerialization.php_unserialize('a:2:{i:0;b:1;i:1;s:3:"foo";};') # => [true, "foo"]
17
+
18
+ Hash
19
+
20
+ RubyPhpSerialization.php_unserialize('a:2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};') # => {"name" => "Rodrigo", "age" => 23}
21
+
22
+ Object
23
+
24
+ class Person
25
+ attr_accessor :name, :age
26
+ end
27
+
28
+ person = RubyPhpSerialization.php_unserialize('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
29
+ person.name # => "Rodrigo"
30
+ person.age # => 23
31
+
32
+ Object without class will map to a Struct
33
+
34
+ person = RubyPhpSerialization.php_unserialize('O:6:"Person":2:{s:4:"name";s:7:"Rodrigo";s:3:"age";i:23;};')
35
+ person.class # => Struct::Person
36
+ person.name # => "Rodrigo"
37
+ person.age # => 23
7
38
 
8
39
  == Copyright
9
40
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -1,2 +1,10 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
  require 'ruby_php_serialization/parser'
3
+
4
+ module RubyPhpSerialization
5
+ extend self
6
+
7
+ def php_unserialize(str)
8
+ Parser.new.parse(str)
9
+ end
10
+ end
@@ -11,7 +11,7 @@ require 'ruby_php_serialization/tokenizer'
11
11
  module RubyPhpSerialization
12
12
  class Parser < Racc::Parser
13
13
 
14
- module_eval(<<'...end parser.y/module_eval...', 'parser.y', 65)
14
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 69)
15
15
 
16
16
  def parse(string)
17
17
  @tokenizer = Tokenizer.new(string)
@@ -273,45 +273,49 @@ module_eval(<<'.,.,', 'parser.y', 27)
273
273
 
274
274
  module_eval(<<'.,.,', 'parser.y', 32)
275
275
  def _reduce_14(val, _values, result)
276
- result = Object.const_get(val[4]).new
277
-
278
- val[9].each do |(attr_name, value)|
279
- result.instance_variable_set("@#{attr_name}", value)
276
+ if Object.const_defined?(val[4])
277
+ result = Object.const_get(val[4]).new
278
+
279
+ val[9].each do |(attr_name, value)|
280
+ result.instance_variable_set("@#{attr_name}", value)
281
+ end
282
+ else
283
+ result = Struct.new(val[4], *val[9].map { |(k,v)| k.to_sym }).new(*val[9].map { |(k,v)| v })
280
284
  end
281
285
 
282
286
  result
283
287
  end
284
288
  .,.,
285
289
 
286
- module_eval(<<'.,.,', 'parser.y', 40)
290
+ module_eval(<<'.,.,', 'parser.y', 44)
287
291
  def _reduce_15(val, _values, result)
288
292
  result = val[0] << val[1]
289
293
  result
290
294
  end
291
295
  .,.,
292
296
 
293
- module_eval(<<'.,.,', 'parser.y', 41)
297
+ module_eval(<<'.,.,', 'parser.y', 45)
294
298
  def _reduce_16(val, _values, result)
295
299
  result = []
296
300
  result
297
301
  end
298
302
  .,.,
299
303
 
300
- module_eval(<<'.,.,', 'parser.y', 44)
304
+ module_eval(<<'.,.,', 'parser.y', 48)
301
305
  def _reduce_17(val, _values, result)
302
306
  @numeric_array = false unless val[0].is_a?(Integer); result = [val[0], val[2]]
303
307
  result
304
308
  end
305
309
  .,.,
306
310
 
307
- module_eval(<<'.,.,', 'parser.y', 47)
311
+ module_eval(<<'.,.,', 'parser.y', 51)
308
312
  def _reduce_18(val, _values, result)
309
313
  @numeric_array = true
310
314
  result
311
315
  end
312
316
  .,.,
313
317
 
314
- module_eval(<<'.,.,', 'parser.y', 49)
318
+ module_eval(<<'.,.,', 'parser.y', 53)
315
319
  def _reduce_19(val, _values, result)
316
320
  if @numeric_array
317
321
  result = []
@@ -30,10 +30,14 @@ rule
30
30
 
31
31
  object : 'O' ':' NUMBER ':' STRING ':' NUMBER ':' '{' attribute_list '}'
32
32
  {
33
- result = Object.const_get(val[4]).new
34
-
35
- val[9].each do |(attr_name, value)|
36
- result.instance_variable_set("@#{attr_name}", value)
33
+ if Object.const_defined?(val[4])
34
+ result = Object.const_get(val[4]).new
35
+
36
+ val[9].each do |(attr_name, value)|
37
+ result.instance_variable_set("@#{attr_name}", value)
38
+ end
39
+ else
40
+ result = Struct.new(val[4], *val[9].map { |(k,v)| k.to_sym }).new(*val[9].map { |(k,v)| v })
37
41
  end
38
42
  }
39
43
  ;
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-php-serialization}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rodrigo Kochenburger"]
@@ -30,7 +30,6 @@ Gem::Specification.new do |s|
30
30
  "lib/ruby_php_serialization/parser.rb",
31
31
  "lib/ruby_php_serialization/parser.y",
32
32
  "lib/ruby_php_serialization/tokenizer.rb",
33
- "lib/ruby_php_serialization/unserializer.rb",
34
33
  "ruby-php-serialization.gemspec",
35
34
  "spec/ruby_php_serialization_spec.rb",
36
35
  "spec/spec_helper.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: divoxx-ruby-php-serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Kochenburger
@@ -65,7 +65,6 @@ files:
65
65
  - lib/ruby_php_serialization/parser.rb
66
66
  - lib/ruby_php_serialization/parser.y
67
67
  - lib/ruby_php_serialization/tokenizer.rb
68
- - lib/ruby_php_serialization/unserializer.rb
69
68
  - ruby-php-serialization.gemspec
70
69
  - spec/ruby_php_serialization_spec.rb
71
70
  - spec/spec_helper.rb
@@ -1,4 +0,0 @@
1
- module RubyPhpSerialization
2
- class Unserializer
3
- end
4
- end