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