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 );
63 # See Maypole::Workflow before trying to understand this.
64 my ($class, $req) = @_;
66 $class->init unless $class->init_done;
68 # Create the request object
71 config => $class->config
73 $r->headers_out(Maypole::Headers->new);
74 $r->get_request($req);
76 my $status = $r->handler_guts();
77 return $status unless $status == OK;
82 # The root of all evil
85 $r->model_class( $r->config->model->class_of( $r, $r->{table} ) );
87 my $applicable = $r->is_applicable;
88 unless ( $applicable == OK ) {
90 # It's just a plain template
91 delete $r->{model_class};
92 $r->{path} =~ s{/$}{}; # De-absolutify
93 $r->template( $r->{path} );
96 # We authenticate every request, needed for proper session management
98 eval { $status = $r->call_authenticate };
99 if ( my $error = $@ ) {
100 $status = $r->call_exception($error);
101 if ( $status != OK ) {
102 warn "caught authenticate error: $error";
103 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
106 if ( $r->debug and $status != OK and $status != DECLINED ) {
107 $r->view_object->error( $r,
108 "Got unexpected status $status from calling authentication" );
110 return $status unless $status == OK;
112 # We run additional_data for every request
114 if ( $applicable == OK ) {
115 eval { $r->model_class->process($r) };
116 if ( my $error = $@ ) {
117 $status = $r->call_exception($error);
118 if ( $status != OK ) {
119 warn "caught model error: $error";
120 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
124 if ( !$r->{output} ) { # You might want to do it yourself
125 eval { $status = $r->view_object->process($r) };
126 if ( my $error = $@ ) {
127 $status = $r->call_exception($error);
128 if ( $status != OK ) {
129 warn "caught view error: $error" if $r->debug;
130 return $r->debug ? $r->view_object->error( $r, $error ) : ERROR;
140 my $config = $self->config;
141 $config->ok_tables || $config->ok_tables( $config->display_tables );
142 $config->ok_tables( { map { $_ => 1 } @{ $config->ok_tables } } )
143 if ref $config->ok_tables eq "ARRAY";
144 warn "We don't have that table ($self->{table}).\n"
145 . "Available tables are: "
146 . join( ",", @{ $config->{display_tables} } )
148 and not $config->ok_tables->{ $self->{table} }
150 return DECLINED() unless exists $config->ok_tables->{ $self->{table} };
153 return DECLINED unless $self->model_class->is_public( $self->{action} );
157 sub call_authenticate {
160 # Check if we have a model class
161 if ( $self->{model_class} ) {
162 return $self->model_class->authenticate($self)
163 if $self->model_class->can("authenticate");
165 return $self->authenticate($self); # Interface consistency is a Good Thing
172 # Check if we have a model class
173 if ( $self->{model_class}
174 && $self->model_class->can('exception') )
176 my $status = $self->model_class->exception( $self, $error );
177 return $status if $status == OK;
179 return $self->exception($error);
182 sub additional_data { }
184 sub authenticate { return OK }
186 sub exception { return ERROR }
190 $self->{path} ||= "frontpage";
191 my @pi = $self->{path} =~ m{([^/]+)/?}g;
192 $self->{table} = shift @pi;
193 $self->{action} = shift @pi;
194 $self->{action} ||= "index";
195 $self->{args} = \@pi;
198 sub param { # like CGI::param(), but read-only
202 unless (exists $r->{params}{$key}) {
203 return wantarray() ? () : undef;
205 my $val = $r->{params}{$key};
207 return ref $val ? @$val : $val;
209 return ref $val ? $val->[0] : $val;
212 return keys %{$r->{params}};
216 sub get_template_root { "." }
220 die "Do not use Maypole directly; use Apache::MVC or similar";
224 die "Do not use Maypole directly; use Apache::MVC or similar";
227 # Session and Repeat Submission Handling
230 use Maypole::Session;
231 return Maypole::Session::generate_unique_id();
236 Maypole - MVC web application framework
240 See L<Maypole::Application>.
244 This documents the Maypole request object. See the L<Maypole::Manual>, for a
245 detailed guide to using Maypole.
247 Maypole is a Perl web application framework similar to Java's struts. It is
248 essentially completely abstracted, and so doesn't know anything about
249 how to talk to the outside world.
251 To use it, you need to create a package which represents your entire
252 application. In our example above, this is the C<BeerDB> package.
254 This needs to first use L<Maypole::Application> which will make your package
255 inherit from the appropriate platform driver such as C<Apache::MVC> or
256 C<CGI::Maypole>, and then call setup. This sets up the model classes and
257 configures your application. The default model class for Maypole uses
258 L<Class::DBI> to map a database to classes, but this can be changed by altering
259 configuration. (B<Before> calling setup.)
265 Returns the L<Maypole::Config> object
269 My::App->setup($data_source, $user, $password, \%attr);
271 Initialise the maypole application and model classes. Your application should
272 call this after setting configuration via L<"config">
276 You should not call this directly, but you may wish to override this to
278 application-specific initialisation.
282 Get/set the Maypole::View object
286 sub My::App::debug {1}
288 Returns the debugging flag. Override this in your application class to
289 enable/disable debugging.
291 =head2 INSTANCE METHODS
293 =head3 parse_location
295 Turns the backend request (e.g. Apache::MVC, Maypole, CGI) into a
297 request. It does this by setting the C<path>, and invoking C<parse_path>
301 You should only need to define this method if you are writing a new
307 Returns the request path
311 Parses the request path and sets the C<args>, C<action> and C<table>
316 The table part of the Maypole request path
320 The action part of the Maypole request path
324 A list of remaining parts of the request path after table and action
330 A L<Maypole::Headers> object containing HTTP headers for the request
334 A L<HTTP::Headers> object that contains HTTP headers for the output
338 Turns post data and query string paramaters into a hash of C<params>.
340 You should only need to define this method if you are writing a new
346 An accessor for request parameters. It behaves similarly to CGI::param() for
347 accessing CGI parameters.
351 Returns a hash of request parameters. The source of the parameters may vary
352 depending on the Maypole backend, but they are usually populated from request
353 query string and POST data.
355 B<Note:> Where muliple values of a parameter were supplied, the
358 will be an array reference.
360 =head3 get_template_root
362 Implementation-specific path to template root.
364 You should only need to define this method if you are writing a new
366 backend. Otherwise, see L<Maypole::Config/"template_root">
370 You should only need to define this method if you are writing a new
371 Maypole backend. It should return something that looks like an Apache
372 or CGI request object, it defaults to blank.
377 Returns a Maypole::Constant to indicate whether the request is valid.
379 The default implementation checks that C<$r-E<gt>table> is publicly
381 and that the model class is configured to handle the C<$r-E<gt>action>
385 Returns a Maypole::Constant to indicate whether the user is
389 The default implementation returns C<OK>
393 Returns the perl package name that will serve as the model for the
394 request. It corresponds to the request C<table> attribute.
396 =head3 additional_data
398 Called before the model processes the request, this method gives you a
400 to do some processing for each request, for example, manipulating
405 Get/set a list of model objects. The objects will be accessible in the
409 If the first item in C<$r-E<gt>args> can be C<retrieve()>d by the model
411 it will be removed from C<args> and the retrieved object will be added
413 C<objects> list. See L<Maypole::Model> for more information.
417 $r->template_args->{foo} = 'bar';
419 Get/set a hash of template variables.
423 Get/set the template to be used by the view. By default, it returns
428 This method is called if any exceptions are raised during the
431 model/view processing. It should accept the exception as a parameter and
433 a Maypole::Constant to indicate whether the request should continue to
439 Get/set a request error
443 Get/set the response output. This is usually populated by the view
445 can skip view processing by setting the C<output>.
447 =head3 document_encoding
449 Get/set the output encoding. Default: utf-8.
453 Get/set the output content type. Default: text/html
457 Sends the output and additional headers to the user.
459 =head3 call_authenticate
461 This method first checks if the relevant model class
462 can authenticate the user, or falls back to the default
463 authenticate method of your Maypole application.
466 =head3 call_exception
468 This model is called to catch exceptions, first after authenticate, then after
469 processing the model class, and finally to check for exceptions from the view
472 This method first checks if the relevant model class
473 can handle exceptions the user, or falls back to the default
474 exception method of your Maypole application.
476 =head3 make_random_id
478 returns a unique id for this request can be used to prevent or detect repeat submissions.
482 This method sets up the class if it's not done yet, sets some
483 defaults and leaves the dirty work to handler_guts.
487 This is the core of maypole. You don't want to know.
491 There's more documentation, examples, and a information on our mailing lists
492 at the Maypole web site:
494 L<http://maypole.perl.org/>
496 L<Maypole::Application>, L<Apache::MVC>, L<CGI::Maypole>.
500 Maypole is currently maintained by Simon Flack C<simonflk#cpan.org>
502 =head1 AUTHOR EMERITUS
504 Simon Cozens, C<simon#cpan.org>
506 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
510 Sebastian Riedel, Danijel Milicevic, Dave Slack, Jesse Sheidlower, Jody Belka,
511 Marcus Ramberg, Mickael Joanne, Randal Schwartz, Simon Flack, Steve Simms,
512 Veljko Vidovic and all the others who've helped.
516 You may distribute this code under the same terms as Perl itself.