-
-
Save b00gizm/3690119 to your computer and use it in GitHub Desktop.
Run Symfony2 apps with Rack (rackup! pow!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim:se filetype=ruby | |
# vim:se foldlevel=3 | |
# | |
# Hacks and monkeys to run Symfony2 applications as Rack apps using | |
# the rack-legacy and rack-rewrite gems. | |
# | |
# A couple of rack-rewrite rules take care of handing requests to the | |
# default front controller (app.php) and a subclass of rack-legacy's | |
# Php fixes PATH_INFO for Symfony routing. | |
# | |
# Got the idea from: | |
# http://stuff-things.net/2011/05/16/legacy-development-with-pow/ | |
# | |
# Original symfony 1.4 file by https://github.com/marzocchi | |
require 'rubygems' | |
require "rack" | |
require "rack-legacy" | |
require "rack-rewrite" | |
# Your Symfony project web dir | |
SF_WEB_DIR = File.join Dir.getwd, 'web' | |
module Rack | |
module Legacy | |
class Symfony2 < Php | |
# Strip the script name from PATH_INFO, or Symfony won't be able to parse it | |
def run(env, path) | |
if env["PATH_INFO"] =~ %r{^/[^\.]+\.php(.*)$} | |
env.merge!({'ORIG_PATH_INFO' => env['PATH_INFO'], 'PATH_INFO' => $1}) | |
end | |
super env, path | |
end | |
end | |
end | |
end | |
use Rack::Rewrite do | |
# Dispatch any request for "/" to /app.php | |
rewrite %r{(^/$)}, lambda {|match, rack_env| | |
return "/app.php" if File.exists? File.join SF_WEB_DIR, 'app.php' | |
} | |
# Prepend "/app.php" to any URI not matching an existing file | |
# and not containing a php script name | |
rewrite %r{^(?!/[^\.]+\.php)(/.*)$}, lambda { |match, rack_env| | |
file = File.join SF_WEB_DIR, rack_env["PATH_INFO"] | |
unless File.exists? file | |
return "/app.php" + rack_env["PATH_INFO"] if File.exists? File.join SF_WEB_DIR, 'app.php' | |
end | |
rack_env["PATH_INFO"] | |
} | |
end | |
use Rack::Legacy::Symfony2, SF_WEB_DIR | |
run Rack::File.new SF_WEB_DIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment