visionmedia-ruby-php-bridge 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +8 -0
- data/Manifest +18 -0
- data/README.rdoc +67 -0
- data/Rakefile +15 -0
- data/Todo.rdoc +12 -0
- data/lib/ruby-php-bridge.rb +26 -0
- data/lib/ruby-php-bridge/bridge.rb +66 -0
- data/lib/ruby-php-bridge/import.rb +4 -0
- data/lib/ruby-php-bridge/version.rb +6 -0
- data/php/php-ruby-bridge.php +21 -0
- data/ruby-php-bridge.gemspec +30 -0
- data/spec/import_spec.rb +8 -0
- data/spec/ruby-php-bridge_spec.rb +62 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +4 -0
- data/tasks/docs.rake +13 -0
- data/tasks/gemspec.rake +3 -0
- data/tasks/spec.rake +25 -0
- metadata +83 -0
data/History.rdoc
ADDED
data/Manifest
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
History.rdoc
|
2
|
+
lib/ruby-php-bridge/bridge.rb
|
3
|
+
lib/ruby-php-bridge/import.rb
|
4
|
+
lib/ruby-php-bridge/version.rb
|
5
|
+
lib/ruby-php-bridge.rb
|
6
|
+
Manifest
|
7
|
+
php/php-ruby-bridge.php
|
8
|
+
Rakefile
|
9
|
+
README.rdoc
|
10
|
+
ruby-php-bridge.gemspec
|
11
|
+
spec/import_spec.rb
|
12
|
+
spec/ruby-php-bridge_spec.rb
|
13
|
+
spec/spec.opts
|
14
|
+
spec/spec_helper.rb
|
15
|
+
tasks/docs.rake
|
16
|
+
tasks/gemspec.rake
|
17
|
+
tasks/spec.rake
|
18
|
+
Todo.rdoc
|
data/README.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
|
2
|
+
= Ruby -> PHP Bridge
|
3
|
+
|
4
|
+
The Ruby PHP bridge is a tiny library providing a JSON bridge
|
5
|
+
between the two languages. Any code which can be converted to JSON
|
6
|
+
can be passed between the two. In both cases the '$return' variable
|
7
|
+
may be set in order to convert it to the other language.
|
8
|
+
|
9
|
+
PHP::Bridge.eval '$return = array("any", "data", "here");'
|
10
|
+
# => ['any', 'data', 'here']
|
11
|
+
|
12
|
+
OR
|
13
|
+
|
14
|
+
require 'ruby-php-bridge/import'
|
15
|
+
php '$return = array("something")'
|
16
|
+
# => ['something']
|
17
|
+
|
18
|
+
OR
|
19
|
+
|
20
|
+
require 'ruby-php-bridge/import'
|
21
|
+
php 'array("something")'
|
22
|
+
# => ['something']
|
23
|
+
|
24
|
+
OR
|
25
|
+
|
26
|
+
bridge = PHP::Bridge.new
|
27
|
+
bridge << '$a = "hello";'
|
28
|
+
bridge << '$a .= " there";'
|
29
|
+
bridge << '$return = $a;'
|
30
|
+
bridge.eval
|
31
|
+
# => ['hello there']
|
32
|
+
|
33
|
+
As you can see in one of the examples above, '$return =' is prepended
|
34
|
+
when not present, providing a cleaner API.
|
35
|
+
|
36
|
+
== PHP -> Ruby Bridge
|
37
|
+
|
38
|
+
A small PHP library functions similarily, providing
|
39
|
+
a bridge to Ruby, which is located in php/php-ruby-bridge.php
|
40
|
+
|
41
|
+
ruby('$return = ["any", "data", "here"]')
|
42
|
+
// => array('any', 'data', 'here')
|
43
|
+
|
44
|
+
== License:
|
45
|
+
|
46
|
+
(The MIT License)
|
47
|
+
|
48
|
+
Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
|
49
|
+
|
50
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
51
|
+
a copy of this software and associated documentation files (the
|
52
|
+
'Software'), to deal in the Software without restriction, including
|
53
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
54
|
+
distribute, sublicense, an d/or sell copies of the Software, and to
|
55
|
+
permit persons to whom the Software is furnished to do so, subject to
|
56
|
+
the following conditions:
|
57
|
+
|
58
|
+
The above copyright notice and this permission notice shall be
|
59
|
+
included in all copies or substantial portions of the Software.
|
60
|
+
|
61
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
62
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
63
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
64
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
65
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
66
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
67
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
$:.unshift 'lib'
|
3
|
+
require 'ruby-php-bridge'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake'
|
6
|
+
require 'echoe'
|
7
|
+
|
8
|
+
Echoe.new "ruby-php-bridge", PHP::Bridge::VERSION do |p|
|
9
|
+
p.author = "TJ Holowaychuk"
|
10
|
+
p.email = "tj@vision-media.ca"
|
11
|
+
p.summary = "Ruby to PHP / PHP to Ruby bridge"
|
12
|
+
p.runtime_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir['tasks/**/*.rake'].sort.each { |f| load f }
|
data/Todo.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
require 'json'
|
25
|
+
require 'ruby-php-bridge/bridge'
|
26
|
+
require 'ruby-php-bridge/version'
|
@@ -0,0 +1,66 @@
|
|
1
|
+
|
2
|
+
module PHP
|
3
|
+
class Bridge
|
4
|
+
|
5
|
+
#--
|
6
|
+
# Constants
|
7
|
+
#++
|
8
|
+
|
9
|
+
BIN = 'php'
|
10
|
+
|
11
|
+
##
|
12
|
+
# PHP source string.
|
13
|
+
|
14
|
+
attr_accessor :source
|
15
|
+
|
16
|
+
##
|
17
|
+
# Initial bridge.
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@source = ''
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# PHP source string.
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
source
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Append _source_.
|
32
|
+
|
33
|
+
def << source
|
34
|
+
@source << source
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Evaluate string against the PHP binary.
|
39
|
+
|
40
|
+
def eval
|
41
|
+
Bridge.eval to_s
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Execute _source_ returning the raw JSON output.
|
46
|
+
|
47
|
+
def self.execute source
|
48
|
+
`#{BIN} -r '#{source << "\n echo json_encode(isset($return) ? (array) $return : array());"}'`
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Evaluate _source_ returning a ruby object.
|
53
|
+
|
54
|
+
def self.eval source
|
55
|
+
source = '$return = ' << source unless source.empty? || source.include?('$return')
|
56
|
+
JSON.parse execute(source)
|
57
|
+
end
|
58
|
+
|
59
|
+
module Delegates
|
60
|
+
def php source
|
61
|
+
::PHP::Bridge.eval source
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<?php
|
2
|
+
|
3
|
+
define('RUBY_BRIDGE_VERSION', '1.1.0');
|
4
|
+
define('RUBY_BRIDGE_BIN', 'ruby');
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Execute ruby $source, returning the JSON
|
8
|
+
* decoded object.
|
9
|
+
*
|
10
|
+
* @param string $source
|
11
|
+
* @return string
|
12
|
+
*/
|
13
|
+
|
14
|
+
function ruby($source) {
|
15
|
+
$ruby = "require 'rubygems'\n";
|
16
|
+
$ruby .= "require 'json'\n";
|
17
|
+
$ruby .= $source;
|
18
|
+
$ruby .= "\n puts \$return.nil? ? '{}' : \$return.to_json";
|
19
|
+
$ruby = str_replace("'", '"', $ruby);
|
20
|
+
return json_decode(shell_exec(RUBY_BRIDGE_BIN . " -e '{$ruby}'"));
|
21
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{ruby-php-bridge}
|
5
|
+
s.version = "1.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["TJ Holowaychuk"]
|
9
|
+
s.date = %q{2009-08-10}
|
10
|
+
s.description = %q{Ruby to PHP / PHP to Ruby bridge}
|
11
|
+
s.email = %q{tj@vision-media.ca}
|
12
|
+
s.extra_rdoc_files = ["lib/ruby-php-bridge/bridge.rb", "lib/ruby-php-bridge/import.rb", "lib/ruby-php-bridge/version.rb", "lib/ruby-php-bridge.rb", "README.rdoc", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
|
13
|
+
s.files = ["History.rdoc", "lib/ruby-php-bridge/bridge.rb", "lib/ruby-php-bridge/import.rb", "lib/ruby-php-bridge/version.rb", "lib/ruby-php-bridge.rb", "Manifest", "php/php-ruby-bridge.php", "Rakefile", "README.rdoc", "ruby-php-bridge.gemspec", "spec/import_spec.rb", "spec/ruby-php-bridge_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake", "Todo.rdoc"]
|
14
|
+
s.homepage = %q{}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ruby-php-bridge", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{ruby-php-bridge}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Ruby to PHP / PHP to Ruby bridge}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/spec/import_spec.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
3
|
+
|
4
|
+
describe PHP::Bridge do
|
5
|
+
before :each do
|
6
|
+
@bridge = PHP::Bridge.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "VERSION" do
|
10
|
+
it "should be a valid triple" do
|
11
|
+
PHP::Bridge::VERSION.should match(/\d+\.\d+\.\d+/)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#to_s" do
|
16
|
+
it "should return the source" do
|
17
|
+
@bridge.source = 'test'
|
18
|
+
@bridge.to_s.should include('test')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#<<" do
|
23
|
+
it "should append source" do
|
24
|
+
@bridge << 'foo'
|
25
|
+
@bridge << 'bar'
|
26
|
+
@bridge.to_s.should include('foobar')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#eval" do
|
31
|
+
it "should evaluate the source" do
|
32
|
+
@bridge << '$return = "test";'
|
33
|
+
@bridge.eval.first.should == 'test'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".eval" do
|
38
|
+
it "should return a ruby object" do
|
39
|
+
result = PHP::Bridge.eval <<-PHP
|
40
|
+
$array = array('foo' => 'bar');
|
41
|
+
$return = $array;
|
42
|
+
PHP
|
43
|
+
result['foo'].should == 'bar'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return empty array when nothing is present" do
|
47
|
+
PHP::Bridge.eval('').should be_empty
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should work with strings" do
|
51
|
+
PHP::Bridge.eval('$return = "foo";').first.should == 'foo'
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should escape quotes" do
|
55
|
+
PHP::Bridge.eval("$return = 'foo';").first.should == 'foo'
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should prepend '$return = ' when it is not present" do
|
59
|
+
PHP::Bridge.eval('"foo";').first.should == 'foo'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
data/tasks/docs.rake
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
namespace :docs do
|
3
|
+
|
4
|
+
desc 'Remove rdoc products'
|
5
|
+
task :remove => [:clobber_docs]
|
6
|
+
|
7
|
+
desc 'Build docs, and open in browser for viewing (specify BROWSER)'
|
8
|
+
task :open do
|
9
|
+
browser = ENV["BROWSER"] || "safari"
|
10
|
+
sh "open -a #{browser} doc/index.html"
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/tasks/gemspec.rake
ADDED
data/tasks/spec.rake
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc "Run all specifications"
|
5
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
6
|
+
t.libs << "lib"
|
7
|
+
t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
namespace :spec do
|
11
|
+
|
12
|
+
desc "Run all specifications verbosely"
|
13
|
+
Spec::Rake::SpecTask.new(:verbose) do |t|
|
14
|
+
t.libs << "lib"
|
15
|
+
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Run specific specification verbosely (specify SPEC)"
|
19
|
+
Spec::Rake::SpecTask.new(:select) do |t|
|
20
|
+
t.libs << "lib"
|
21
|
+
t.spec_files = [ENV["SPEC"]]
|
22
|
+
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: visionmedia-ruby-php-bridge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TJ Holowaychuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Ruby to PHP / PHP to Ruby bridge
|
17
|
+
email: tj@vision-media.ca
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- lib/ruby-php-bridge/bridge.rb
|
24
|
+
- lib/ruby-php-bridge/import.rb
|
25
|
+
- lib/ruby-php-bridge/version.rb
|
26
|
+
- lib/ruby-php-bridge.rb
|
27
|
+
- README.rdoc
|
28
|
+
- tasks/docs.rake
|
29
|
+
- tasks/gemspec.rake
|
30
|
+
- tasks/spec.rake
|
31
|
+
files:
|
32
|
+
- History.rdoc
|
33
|
+
- lib/ruby-php-bridge/bridge.rb
|
34
|
+
- lib/ruby-php-bridge/import.rb
|
35
|
+
- lib/ruby-php-bridge/version.rb
|
36
|
+
- lib/ruby-php-bridge.rb
|
37
|
+
- Manifest
|
38
|
+
- php/php-ruby-bridge.php
|
39
|
+
- Rakefile
|
40
|
+
- README.rdoc
|
41
|
+
- ruby-php-bridge.gemspec
|
42
|
+
- spec/import_spec.rb
|
43
|
+
- spec/ruby-php-bridge_spec.rb
|
44
|
+
- spec/spec.opts
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
- tasks/docs.rake
|
47
|
+
- tasks/gemspec.rake
|
48
|
+
- tasks/spec.rake
|
49
|
+
- Todo.rdoc
|
50
|
+
has_rdoc: false
|
51
|
+
homepage: ""
|
52
|
+
licenses:
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --line-numbers
|
56
|
+
- --inline-source
|
57
|
+
- --title
|
58
|
+
- Ruby-php-bridge
|
59
|
+
- --main
|
60
|
+
- README.rdoc
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "1.2"
|
74
|
+
version:
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project: ruby-php-bridge
|
78
|
+
rubygems_version: 1.3.5
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Ruby to PHP / PHP to Ruby bridge
|
82
|
+
test_files: []
|
83
|
+
|