Permalink
Browse files

make this thing work better

  • Loading branch information...
tenderlove committed May 2, 2016
1 parent 80d3d99 commit a5496246cc2df5d977c9520172d83b38fe803f36
Showing with 43 additions and 13 deletions.
  1. +43 −13 lib/phuby/rails.rb
View
@@ -1,6 +1,6 @@
require 'phuby'
-class PHPHandler < ActionView::TemplateHandler
+class PHPHandler
class Events < Struct.new(:code, :headers, :body)
def write string; body << string; end
def send_headers response_code; end
@@ -22,25 +22,55 @@ def method_missing name
end
end
- def initialize view
- @controller = view.controller
+ attr_reader :filename
+
+ def initialize filename, controller
+ @controller = controller
@proxy = ControllerProxy.new @controller
+ @filename = filename
end
- def render template, *args
- filename = File.join template.load_path, template.template_path
+ def run
events = Events.new(200, {}, '')
- Dir.chdir(File.dirname(filename)) do
- Phuby::Runtime.php do |rt|
- rt.eval "date_default_timezone_set('America/Los_Angeles');"
- rt['at'] = @proxy
- rt.with_events(events) do
- open(filename) { |f| rt.eval f }
- end
+ rd, wr = IO.pipe
+
+ buf = ''
+ th = Thread.new do
+ Thread.current.abort_on_exception = true
+
+ while !rd.eof?
+ buf << rd.read
end
end
- events.body
+ # PHP messes up openssl, so lets fork to protect the main Ruby process.
+ pid = fork {
+ rd.close
+
+ Dir.chdir(File.dirname(filename)) do
+ Phuby::Runtime.php do |rt|
+ rt.eval "date_default_timezone_set('America/Los_Angeles');"
+ rt['at'] = @proxy
+ rt.with_events(events) do
+ open(filename) { |f| rt.eval f }
+ end
+ end
+ end
+
+ str = events.body
+ str.force_encoding 'utf-8'
+ str.scrub!
+ wr.write str
+ wr.close
+ }
+ wr.close
+ Process.waitpid pid
+ th.join
+ buf
+ end
+
+ def self.call template
+ "PHPHandler.new(#{template.identifier.inspect}, controller).run"
end
end

0 comments on commit a549624

Please sign in to comment.