2 use base qw(Class::Accessor::Fast Class::Data::Inheritable);
3 use UNIVERSAL::require;
7 use Maypole::Constants;
10 our $VERSION = '2.10';
12 # proposed privacy conventions:
13 # - no leading underscore - public to custom application code and plugins
14 # - single leading underscore - private to the main Maypole stack - *not* including plugins
15 # - double leading underscore - private to the current package
17 __PACKAGE__->mk_classdata($_) for qw( config init_done view_object );
18 __PACKAGE__->mk_accessors(
19 qw( params query objects model_class template_args output path
20 args action template error document_encoding content_type table
21 headers_in headers_out )
23 __PACKAGE__->config( Maypole::Config->new() );
24 __PACKAGE__->init_done(0);
30 my $calling_class = shift;
32 $calling_class = ref $calling_class if ref $calling_class;
34 my $config = $calling_class->config;
36 $config->model || $config->model('Maypole::Model::CDBI');
38 $config->model->require or die sprintf
39 "Couldn't load the model class %s: %s", $config->model, $@;
41 $config->model->setup_database($config, $calling_class, @_);
43 foreach my $subclass ( @{ $config->classes } )
46 unshift @{ $subclass . "::ISA" }, $config->model;
47 $config->model->adopt($subclass)
48 if $config->model->can("adopt");
55 my $config = $class->config;
56 $config->view || $config->view("Maypole::View::TT");
57 $config->view->require;
58 die "Couldn't load the view class " . $config->view . ": $@" if $@;
59 $config->display_tables
60 || $config->display_tables( $class->config->tables );
61 $class->view_object( $class->config->view->new );
66 # handler() has a method attribute so that mod_perl will invoke
67 # BeerDB->handler() as a method rather than a plain function
68 # BeerDB::handler() and so this inherited implementation will be
69 # found. See e.g. "Practical mod_perl" by Bekman & Cholet for
70 # more information <http://modperlbook.org/html/ch25_01.html>
73 # See Maypole::Workflow before trying to understand this.
74 my ($class, $req) = @_;
76 $class->init unless $class->init_done;
78 # Create the request object
81 config => $class->config
84 $r->headers_out(Maypole::Headers->new);
86 $r->get_request($req);
90 my $status = $r->handler_guts;
92 # moving this here causes unit test failures - need to check why
93 # before committing the move
94 #$status = $r->__call_process_view unless $r->output;
96 return $status unless $status == OK;
103 # The root of all evil
110 my $applicable = __to_boolean( $r->is_applicable );
112 $r->__setup_plain_template unless $applicable;
114 # We authenticate every request, needed for proper session management
117 eval { $status = $r->call_authenticate };
119 if ( my $error = $@ )
121 $status = $r->call_exception($error);
125 warn "caught authenticate error: $error";
126 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
130 if ( $r->debug and $status != OK and $status != DECLINED )
132 $r->view_object->error( $r,
133 "Got unexpected status $status from calling authentication" );
136 return $status unless $status == OK;
138 # We run additional_data for every request
143 eval { $r->model_class->process($r) };
145 if ( my $error = $@ )
147 $status = $r->call_exception($error);
151 warn "caught model error: $error";
152 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
157 # unusual path - perhaps output has been set to an error message
158 return OK if $r->output;
160 # normal path - no output has been generated yet
161 return $r->__call_process_view;
164 # is_applicable() returned false, so set up a plain template. Model processing
165 # will be skipped, but need to remove the model anyway so the template can't
167 sub __setup_plain_template
171 # It's just a plain template
172 $r->model_class(undef);
175 $path =~ s{/$}{}; # De-absolutify
178 $r->template($r->path);
181 # The model has been processed or skipped (if is_applicable returned false),
182 # any exceptions have been handled, and there's no content in $r->output
183 sub __call_process_view
189 eval { $status = $r->view_object->process($r) };
191 if ( my $error = $@ )
193 $status = $r->call_exception($error);
197 warn "caught view error: $error" if $r->debug;
198 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
208 $r->model_class( $r->config->model->class_of($r, $r->table) );
211 # is_applicable() should return true or false, not OK or DECLINED, because
212 # the return value is never used as the return value from handler(). There's
213 # probably a lot of code out there supplying the return codes though, so instead
214 # of changing is_applicable() to return 0 or 1, the return value is passed through
215 # __to_boolean. I think it helps handler_guts() if we don't have multiple sets of
216 # return codes being checked for different things.
221 my $config = $r->config;
223 $config->ok_tables || $config->ok_tables( $config->display_tables );
225 $config->ok_tables( { map { $_ => 1 } @{ $config->ok_tables } } )
226 if ref $config->ok_tables eq "ARRAY";
228 my $table = $r->table;
230 warn "We don't have that table ($table).\n"
231 . "Available tables are: "
232 . join( ",", @{ $config->display_tables } )
234 and not $config->ok_tables->{$table}
235 and $r->action; # I think this is always true
237 return DECLINED unless exists $config->ok_tables->{$table};
240 return DECLINED unless $r->model_class->is_public( $r->action );
245 # *only* intended for translating the return code from is_applicable()
246 sub __to_boolean { $_[0] == OK ? 1 : 0 }
250 sub call_authenticate
254 # Check if we have a model class
255 return $r->model_class->authenticate($r)
257 and $r->model_class->can('authenticate');
259 # if the driver ($r) and the request ($r) ever get separated out
260 # properly, then passing the request as an argument will make more sense
261 return $r->authenticate($r);
266 my ($r, $error) = @_;
268 # Check if we have a model class
269 if ( $r->model_class && $r->model_class->can('exception') )
271 my $status = $r->model_class->exception( $r, $error );
272 return $status if $status == OK;
275 return $r->exception($error);
278 sub additional_data { }
280 sub authenticate { return OK }
282 sub exception { return ERROR }
288 $r->path || $r->path('frontpage');
290 my @pi = grep {length} split '/', $r->path;
292 $r->table(shift @pi);
294 $r->action( shift @pi or 'index' );
299 # like CGI::param(), but read only
304 return keys %{$r->params} unless defined $key;
306 return unless exists $r->params->{$key};
308 my $val = $r->params->{$key};
310 return ref $val ? @$val : ($val) if wantarray;
312 return ref $val ? $val->[0] : $val;
315 sub get_template_root {'.'}
319 die "Do not use Maypole directly; use Apache::MVC or similar";
323 die "Do not use Maypole directly; use Apache::MVC or similar";
326 # Session and Repeat Submission Handling
329 use Maypole::Session;
330 return Maypole::Session::generate_unique_id();
335 Maypole - MVC web application framework
339 See L<Maypole::Application>.
343 This documents the Maypole request object. See the L<Maypole::Manual>, for a
344 detailed guide to using Maypole.
346 Maypole is a Perl web application framework similar to Java's struts. It is
347 essentially completely abstracted, and so doesn't know anything about
348 how to talk to the outside world.
350 To use it, you need to create a package which represents your entire
351 application. In our example above, this is the C<BeerDB> package.
353 This needs to first use L<Maypole::Application> which will make your package
354 inherit from the appropriate platform driver such as C<Apache::MVC> or
355 C<CGI::Maypole>, and then call setup. This sets up the model classes and
356 configures your application. The default model class for Maypole uses
357 L<Class::DBI> to map a database to classes, but this can be changed by altering
358 configuration. (B<Before> calling setup.)
364 Returns the L<Maypole::Config> object
368 My::App->setup($data_source, $user, $password, \%attr);
370 Initialise the maypole application and model classes. Your application should
371 call this after setting configuration via L<"config">
375 You should not call this directly, but you may wish to override this to
377 application-specific initialisation.
381 Get/set the Maypole::View object
385 sub My::App::debug {1}
387 Returns the debugging flag. Override this in your application class to
388 enable/disable debugging.
390 =head2 INSTANCE METHODS
392 =head3 parse_location
394 Turns the backend request (e.g. Apache::MVC, Maypole, CGI) into a
396 request. It does this by setting the C<path>, and invoking C<parse_path>
400 You should only need to define this method if you are writing a new
406 Returns the request path
410 Parses the request path and sets the C<args>, C<action> and C<table>
415 The table part of the Maypole request path
419 The action part of the Maypole request path
423 A list of remaining parts of the request path after table and action
429 A L<Maypole::Headers> object containing HTTP headers for the request
433 A L<HTTP::Headers> object that contains HTTP headers for the output
437 Turns post data and query string paramaters into a hash of C<params>.
439 You should only need to define this method if you are writing a new
445 An accessor for request parameters. It behaves similarly to CGI::param() for
446 accessing CGI parameters.
450 Returns a hash of request parameters. The source of the parameters may vary
451 depending on the Maypole backend, but they are usually populated from request
452 query string and POST data.
454 B<Note:> Where muliple values of a parameter were supplied, the
457 will be an array reference.
459 =head3 get_template_root
461 Implementation-specific path to template root.
463 You should only need to define this method if you are writing a new
465 backend. Otherwise, see L<Maypole::Config/"template_root">
469 You should only need to define this method if you are writing a new
470 Maypole backend. It should return something that looks like an Apache
471 or CGI request object, it defaults to blank.
476 Returns a Maypole::Constant to indicate whether the request is valid.
478 The default implementation checks that C<$r-E<gt>table> is publicly
480 and that the model class is configured to handle the C<$r-E<gt>action>
484 Returns a Maypole::Constant to indicate whether the user is
488 The default implementation returns C<OK>
492 Returns the perl package name that will serve as the model for the
493 request. It corresponds to the request C<table> attribute.
495 =head3 additional_data
497 Called before the model processes the request, this method gives you a
499 to do some processing for each request, for example, manipulating
504 Get/set a list of model objects. The objects will be accessible in the
508 If the first item in C<$r-E<gt>args> can be C<retrieve()>d by the model
510 it will be removed from C<args> and the retrieved object will be added
512 C<objects> list. See L<Maypole::Model> for more information.
516 $r->template_args->{foo} = 'bar';
518 Get/set a hash of template variables.
522 Get/set the template to be used by the view. By default, it returns
527 This method is called if any exceptions are raised during the
530 model/view processing. It should accept the exception as a parameter and
532 a Maypole::Constant to indicate whether the request should continue to
538 Get/set a request error
542 Get/set the response output. This is usually populated by the view
544 can skip view processing by setting the C<output>.
546 =head3 document_encoding
548 Get/set the output encoding. Default: utf-8.
552 Get/set the output content type. Default: text/html
556 Sends the output and additional headers to the user.
558 =head3 call_authenticate
560 This method first checks if the relevant model class
561 can authenticate the user, or falls back to the default
562 authenticate method of your Maypole application.
565 =head3 call_exception
567 This model is called to catch exceptions, first after authenticate, then after
568 processing the model class, and finally to check for exceptions from the view
571 This method first checks if the relevant model class
572 can handle exceptions the user, or falls back to the default
573 exception method of your Maypole application.
575 =head3 make_random_id
577 returns a unique id for this request can be used to prevent or detect repeat submissions.
581 This method sets up the class if it's not done yet, sets some
582 defaults and leaves the dirty work to handler_guts.
586 This is the core of maypole. You don't want to know.
590 There's more documentation, examples, and a information on our mailing lists
591 at the Maypole web site:
593 L<http://maypole.perl.org/>
595 L<Maypole::Application>, L<Apache::MVC>, L<CGI::Maypole>.
599 Maypole is currently maintained by Simon Flack C<simonflk#cpan.org>
601 =head1 AUTHOR EMERITUS
603 Simon Cozens, C<simon#cpan.org>
605 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
609 Sebastian Riedel, Danijel Milicevic, Dave Slack, Jesse Sheidlower, Jody Belka,
610 Marcus Ramberg, Mickael Joanne, Randal Schwartz, Simon Flack, Steve Simms,
611 Veljko Vidovic and all the others who've helped.
615 You may distribute this code under the same terms as Perl itself.