]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC.pm
2.09 - maintain the order that plugins are loaded, add tests for Maypole::Application...
[maypole.git] / lib / Apache / MVC.pm
1 package Apache::MVC;
2
3 our $VERSION = '2.09';
4
5 use strict;
6 use warnings;
7
8 use base 'Maypole';
9 use mod_perl;
10 use Maypole::Headers;
11
12 use constant APACHE2 => $mod_perl::VERSION >= 1.99;
13
14 if (APACHE2) {
15     require Apache2;
16     require Apache::RequestIO;
17     require Apache::RequestRec;
18     require Apache::RequestUtil;
19     require APR::URI;
20 }
21 else { require Apache }
22 require Apache::Request;
23
24 sub get_request {
25     my ( $self, $r ) = @_;
26     $self->{ar} = Apache::Request->new($r);
27 }
28
29 sub parse_location {
30     my $self = shift;
31
32     # Reconstruct the request headers
33     $self->headers_in(Maypole::Headers->new);
34     my %headers;
35     if (APACHE2) { %headers = %{$self->{ar}->headers_in};
36     } else { %headers = $self->{ar}->headers_in; }
37     for (keys %headers) {
38         $self->headers_in->set($_, $headers{$_});
39     }
40
41     $self->{path} = $self->{ar}->uri;
42     my $loc = $self->{ar}->location;
43     no warnings 'uninitialized';
44     $self->{path} .= '/' if $self->{path} eq $loc;
45     $self->{path} =~ s/^($loc)?\///;
46     $self->parse_path;
47     $self->parse_args;
48 }
49
50 sub parse_args {
51     my $self = shift;
52     $self->{params} = { $self->_mod_perl_args( $self->{ar} ) };
53     $self->{query}  = { $self->_mod_perl_args( $self->{ar} ) };
54 }
55
56 sub send_output {
57     my $r = shift;
58     $r->{ar}->content_type(
59           $r->{content_type} =~ m/^text/
60         ? $r->{content_type} . "; charset=" . $r->{document_encoding}
61         : $r->{content_type}
62     );
63     $r->{ar}->headers_out->set(
64         "Content-Length" => do { use bytes; length $r->{output} }
65     );
66
67     foreach ($r->headers_out->field_names) {
68         next if /^Content-(Type|Length)/;
69         $r->{ar}->headers_out->set($_ => $r->headers_out->get($_));
70     }
71
72     APACHE2 || $r->{ar}->send_http_header;
73     $r->{ar}->print( $r->{output} );
74 }
75
76 sub get_template_root {
77     my $r = shift;
78     $r->{ar}->document_root . "/" . $r->{ar}->location;
79 }
80
81 sub _mod_perl_args {
82     my ( $self, $apr ) = @_;
83     my %args;
84     foreach my $key ( $apr->param ) {
85         my @values = $apr->param($key);
86         $args{$key} = @values == 1 ? $values[0] : \@values;
87     }
88     return %args;
89 }
90
91 1;
92
93 =head1 NAME
94
95 Apache::MVC - Apache front-end to Maypole
96
97 =head1 SYNOPSIS
98
99     package BeerDB;
100     use base 'Apache::MVC';
101     BeerDB->setup("dbi:mysql:beerdb");
102     BeerDB->config->uri_base("http://your.site/");
103     BeerDB->config->display_tables([qw[beer brewery pub style]]);
104     # Now set up your database:
105     # has-a relationships
106     # untaint columns
107
108     1;
109
110 =head1 DESCRIPTION
111
112 A mod_perl platform driver for Maypole. Your application can inherit from
113 Apache::MVC directly, but it is recommended that you use
114 L<Maypole::Application>.
115
116 =head1 INSTALLATION
117
118 Create a driver module like the one above.
119
120 Put the following in your Apache config:
121
122     <Location /beer>
123         SetHandler perl-script
124         PerlHandler BeerDB
125     </Location>
126
127 Copy the templates found in F<templates/factory> into the
128 F<beer/factory> directory off the web root. When the designers get
129 back to you with custom templates, they are to go in
130 F<beer/custom>. If you need to do override templates on a
131 database-table-by-table basis, put the new template in
132 F<beer/I<table>>. 
133
134 This will automatically give you C<add>, C<edit>, C<list>, C<view> and
135 C<delete> commands; for instance, a list of breweries, go to 
136
137     http://your.site/beer/brewery/list
138
139 For more information about how the system works and how to extend it,
140 see L<Maypole>.
141
142 =head1 Implementation
143
144 This class overrides a set of methods in the base Maypole class to provide it's
145 functionality. See L<Maypole> for these:
146
147 =over
148
149 =item get_request
150
151 =item get_template_root
152
153 =item parse_args
154
155 =item parse_location
156
157 =item send_output
158
159 =back
160
161 =head1 AUTHOR
162
163 Simon Cozens, C<simon@cpan.org>
164 Marcus Ramberg, C<marcus@thefeed.no>
165 Screwed up by Sebastian Riedel, C<sri@oook.de>
166
167 =head1 LICENSE
168
169 You may distribute this code under the same terms as Perl itself.
170
171 =cut