Maypole.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 156 CC 14 SCAR 34.6
CC
14
Cov
0%
_paths
72.0
send_output
42.0
request_args
6.0
CRAP 31.9
1 package MasonX::Maypole;
2 use warnings;
3 use strict;
4 use Carp;
5  
6 use HTML::Mason 1.30; # for dynamic comp roots
7 use Maypole 2.10; # for Maypole::Application support
8  
9 # need to get rid of this, and a couple of bits and pieces, to allow CGI mode
10 use base 'Apache::MVC';
11  
12 our $VERSION = 0.51;
13  
14 Maypole::Config->mk_accessors( qw( masonx factory_root ) );
15  
16 __PACKAGE__->mk_classdata( 'mason_ah' );
17  
18 =head1 NAME
19  
20 MasonX::Maypole - use Mason as the frontend and view for Maypole version 2
21  
22 =head1 SYNOPSIS
23  
24 package BeerDB;
25 use warnings;
26 use strict;
27  
28 use Class::DBI::Loader::Relationship;
29  
30 use Maypole::Application qw( -Debug2 MasonX AutoUntaint );
31  
32 BeerDB->setup( 'dbi:mysql:beerdb', 'username', 'password' );
33  
34 BeerDB->config->{template_root} = '/home/beerdb/www/www/htdocs';
35 BeerDB->config->{uri_base} = '/';
36 BeerDB->config->{rows_per_page} = 10;
37 BeerDB->config->{display_tables} = [ qw( beer brewery pub style ) ];
38 BeerDB->config->{application_name} = 'The Beer Database';
39  
40 BeerDB->config->masonx->{comp_root} = [ [ factory => '/var/www/maypole/factory' ] ];
41 BeerDB->config->masonx->{data_dir} = '/home/beerdb/www/www/mdata';
42 BeerDB->config->masonx->{in_package} = 'BeerDB::TestApp';
43  
44 BeerDB->auto_untaint;
45  
46 BeerDB->config->{loader}->relationship($_) for (
47 'a brewery produces beers',
48 'a style defines beers',
49 'a pub has beers on handpumps',
50 );
51  
52 1;
53  
54 =head1 ** API CHANGES **
55  
56 Version 0.5 contains major modifications, and changes to error handling. See
57 the Changes file for details.
58  
59 =head1 DESCRIPTION
60  
61 A frontend and view for Maypole, using Mason.
62  
63 =head1 EXAMPLES
64  
65 Example C<BeerDB.pm> and a C<httpd.conf> VirtualHost setup are included in
66 the C</doc> directory of the distribution.
67  
68 A working example of the BeerDB application is at C<http://beerdb.riverside-cms.co.uk>,
69 including the C<BeerDB.pm> and C<httpd.conf> used for that site.
70  
71 =head1 CONFIGURING MASON
72  
73 Set any parameters for the Mason ApacheHandler in C<<BeerDB->config->{masonx}>>.
74 This is where to tell Maypole/Mason where the factory templates are stored.
75  
76 Note that the user the server runs as must have permission to read the files in the
77 factory templates directory, which also means all directories in the path to the
78 templates must be readable and executable (i.e. openable). If Mason can't read
79 these templates, you may get a cryptic 'file doesn't exist' error, but you
80 will not get a 'not permitted' error.
81  
82 =head1 Maypole::Application
83  
84 L<Maypole::Application|Maypole::Application> needs to be patched before it will work
85 with MasonX::Maypole. You can download a patched copy from C<http://beerdb.riverside-cms.co.uk>,
86 until the required updates are included in the version distributed with L<Maypole>.
87  
88 =head1 TEMPLATES
89  
90 This distribution includes Masonized versions of the standard Maypole templates,
91 plus a dhandler and autohandler. The autohandler simply takes care of adding
92 a header and footer to every page, while the dhandler loads the template
93 specified in the Maypole request object.
94  
95 So if you set the factory comp_root to point at the Maypole factory templates,
96 the thing should Just Work right out of the box. Except for maypole.css, which
97 you will need to copy to the right place on your server.
98  
99 =head1 METHODS
100  
101 =over
102  
103 =item init
104  
105 This method is called by Maypole while processing the first request the server
106 receives. Probably better under mod_perl to call this explicitly at the end of
107 your setup code (C<BeerDB-E<gt>init>) to share memory among Apache children.
108 Sets up the Mason ApacheHandler, including the search path behaviour.
109  
110 =cut
111  
112 # This only gets called once. Mason's path searching mechanism replaces
113 # get_template_root and Maypole::View::Base::paths.
114 sub init
115 {
116 my ( $r ) = @_;
117  
118 my $mason_cfg = $r->config->masonx;
119
120 $mason_cfg->{comp_root} = [ $r->_paths ];
121
122 $mason_cfg->{decline_dirs} ||= 0;
123 $mason_cfg->{in_package} ||= 'HTML::Mason::Commands';
124 $mason_cfg->{dynamic_comp_root} = 1;
125
126 $r->mason_ah( MasonX::Maypole::ApacheHandler->new( %{ $mason_cfg } ) );
127
128 $r->config->view || $r->config->view( 'MasonX::Maypole::View' );
129
130 # set up the view object
131 $r->SUPER::init;
132 }
133  
134 # MasonX::Maypole::View::path() wraps this
135 sub _paths
136 {
137 my ( $r ) = @_; # class during startup, object during requests
138  
139 my $root = $r->config->template_root || $r->get_template_root;
140 my $factory = $r->config->factory_root;
141
142 $root = ref $root eq 'ARRAY' ? $root : [ $root ];
143
144 my @paths;
145  
146 foreach my $path ( @$root )
147 {
148 if ( ref $r ) # false during startup - calling model_class() is a fatal error
149 {
150 if ( my $model = $r->model_class )
151 {
152 my $model_path = File::Spec->catdir( $path, $model->moniker );
153
154 $model =~ s/::/_/g;
155
156 push @paths, [ $model, $model_path ] if -d $model_path;
157 }
158 }
159
160 my $custom = File::Spec->catdir( $path, 'custom' );
161
162 push @paths, [ custom => $custom ] if -d $custom;
163
164 push @paths, [ default => $path ];
165
166 my $this_factory = File::Spec->catdir( $path, 'factory' );
167 push @paths, [ factory => $this_factory ] if -d $this_factory;
168 }
169
170 push @paths, [ factory => $factory ] if $factory;
171
172 return @paths;
173 }
174  
175 =item parse_args
176  
177 Uses Mason to extract the request arguments from the request.
178  
179 =cut
180  
181 # override the method in Apache::MVC
182 sub parse_args
183 {
184 my ( $r ) = @_;
185  
186 # set and return request args in Mason request object
187 my $args = $r->mason_ah->request_args( $r->ar );
188  
189 $r->{params} = $args;
190 $r->{query} = $args;
191 }
192  
193 =item send_output
194  
195 Template variables have already been exported to Mason components namespace
196 in C<MasonX::Maypole::View::template>. This method now runs the Mason C<exec>
197 phase to generate and send output.
198  
199 =cut
200  
201 sub send_output
202 {
203 my ( $r ) = @_;
204
205 #
206 # set up the Mason request object
207 #
208
209 # if there was an error, there may already be a report in the output slot
210 die $r->output if $r->output;
211  
212 my $m = $r->mason_ah->prepare_request( $r->ar );
213
214 unless ( ref $m )
215 {
216 warn "prepare_request returned this: [$m] instead of a Mason request object";
217 return $m;
218 }
219
220 #
221 # set headers (Mason will set content-length and call send_http_header)
222 #
223 $r->ar->content_type(
224 $r->content_type =~ m/^text/
225 ? $r->content_type . "; charset=" . $r->document_encoding
226 : $r->content_type
227 );
228
229 foreach ( $r->headers_out->field_names )
230 {
231 next if /^Content-(Type|Length)/;
232 $r->{ar}->headers_out->set( $_ => $r->headers_out->get( $_ ) );
233 }
234
235 #
236 # set dynamic comp roots
237 #
238 $m->interp->comp_root( [ $r->view_object->paths( $r ) ] );
239  
240 if ( $r->debug > 1 )
241 {
242 Data::Dumper->require or die "Failed to load Data::Dumper: $@";
243 warn "Comp roots: " . Data::Dumper::Dumper( $m->interp->comp_root );
244 }
245
246 # now generate and send output
247 my $status = $m->exec;
248
249 # Maypole doesn't actually check this status, but for the sake of good form:
250 return $status;
251 }
252  
253 =item get_template_root
254  
255 Returns C<template_root> from the config.
256  
257 This varies from L<Apache::MVC|Apache::MVC>, which concatenates
258 document_root and location from the Apache request server config.
259  
260 =cut
261  
262 sub get_template_root { $_[0]->config->template_root }
263  
264 =item get_request
265  
266 Replaces C<Apache::MVC::get_request>, using C<Apache::Request::instance()> instead
267 of C<Apache::Request::new()> to obtain the APR object. Calling C<new> means Mason
268 and Maypole have different APR objects, and the Mason one doesn't have any POST
269 data.
270  
271 =cut
272  
273 sub get_request
274 {
275 my ( $self, $r ) = @_;
276
277 $self->{ar} = Apache::Request->instance( $r );
278 }
279  
280 {
281 # copied from MasonX::WebApp
282 package MasonX::Maypole::ApacheHandler;
283 use base 'HTML::Mason::ApacheHandler';
284  
285 sub request_args
286 {
287 my ( $self, $apr ) = @_;
288  
289 return $apr->pnotes('__request_args__') if $apr->pnotes('__request_args__');
290  
291 my $args = ($self->SUPER::request_args($apr))[0] || {};
292  
293 $apr->pnotes( __request_args__ => $args );
294  
295 return $args;
296 }
297 }
298  
299 =back
300  
301 =head1 AUTHOR
302  
303 David Baird, C<< <cpan@riverside-cms.co.uk> >>
304  
305 =head1 TODO
306  
307 Currently hard-coded to use Apache/mod_perl. Shouldn't be too hard to use CGI
308 instead.
309  
310 =head1 BUGS
311  
312 Please report any bugs or feature requests to
313 C<bug-masonx-maypole2@rt.cpan.org>, or through the web interface at
314 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
315 be notified of progress on your bug as I make changes.
316  
317 =head1 TESTS
318  
319 There are none. The module loads Mason::ApacheHandler, which causes compile
320 time errors unless loaded within mod_perl.
321  
322 =head1 ACKNOWLEDGEMENTS
323  
324 =head1 COPYRIGHT & LICENSE
325  
326 Copyright 2004 David Baird, All Rights Reserved.
327  
328 This program is free software; you can redistribute it and/or modify it
329 under the same terms as Perl itself.
330  
331 =cut
332  
333 1; # End of MasonX::Maypole2