« Jifty/Plugin/Authentication/Password/Action/SendPasswordReminder.pm
↑ Summary
Jifty/Test/Email.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::Plugin::Compat; | ||
| 5 | use base 'Jifty::Plugin'; | ||
| 6 | use CGI::Emulate::PSGI (); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Plugin::Compat - Provide Jifty API compatibility | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | # In your jifty config.yml under the framework section: | ||
| 15 | |||
| 16 | Plugins: | ||
| 17 | - Compat: {} | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This plugin provides Jifty API compatibility. | ||
| 22 | |||
| 23 | =head2 ConfigVersion VERSION 5 | ||
| 24 | |||
| 25 | Provides Jifty::Handler->apache for apps prior to ConfigFileVersion 5. | ||
| 26 | |||
| 27 | TODO: this should also rebind STDIN/STDOUT in the per-request hook. | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =head2 wrap APP | ||
| 32 | |||
| 33 | Wraps the provided Plack app in the compatibility layer. You'll never need to call | ||
| 34 | this yourself. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | *Jifty::Handler::apache = sub { | ||
| 39 | return 'Jifty::Plugin::Compat::Apache'; | ||
| 40 | }; | ||
| 41 | |||
| 42 | *Jifty::Handler::cgi = sub { | ||
| 43 | return 'Jifty::Plugin::Compat::CGI'; | ||
| 44 | }; | ||
| 45 | |||
| 46 | require Jifty::View::Mason::Handler; | ||
| 47 | my $old_new = Jifty::View::Mason::Handler->can('new'); | ||
| 48 | no warnings 'redefine'; | ||
| 49 | *Jifty::View::Mason::Handler::new = sub { | ||
| 50 | my $self = $old_new->(@_); | ||
| 51 | |||
| 52 | $self->interp->compiler->add_allowed_globals('$r'); | ||
| 53 | $self->interp->set_global('$r', 'Jifty::Plugin::Compat::Apache'); | ||
| 54 | |||
| 55 | return $self; | ||
| 56 | }; | ||
| 57 | |||
| 58 | sub wrap { | ||
| 59 | my ($self, $app) = @_; | ||
| 60 | |||
| 61 | sub { | ||
| 62 | my $env = shift; | ||
| 63 | my %cgi = CGI::Emulate::PSGI->emulate_environment($env); | ||
| 64 | local *STDIN = $env->{'psgi.input'}; | ||
| 65 | local *STDERR = $env->{'psgi.errors'}; | ||
| 66 | local %ENV = (%ENV, %cgi); | ||
| 67 | $app->($env); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | push @Jifty::TestServer::ISA, 'Jifty::Server'; | ||
| 72 | |||
| 73 | package Jifty::Server; | ||
| 74 | |||
| 75 | package Jifty::Plugin::Compat::CGI; | ||
| 76 | use Carp; | ||
| 77 | |||
| 78 | sub param { | ||
| 79 | my ($class, $key, $val) = @_; | ||
| 80 | my $params = Jifty->web->request->parameters; | ||
| 81 | if (defined $val) { | ||
| 82 | $params->{$key} = $val; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | return $params->{$key}; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub Vars { | ||
| 90 | my $params = Jifty->web->request->parameters; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub AUTOLOAD { | ||
| 94 | our $AUTOLOAD; | ||
| 95 | croak "compat cgi does not handle $AUTOLOAD"; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | package Jifty::Plugin::Compat::Apache; | ||
| 100 | use Carp; | ||
| 101 | |||
| 102 | sub header_in { | ||
| 103 | my $class = shift; | ||
| 104 | return Jifty->web->request->header(@_); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub headers_in { | ||
| 108 | return Jifty->web->request->headers; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub header_out { | ||
| 112 | my ($class, $key, $val) = @_; | ||
| 113 | if ($key eq 'Status') { | ||
| 114 | Jifty->web->response->status( $val ); | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | if ($key eq 'Content-Type') { | ||
| 119 | Jifty->web->response->content_type( $val ); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | if ($key eq 'Location') { | ||
| 124 | Jifty->web->response->status( 302 ); | ||
| 125 | } | ||
| 126 | |||
| 127 | Jifty->web->response->header( $key, $val ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub headers_out { | ||
| 131 | return Jifty->web->response->headers; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub uri { | ||
| 135 | my $req = Jifty->web->request; | ||
| 136 | return $req->script_name . $req->path_info || ''; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub method { | ||
| 140 | Jifty->web->request->method; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub content_type { | ||
| 144 | my $class = shift; | ||
| 145 | Jifty->web->response->content_type( @_ ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub AUTOLOAD { | ||
| 149 | our $AUTOLOAD; | ||
| 150 | croak "compat apache does not handle $AUTOLOAD"; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub params { | ||
| 154 | my $class = shift; | ||
| 155 | return %{ Jifty->web->request->parameters }; | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; | ||
| 159 |