Class: Rack::Legacy::Php

Inherits:
Cgi
  • Object
show all
Defined in:
lib/rack/legacy/php.rb

Defined Under Namespace

Classes: HtAccess

Instance Attribute Summary

Attributes inherited from Cgi

#public_dir

Instance Method Summary collapse

Methods inherited from Cgi

#call

Constructor Details

#initialize(app, public_dir = FileUtils.pwd, php_exe = 'php-cgi', htaccess_enabled = true) ⇒ Php

Like Rack::Legacy::Cgi.new except allows an additional argument of which executable to use to run the PHP code.

use Rack::Legacy::Php, 'public', 'php5-cgi'


11
12
13
14
15
# File 'lib/rack/legacy/php.rb', line 11

def initialize(app, public_dir=FileUtils.pwd, php_exe='php-cgi', htaccess_enabled=true)
  super app, public_dir
  @php_exe = php_exe
  @htaccess_enabled = htaccess_enabled
end

Instance Method Details

#run(env, path) ⇒ Object

Monkeys with the arguments so that it actually runs PHP's cgi program with the path as an argument to that program.



26
27
28
29
30
31
32
33
34
# File 'lib/rack/legacy/php.rb', line 26

def run(env, path)
  config = {'cgi.force_redirect' => 0}
  config.merge! HtAccess.merge_all(path, public_dir) if @htaccess_enabled
  config = config.collect {|(key, value)| "#{key}=#{value}"}
  config.collect! {|kv| ['-d', kv]}

  env['SCRIPT_FILENAME'] = path
  super env, @php_exe, *config.flatten
end

#valid?(path) ⇒ Boolean

Adds extension checking in addition to checks in superclass.

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rack/legacy/php.rb', line 18

def valid?(path)
  fp = full_path path
  [/php$/, /php?\d$/].any? {|ext| ::File.extname(fp) =~ ext} &&
  fp.start_with?(::File.expand_path @public_dir) && ::File.file?(fp)
end