Permalink
Browse files

Merge branch 'master' of github.com:tenderlove/phuby

* 'master' of github.com:tenderlove/phuby:
  switching to rack::utils
  cleaning up the php handler
  a rack adapter for phuby
  arrays move across
  • Loading branch information...
2 parents c71ae05 + 6c8d350 commit 334bb3d0d628efd1f241c7c856cc553d975ef0b0 @zenspider zenspider committed Oct 26, 2009
Showing with 121 additions and 2 deletions.
  1. +90 −0 bin/phrack
  2. +16 −2 ext/phuby/phuby_conversions.c
  3. +9 −0 test/test_array.rb
  4. +6 −0 test/test_object.rb
View
90 bin/phrack
@@ -0,0 +1,90 @@
+#!/usr/bin/env ruby
+
+require 'phuby'
+require 'rack'
+require 'rack/showexceptions'
+require 'logger'
+
+###
+# Rack::Phrack is a Rack handler that will evaulate and serve PHP files.
+#
+# The handler creates a new PHP runtime if it detects that a PHP file has
+# been requested.
+class Rack::Phrack < Rack::File
+ class Events < Struct.new(:code, :headers, :body)
+ def write string
+ body << string
+ end
+
+ def header value, op
+ k, v = value.split(': ', 2)
+ case k
+ when 'Location'
+ self.code = 302
+ v = Rack::Utils.unescape v
+ when 'Set-Cookie'
+ v = [headers[k], v].compact.join "\n"
+ end
+ headers[k] = v
+ end
+
+ def send_headers response_code
+ end
+ end
+
+ def call env
+ events = Events.new 200, {}, ''
+ file = File.join(@root, env['PATH_INFO'])
+ file = File.join(file, "index.php") if File.directory?(file)
+
+ return super unless file =~ /php$/
+
+ Dir.chdir(File.dirname(file)) do
+ Phuby::Runtime.php do |rt|
+
+ rt['_LOGGER'] = Logger.new($stderr)
+
+ # Set the timezone. *shrug*
+ rt.eval("date_default_timezone_set('America/Los_Angeles');")
+
+ # Set the GET parameters in the PHP runtime
+ params(env['QUERY_STRING']) { |k,v| rt["_GET"][k] = v }
+
+ # Set the POST parameters in the PHP runtime
+ params(env['rack.input'].read) { |k,v| rt["_POST"][k] = v }
+
+ # Set the cookies in the PHP runtime
+ env['HTTP_COOKIE'].split('; ').each do |pair|
+ k, v = pair.split '='
+ rt["_COOKIE"][k] = Rack::Utils.unescape v
+ end
+
+ # Set other misc info.
+ env.each { |k,v|
+ next if k =~ /^rack/
+ rt['_SERVER'][k] = v || ''
+ }
+ rt["_SERVER"]['REQUEST_URI'] = env['PATH_INFO']
+
+ # Evaluate the PHP file
+ rt.with_events(events) do
+ File.open(file, 'rb') { |f| rt.eval f }
+ end
+ end
+ end
+ events.to_a
+ end
+
+ private
+ def params string
+ string.split('&').each do |pair|
+ yield pair.split('=').map { |x| Rack::Utils.unescape(x) }
+ end
+ end
+end
+
+if $0 == __FILE__
+ Rack::Handler::WEBrick.run(
+ Rack::ShowExceptions.new(Rack::Phrack.new(ARGV[0] || Dir.pwd)),
+ :Port => 10101)
+end
View
18 ext/phuby/phuby_conversions.c
@@ -1,4 +1,4 @@
-#include <phuby_conversions.h>
+#include <phuby.h>
zval * Phuby_value_to_zval(VALUE rt, VALUE value)
{
@@ -26,17 +26,31 @@ zval * Phuby_value_to_zval(VALUE rt, VALUE value)
ZVAL_STRINGL(php_value, StringValuePtr(value), RSTRING_LEN(value), 1);
break;
case T_OBJECT:
+ case T_DATA:
{
object_init_ex(php_value, php_ruby_proxy);
VALUE map = rb_iv_get(rt, "@proxy_map");
rb_hash_aset(map, INT2NUM((int)php_value), value);
}
break;
+ case T_ARRAY:
+ {
+ array_init(php_value);
+ int i;
+ for(i = 0; i < RARRAY_LEN(value); i++) {
+ VALUE key = rb_funcall(INT2NUM(i), rb_intern("to_s"), 0);
+ VALUE thing = RARRAY_PTR(value)[i];
+ add_assoc_zval(php_value, StringValuePtr(key), Phuby_value_to_zval(rt, thing));
+ }
+ VALUE map = rb_iv_get(rt, "@proxy_map");
+ rb_hash_aset(map, INT2NUM((int)php_value), value);
+ }
+ break;
case T_NIL:
ZVAL_NULL(php_value);
break;
default:
- rb_raise(rb_eRuntimeError, "Can't convert ruby object: %d", TYPE(value));
+ rb_raise(rb_eRuntimeError, "Can't convert ruby object: %s %d", rb_class2name(CLASS_OF(value)), TYPE(value));
}
return php_value;
View
9 test/test_array.rb
@@ -1,6 +1,15 @@
require 'helper'
class TestArray < Phuby::TestCase
+ def test_move_to_runtime
+ Phuby::Runtime.php do |rt|
+ rt['foo'] = [1,2,3]
+ assert_equal 1, rt['foo'][0]
+ assert_equal 2, rt['foo'][1]
+ assert_equal 3, rt['foo'][2]
+ end
+ end
+
def test_array_length
Phuby::Runtime.php do |rt|
rt.eval('$get_length = count($_GET);')
View
6 test/test_object.rb
@@ -17,6 +17,12 @@ def value x
end
end
+ def test_stringio
+ Phuby::Runtime.php do |rt|
+ rt['x'] = StringIO.new('')
+ end
+ end
+
def test_method_call
x = FunObject.new
Phuby::Runtime.php do |rt|

0 comments on commit 334bb3d

Please sign in to comment.