Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Run PHP middleware inside a rack stack

  • Loading branch information...
commit 1434484c264e13c9e7527408465e68514412d3f7 1 parent c8e8c70
Simo Niemelä sniemela authored
63 README.markdown
View
@@ -65,6 +65,69 @@ RubyBridge is simple a proxy class that passes the real work to the php script v
Here we can take advantage of the ruby Rack::File middle-ware to serve static files. This is needed when running through servers like thin or mongrel directly.
+We can call PHP middleware through a RubyMiddlewareBridge class:
+
+**config.ru**
+
+ class RubyMiddlewareBridge
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ env['rack.ruby_bridge_response'] = @app.call(env)
+
+ response = []
+ data = JSON.dump(env)
+ IO.popen("./rackup.php", 'r+') do |io|
+ io.write data
+ io.close_write
+ response = io.read
+ end
+ JSON.load(response)
+ end
+ end
+
+ use RubyMiddlewareBridge
+ use Rack::Reloader
+ use Rack::ContentLength
+
+ app = proc do |env|
+ [200, {'Content-Type' => 'text/html'}, ['Hello world']]
+ end
+
+ run app
+
+And inside the rackup.php we can use PHP middleware to continue the rack app callchain:
+
+**rackup.php**
+
+ #!/usr/bin/env php
+ <?
+ require realpath(dirname(__FILE__).'/lib/autoload.php');
+ use core\RubyBridge;
+
+ class SwapHelloWorld extends core\App
+ {
+ protected $app;
+
+ public function __construct($app)
+ {
+ $this->app = $app;
+ }
+
+ public function __invoke($env)
+ {
+ list($status, $headers, $body) = $this->call($this->app, $env);
+ $parts = split(' ', $body[0]);
+ $body = $parts[1] . ' ' . $parts[0];
+ return array($status, $headers, array($body));
+ }
+ }
+
+ RubyBridge::useMiddleware('SwapHelloWorld');
+ RubyBridge::run(); // => world Hello
+
## Simple Rack PHP App
Here is a small sample rack-php app that simply ignores all input and outputs the string "Hello World".
19 config.ru
View
@@ -14,6 +14,25 @@ class RubyBridge
end
end
+class RubyMiddlewareBridge
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ env['rack.ruby_bridge_response'] = @app.call(env)
+
+ response = []
+ data = JSON.dump(env)
+ IO.popen("./rackup.php", 'r+') do |io|
+ io.write data
+ io.close_write
+ response = io.read
+ end
+ JSON.load(response)
+ end
+end
+
use Rack::Reloader
run Rack::Cascade.new([
Rack::File.new('public'),
20 lib/core/RubyBridge.php
View
@@ -16,5 +16,23 @@ protected static function execute($result, $env)
$headers['X-Powered-By'] = 'rack-php ' . implode('.', $env['rack.version']);
exit(json_encode(array($status, $headers, $body)));
}
-
+
+ public static function run($app = null)
+ {
+ $env =& static::get_env();
+
+ if (is_null($app) && !is_null($env['rack.ruby_bridge_response'])) {
+ $app = function ($env) use ($env) { return $env['rack.ruby_bridge_response']; };
+ }
+
+ ob_start();
+ $result = self::runMiddleware($app, $env);
+ $output = ob_get_clean();
+
+ if ($output)
+ {
+ $result[1]["X-Output"] = json_encode($output);
+ }
+ static::execute($result, $env);
+ }
}
5 public/index.php
View
@@ -2,6 +2,9 @@
require realpath(dirname(__FILE__).'/../lib/autoload.php');
use core\Rack;
-$app = new creationix\MyApplication;
+//$app = new creationix\MyApplication;
+$app = function ($env) {
+ return array(200, array('Content-Type' => 'text/html'), array('Hello World'));
+};
Rack::run($app);
5 rackup.php
View
@@ -1,7 +1,8 @@
#!/usr/bin/env php
<?
require realpath(dirname(__FILE__).'/lib/autoload.php');
+use core\RubyBridge;
-$app = new \creationix\MyApplication();
-$env = \core\RubyBridge::run($app);
+$app = new creationix\MyApplication;
+RubyBridge::run($app);
Please sign in to comment.
Something went wrong with that request. Please try again.