2 use base qw(Class::Accessor::Fast Class::Data::Inheritable);
3 use UNIVERSAL::require;
7 use Maypole::Constants;
10 our $VERSION = '2.10';
12 __PACKAGE__->mk_classdata($_) for qw( config init_done view_object );
13 __PACKAGE__->mk_accessors(
14 qw( params query objects model_class template_args output path
15 args action template error document_encoding content_type table
16 headers_in headers_out )
18 __PACKAGE__->config( Maypole::Config->new() );
19 __PACKAGE__->init_done(0);
25 my $calling_class = shift;
27 $calling_class = ref $calling_class if ref $calling_class;
29 my $config = $calling_class->config;
31 $config->model || $config->model('Maypole::Model::CDBI');
33 $config->model->require or die
34 "Couldn't load the model class $config->{model}: $@";
36 $config->model->setup_database($config, $calling_class, @_);
38 foreach my $subclass ( @{ $config->classes } )
41 unshift @{ $subclass . "::ISA" }, $config->model;
42 $config->model->adopt($subclass)
43 if $config->model->can("adopt");
50 my $config = $class->config;
51 $config->view || $config->view("Maypole::View::TT");
52 $config->view->require;
53 die "Couldn't load the view class " . $config->view . ": $@" if $@;
54 $config->display_tables
55 || $config->display_tables( $class->config->tables );
56 $class->view_object( $class->config->view->new );
61 # handler() has a method attribute so that mod_perl will invoke
62 # BeerDB->handler() as a method rather than a plain function
63 # BeerDB::handler() and so this inherited implementation will be
64 # found. See e.g. "Practical mod_perl" by Bekman & Cholet for
65 # more information <http://modperlbook.org/html/ch25_01.html>
68 # See Maypole::Workflow before trying to understand this.
69 my ($class, $req) = @_;
71 $class->init unless $class->init_done;
73 # Create the request object
76 config => $class->config
78 $r->headers_out(Maypole::Headers->new);
79 $r->get_request($req);
81 my $status = $r->handler_guts();
82 return $status unless $status == OK;
87 # The root of all evil
90 $r->model_class( $r->config->model->class_of( $r, $r->{table} ) );
92 my $applicable = $r->is_applicable;
93 unless ( $applicable == OK ) {
95 # It's just a plain template
96 delete $r->{model_class};
97 $r->{path} =~ s{/$}{}; # De-absolutify
98 $r->template( $r->{path} );
101 # We authenticate every request, needed for proper session management
103 eval { $status = $r->call_authenticate };
104 if ( my $error = $@ ) {
105 $status = $r->call_exception($error);
106 if ( $status != OK ) {
107 warn "caught authenticate error: $error";
108 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
111 if ( $r->debug and $status != OK and $status != DECLINED ) {
112 $r->view_object->error( $r,
113 "Got unexpected status $status from calling authentication" );
115 return $status unless $status == OK;
117 # We run additional_data for every request
119 if ( $applicable == OK ) {
120 eval { $r->model_class->process($r) };
121 if ( my $error = $@ ) {
122 $status = $r->call_exception($error);
123 if ( $status != OK ) {
124 warn "caught model error: $error";
125 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
129 if ( !$r->{output} ) { # You might want to do it yourself
130 eval { $status = $r->view_object->process($r) };
131 if ( my $error = $@ ) {
132 $status = $r->call_exception($error);
133 if ( $status != OK ) {
134 warn "caught view error: $error" if $r->debug;
135 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
145 my $config = $self->config;
146 $config->ok_tables || $config->ok_tables( $config->display_tables );
147 $config->ok_tables( { map { $_ => 1 } @{ $config->ok_tables } } )
148 if ref $config->ok_tables eq "ARRAY";
149 warn "We don't have that table ($self->{table}).\n"
150 . "Available tables are: "
151 . join( ",", @{ $config->{display_tables} } )
153 and not $config->ok_tables->{ $self->{table} }
155 return DECLINED() unless exists $config->ok_tables->{ $self->{table} };
158 return DECLINED unless $self->model_class->is_public( $self->{action} );
162 sub call_authenticate {
165 # Check if we have a model class
166 if ( $self->{model_class} ) {
167 return $self->model_class->authenticate($self)
168 if $self->model_class->can("authenticate");
170 return $self->authenticate($self); # Interface consistency is a Good Thing
177 # Check if we have a model class
178 if ( $self->{model_class}
179 && $self->model_class->can('exception') )
181 my $status = $self->model_class->exception( $self, $error );
182 return $status if $status == OK;
184 return $self->exception($error);
187 sub additional_data { }
189 sub authenticate { return OK }
191 sub exception { return ERROR }
195 $self->{path} ||= "frontpage";
196 my @pi = $self->{path} =~ m{([^/]+)/?}g;
197 $self->{table} = shift @pi;
198 $self->{action} = shift @pi;
199 $self->{action} ||= "index";
200 $self->{args} = \@pi;
203 sub param { # like CGI::param(), but read-only
207 unless (exists $r->{params}{$key}) {
208 return wantarray() ? () : undef;
210 my $val = $r->{params}{$key};
212 return ref $val ? @$val : $val;
214 return ref $val ? $val->[0] : $val;
217 return keys %{$r->{params}};
221 sub get_template_root { "." }
225 die "Do not use Maypole directly; use Apache::MVC or similar";
229 die "Do not use Maypole directly; use Apache::MVC or similar";
232 # Session and Repeat Submission Handling
235 use Maypole::Session;
236 return Maypole::Session::generate_unique_id();
241 Maypole - MVC web application framework
245 See L<Maypole::Application>.
249 This documents the Maypole request object. See the L<Maypole::Manual>, for a
250 detailed guide to using Maypole.
252 Maypole is a Perl web application framework similar to Java's struts. It is
253 essentially completely abstracted, and so doesn't know anything about
254 how to talk to the outside world.
256 To use it, you need to create a package which represents your entire
257 application. In our example above, this is the C<BeerDB> package.
259 This needs to first use L<Maypole::Application> which will make your package
260 inherit from the appropriate platform driver such as C<Apache::MVC> or
261 C<CGI::Maypole>, and then call setup. This sets up the model classes and
262 configures your application. The default model class for Maypole uses
263 L<Class::DBI> to map a database to classes, but this can be changed by altering
264 configuration. (B<Before> calling setup.)
270 Returns the L<Maypole::Config> object
274 My::App->setup($data_source, $user, $password, \%attr);
276 Initialise the maypole application and model classes. Your application should
277 call this after setting configuration via L<"config">
281 You should not call this directly, but you may wish to override this to
283 application-specific initialisation.
287 Get/set the Maypole::View object
291 sub My::App::debug {1}
293 Returns the debugging flag. Override this in your application class to
294 enable/disable debugging.
296 =head2 INSTANCE METHODS
298 =head3 parse_location
300 Turns the backend request (e.g. Apache::MVC, Maypole, CGI) into a
302 request. It does this by setting the C<path>, and invoking C<parse_path>
306 You should only need to define this method if you are writing a new
312 Returns the request path
316 Parses the request path and sets the C<args>, C<action> and C<table>
321 The table part of the Maypole request path
325 The action part of the Maypole request path
329 A list of remaining parts of the request path after table and action
335 A L<Maypole::Headers> object containing HTTP headers for the request
339 A L<HTTP::Headers> object that contains HTTP headers for the output
343 Turns post data and query string paramaters into a hash of C<params>.
345 You should only need to define this method if you are writing a new
351 An accessor for request parameters. It behaves similarly to CGI::param() for
352 accessing CGI parameters.
356 Returns a hash of request parameters. The source of the parameters may vary
357 depending on the Maypole backend, but they are usually populated from request
358 query string and POST data.
360 B<Note:> Where muliple values of a parameter were supplied, the
363 will be an array reference.
365 =head3 get_template_root
367 Implementation-specific path to template root.
369 You should only need to define this method if you are writing a new
371 backend. Otherwise, see L<Maypole::Config/"template_root">
375 You should only need to define this method if you are writing a new
376 Maypole backend. It should return something that looks like an Apache
377 or CGI request object, it defaults to blank.
382 Returns a Maypole::Constant to indicate whether the request is valid.
384 The default implementation checks that C<$r-E<gt>table> is publicly
386 and that the model class is configured to handle the C<$r-E<gt>action>
390 Returns a Maypole::Constant to indicate whether the user is
394 The default implementation returns C<OK>
398 Returns the perl package name that will serve as the model for the
399 request. It corresponds to the request C<table> attribute.
401 =head3 additional_data
403 Called before the model processes the request, this method gives you a
405 to do some processing for each request, for example, manipulating
410 Get/set a list of model objects. The objects will be accessible in the
414 If the first item in C<$r-E<gt>args> can be C<retrieve()>d by the model
416 it will be removed from C<args> and the retrieved object will be added
418 C<objects> list. See L<Maypole::Model> for more information.
422 $r->template_args->{foo} = 'bar';
424 Get/set a hash of template variables.
428 Get/set the template to be used by the view. By default, it returns
433 This method is called if any exceptions are raised during the
436 model/view processing. It should accept the exception as a parameter and
438 a Maypole::Constant to indicate whether the request should continue to
444 Get/set a request error
448 Get/set the response output. This is usually populated by the view
450 can skip view processing by setting the C<output>.
452 =head3 document_encoding
454 Get/set the output encoding. Default: utf-8.
458 Get/set the output content type. Default: text/html
462 Sends the output and additional headers to the user.
464 =head3 call_authenticate
466 This method first checks if the relevant model class
467 can authenticate the user, or falls back to the default
468 authenticate method of your Maypole application.
471 =head3 call_exception
473 This model is called to catch exceptions, first after authenticate, then after
474 processing the model class, and finally to check for exceptions from the view
477 This method first checks if the relevant model class
478 can handle exceptions the user, or falls back to the default
479 exception method of your Maypole application.
481 =head3 make_random_id
483 returns a unique id for this request can be used to prevent or detect repeat submissions.
487 This method sets up the class if it's not done yet, sets some
488 defaults and leaves the dirty work to handler_guts.
492 This is the core of maypole. You don't want to know.
496 There's more documentation, examples, and a information on our mailing lists
497 at the Maypole web site:
499 L<http://maypole.perl.org/>
501 L<Maypole::Application>, L<Apache::MVC>, L<CGI::Maypole>.
505 Maypole is currently maintained by Simon Flack C<simonflk#cpan.org>
507 =head1 AUTHOR EMERITUS
509 Simon Cozens, C<simon#cpan.org>
511 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
515 Sebastian Riedel, Danijel Milicevic, Dave Slack, Jesse Sheidlower, Jody Belka,
516 Marcus Ramberg, Mickael Joanne, Randal Schwartz, Simon Flack, Steve Simms,
517 Veljko Vidovic and all the others who've helped.
521 You may distribute this code under the same terms as Perl itself.