Skip to content

Commit

Permalink
Added open3.
Browse files Browse the repository at this point in the history
Added PHPExcel-example.
  • Loading branch information
kaspernj committed May 8, 2012
1 parent 4723cdd commit 46746d4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -49,3 +49,5 @@ pkg
#*.rbc

Gemfile.lock
examples/PHPExcel
examples/example_phpexcel.xlsx
42 changes: 42 additions & 0 deletions examples/example_phpexcel.rb
@@ -0,0 +1,42 @@
#!/usr/bin/env ruby

#Remember to install 'php5-cli' like under Ubuntu: apt-get install php5-cli
#Load 'Php_process' through RubyGems.
require "rubygems"
require "php_process"
php = Php_process.new

#Load PHPExcel (can be downloaded here: 'http://phpexcel.codeplex.com/releases/view/45412')
php.func("require_once", "#{File.dirname(__FILE__)}/PHPExcel/PHPExcel.php")

#Create new PHPExcel object
print "#{Time.now} Create new PHPExcel object\n"
objPHPExcel = php.new("PHPExcel")

#Set properties
print "#{Time.now} Set properties\n"
objPHPExcel.getProperties.setCreator("Maarten Balliauw")
objPHPExcel.getProperties.setLastModifiedBy("Maarten Balliauw")
objPHPExcel.getProperties.setTitle("Office 2007 XLSX Test Document")
objPHPExcel.getProperties.setSubject("Office 2007 XLSX Test Document")
objPHPExcel.getProperties.setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")

#Add some data
print "#{Time.now} Add some data\n"
objPHPExcel.setActiveSheetIndex(0);
objPHPExcel.getActiveSheet.SetCellValue('A1', 'Hello')
objPHPExcel.getActiveSheet.SetCellValue('B2', 'world!')
objPHPExcel.getActiveSheet.SetCellValue('C1', 'Hello')
objPHPExcel.getActiveSheet.SetCellValue('D2', 'world!')

#Rename sheet
print "#{Time.now} Rename sheet\n";
objPHPExcel.getActiveSheet.setTitle('Simple')

#Save Excel 2007 file
print "#{Time.now} Write to Excel2007 format\n"
objWriter = php.new("PHPExcel_Writer_Excel2007", objPHPExcel)
objWriter.save(__FILE__.gsub(".rb", ".xlsx"))

#Echo done
print "#{Time.now} Done writing file.\n"
9 changes: 8 additions & 1 deletion lib/php_process.rb
Expand Up @@ -2,6 +2,7 @@
require "knj/wref"
require "base64"
require "php-serialize4ruby"
require "open3"

#This class starts a PHP-process and proxies various calls to it. It also spawns proxy-objects, which can you can call like they were normal Ruby-objects.
#===Examples
Expand Down Expand Up @@ -41,7 +42,13 @@ def initialize(args = {})
@callbacks_count = 0
@callbacks_mutex = Mutex.new

cmd_str = "/usr/bin/env php5 \"#{File.dirname(__FILE__)}/php_script.php\""
if @args[:cmd_php]
cmd_str = "#{@args[:cmd_php]} "
else
cmd_str = "/usr/bin/env php5 "
end

cmd_str << "\"#{File.dirname(__FILE__)}/php_script.php\""

if RUBY_ENGINE == "jruby"
pid, @stdin, @stdout, @stderr = IO.popen4(cmd_str)
Expand Down

0 comments on commit 46746d4

Please sign in to comment.