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
39 "Couldn't load the model class $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 return $status unless $status == OK;
99 # The root of all evil
106 my $applicable = $r->is_applicable;
108 unless ( $applicable == OK )
110 # It's just a plain template
111 $r->model_class(undef);
114 $path =~ s{/$}{}; # De-absolutify
117 $r->template($r->path);
120 # We authenticate every request, needed for proper session management
123 eval { $status = $r->call_authenticate };
125 if ( my $error = $@ )
127 $status = $r->call_exception($error);
131 warn "caught authenticate error: $error";
132 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
136 if ( $r->debug and $status != OK and $status != DECLINED )
138 $r->view_object->error( $r,
139 "Got unexpected status $status from calling authentication" );
142 return $status unless $status == OK;
144 # We run additional_data for every request
147 if ( $applicable == OK )
149 eval { $r->model_class->process($r) };
151 if ( my $error = $@ )
153 $status = $r->call_exception($error);
156 warn "caught model error: $error";
157 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
163 { # You might want to do it yourself
164 eval { $status = $r->view_object->process($r) };
166 if ( my $error = $@ )
168 $status = $r->call_exception($error);
172 warn "caught view error: $error" if $r->debug;
173 return $r->debug ? $r->view_object->error($r, $error) : ERROR;
186 $r->model_class( $r->config->model->class_of($r, $r->table) );
191 my $config = $self->config;
192 $config->ok_tables || $config->ok_tables( $config->display_tables );
193 $config->ok_tables( { map { $_ => 1 } @{ $config->ok_tables } } )
194 if ref $config->ok_tables eq "ARRAY";
195 warn "We don't have that table ($self->{table}).\n"
196 . "Available tables are: "
197 . join( ",", @{ $config->{display_tables} } )
199 and not $config->ok_tables->{ $self->{table} }
201 return DECLINED() unless exists $config->ok_tables->{ $self->{table} };
204 return DECLINED unless $self->model_class->is_public( $self->{action} );
208 sub call_authenticate {
211 # Check if we have a model class
212 if ( $self->{model_class} ) {
213 return $self->model_class->authenticate($self)
214 if $self->model_class->can("authenticate");
216 return $self->authenticate($self); # Interface consistency is a Good Thing
223 # Check if we have a model class
224 if ( $self->{model_class}
225 && $self->model_class->can('exception') )
227 my $status = $self->model_class->exception( $self, $error );
228 return $status if $status == OK;
230 return $self->exception($error);
233 sub additional_data { }
235 sub authenticate { return OK }
237 sub exception { return ERROR }
241 $self->{path} ||= "frontpage";
242 my @pi = $self->{path} =~ m{([^/]+)/?}g;
243 $self->{table} = shift @pi;
244 $self->{action} = shift @pi;
245 $self->{action} ||= "index";
246 $self->{args} = \@pi;
249 sub param { # like CGI::param(), but read-only
253 unless (exists $r->{params}{$key}) {
254 return wantarray() ? () : undef;
256 my $val = $r->{params}{$key};
258 return ref $val ? @$val : $val;
260 return ref $val ? $val->[0] : $val;
263 return keys %{$r->{params}};
267 sub get_template_root { "." }
271 die "Do not use Maypole directly; use Apache::MVC or similar";
275 die "Do not use Maypole directly; use Apache::MVC or similar";
278 # Session and Repeat Submission Handling
281 use Maypole::Session;
282 return Maypole::Session::generate_unique_id();
287 Maypole - MVC web application framework
291 See L<Maypole::Application>.
295 This documents the Maypole request object. See the L<Maypole::Manual>, for a
296 detailed guide to using Maypole.
298 Maypole is a Perl web application framework similar to Java's struts. It is
299 essentially completely abstracted, and so doesn't know anything about
300 how to talk to the outside world.
302 To use it, you need to create a package which represents your entire
303 application. In our example above, this is the C<BeerDB> package.
305 This needs to first use L<Maypole::Application> which will make your package
306 inherit from the appropriate platform driver such as C<Apache::MVC> or
307 C<CGI::Maypole>, and then call setup. This sets up the model classes and
308 configures your application. The default model class for Maypole uses
309 L<Class::DBI> to map a database to classes, but this can be changed by altering
310 configuration. (B<Before> calling setup.)
316 Returns the L<Maypole::Config> object
320 My::App->setup($data_source, $user, $password, \%attr);
322 Initialise the maypole application and model classes. Your application should
323 call this after setting configuration via L<"config">
327 You should not call this directly, but you may wish to override this to
329 application-specific initialisation.
333 Get/set the Maypole::View object
337 sub My::App::debug {1}
339 Returns the debugging flag. Override this in your application class to
340 enable/disable debugging.
342 =head2 INSTANCE METHODS
344 =head3 parse_location
346 Turns the backend request (e.g. Apache::MVC, Maypole, CGI) into a
348 request. It does this by setting the C<path>, and invoking C<parse_path>
352 You should only need to define this method if you are writing a new
358 Returns the request path
362 Parses the request path and sets the C<args>, C<action> and C<table>
367 The table part of the Maypole request path
371 The action part of the Maypole request path
375 A list of remaining parts of the request path after table and action
381 A L<Maypole::Headers> object containing HTTP headers for the request
385 A L<HTTP::Headers> object that contains HTTP headers for the output
389 Turns post data and query string paramaters into a hash of C<params>.
391 You should only need to define this method if you are writing a new
397 An accessor for request parameters. It behaves similarly to CGI::param() for
398 accessing CGI parameters.
402 Returns a hash of request parameters. The source of the parameters may vary
403 depending on the Maypole backend, but they are usually populated from request
404 query string and POST data.
406 B<Note:> Where muliple values of a parameter were supplied, the
409 will be an array reference.
411 =head3 get_template_root
413 Implementation-specific path to template root.
415 You should only need to define this method if you are writing a new
417 backend. Otherwise, see L<Maypole::Config/"template_root">
421 You should only need to define this method if you are writing a new
422 Maypole backend. It should return something that looks like an Apache
423 or CGI request object, it defaults to blank.
428 Returns a Maypole::Constant to indicate whether the request is valid.
430 The default implementation checks that C<$r-E<gt>table> is publicly
432 and that the model class is configured to handle the C<$r-E<gt>action>
436 Returns a Maypole::Constant to indicate whether the user is
440 The default implementation returns C<OK>
444 Returns the perl package name that will serve as the model for the
445 request. It corresponds to the request C<table> attribute.
447 =head3 additional_data
449 Called before the model processes the request, this method gives you a
451 to do some processing for each request, for example, manipulating
456 Get/set a list of model objects. The objects will be accessible in the
460 If the first item in C<$r-E<gt>args> can be C<retrieve()>d by the model
462 it will be removed from C<args> and the retrieved object will be added
464 C<objects> list. See L<Maypole::Model> for more information.
468 $r->template_args->{foo} = 'bar';
470 Get/set a hash of template variables.
474 Get/set the template to be used by the view. By default, it returns
479 This method is called if any exceptions are raised during the
482 model/view processing. It should accept the exception as a parameter and
484 a Maypole::Constant to indicate whether the request should continue to
490 Get/set a request error
494 Get/set the response output. This is usually populated by the view
496 can skip view processing by setting the C<output>.
498 =head3 document_encoding
500 Get/set the output encoding. Default: utf-8.
504 Get/set the output content type. Default: text/html
508 Sends the output and additional headers to the user.
510 =head3 call_authenticate
512 This method first checks if the relevant model class
513 can authenticate the user, or falls back to the default
514 authenticate method of your Maypole application.
517 =head3 call_exception
519 This model is called to catch exceptions, first after authenticate, then after
520 processing the model class, and finally to check for exceptions from the view
523 This method first checks if the relevant model class
524 can handle exceptions the user, or falls back to the default
525 exception method of your Maypole application.
527 =head3 make_random_id
529 returns a unique id for this request can be used to prevent or detect repeat submissions.
533 This method sets up the class if it's not done yet, sets some
534 defaults and leaves the dirty work to handler_guts.
538 This is the core of maypole. You don't want to know.
542 There's more documentation, examples, and a information on our mailing lists
543 at the Maypole web site:
545 L<http://maypole.perl.org/>
547 L<Maypole::Application>, L<Apache::MVC>, L<CGI::Maypole>.
551 Maypole is currently maintained by Simon Flack C<simonflk#cpan.org>
553 =head1 AUTHOR EMERITUS
555 Simon Cozens, C<simon#cpan.org>
557 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
561 Sebastian Riedel, Danijel Milicevic, Dave Slack, Jesse Sheidlower, Jody Belka,
562 Marcus Ramberg, Mickael Joanne, Randal Schwartz, Simon Flack, Steve Simms,
563 Veljko Vidovic and all the others who've helped.
567 You may distribute this code under the same terms as Perl itself.