2 use base qw(Class::Accessor::Fast Class::Data::Inheritable);
3 use UNIVERSAL::require;
7 use Maypole::Constants;
10 our $VERSION = '2.11';
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*
16 # - double leading underscore - private to the current package
18 __PACKAGE__->mk_classdata($_) for qw( config init_done view_object );
19 __PACKAGE__->mk_accessors(
20 qw( params query objects model_class template_args output path
21 args action template error document_encoding content_type table
22 headers_in headers_out stash)
24 __PACKAGE__->config( Maypole::Config->new() );
25 __PACKAGE__->init_done(0);
31 my $calling_class = shift;
33 $calling_class = ref $calling_class if ref $calling_class;
35 my $config = $calling_class->config;
37 $config->model || $config->model('Maypole::Model::CDBI');
39 $config->model->require or die sprintf
40 "Couldn't load the model class %s: %s", $config->model, $@;
42 $config->model->setup_database($config, $calling_class, @_);
44 foreach my $subclass ( @{ $config->classes } )
47 unshift @{ $subclass . "::ISA" }, $config->model;
48 $config->model->adopt($subclass)
49 if $config->model->can("adopt");
56 my $config = $class->config;
57 $config->view || $config->view("Maypole::View::TT");
58 $config->view->require;
59 die "Couldn't load the view class " . $config->view . ": $@" if $@;
60 $config->display_tables
61 || $config->display_tables( $class->config->tables );
62 $class->view_object( $class->config->view->new );
73 config => $class->config,
79 # handler() has a method attribute so that mod_perl will invoke
80 # BeerDB->handler() as a method rather than a plain function
81 # BeerDB::handler() and so this inherited implementation will be
82 # found. See e.g. "Practical mod_perl" by Bekman & Cholet for
83 # more information <http://modperlbook.org/html/ch25_01.html>
86 # See Maypole::Workflow before trying to understand this.
87 my ($class, $req) = @_;
89 $class->init unless $class->init_done;
91 my $self = $class->new;
93 # initialise the request
94 $self->headers_out(Maypole::Headers->new);
95 $self->get_request($req);
96 $self->parse_location;
98 my $status = $self->handler_guts;
100 # moving this here causes unit test failures - need to check why
101 # before committing the move
102 #$status = $self->__call_process_view unless $self->output;
104 return $status unless $status == OK;
111 # The root of all evil
118 my $applicable = $self->is_model_applicable;
120 $self->__setup_plain_template unless $applicable;
122 # We authenticate every request, needed for proper session management
125 eval { $status = $self->call_authenticate };
127 if ( my $error = $@ )
129 $status = $self->call_exception($error);
133 warn "caught authenticate error: $error";
134 return $self->debug ?
135 $self->view_object->error($self, $error) : ERROR;
139 if ( $self->debug and $status != OK and $status != DECLINED )
141 $self->view_object->error( $self,
142 "Got unexpected status $status from calling authentication" );
145 return $status unless $status == OK;
147 # We run additional_data for every request
148 $self->additional_data;
152 eval { $self->model_class->process($self) };
154 if ( my $error = $@ )
156 $status = $self->call_exception($error);
160 warn "caught model error: $error";
161 return $self->debug ?
162 $self->view_object->error($self, $error) : ERROR;
167 # less frequent path - perhaps output has been set to an error message
168 return OK if $self->output;
170 # normal path - no output has been generated yet
171 return $self->__call_process_view;
174 # is_applicable() returned false, so set up a plain template. Model processing
175 # will be skipped, but need to remove the model anyway so the template can't
177 sub __setup_plain_template
181 # It's just a plain template
182 $self->model_class(undef);
184 my $path = $self->path;
185 $path =~ s{/$}{}; # De-absolutify
188 $self->template($self->path);
191 # The model has been processed or skipped (if is_applicable returned false),
192 # any exceptions have been handled, and there's no content in $self->output
193 sub __call_process_view
199 eval { $status = $self->view_object->process($self) };
201 if ( my $error = $@ )
203 $status = $self->call_exception($error);
207 warn "caught view error: $error" if $self->debug;
208 return $self->debug ?
209 $self->view_object->error($self, $error) : ERROR;
219 $self->model_class( $self->config->model->class_of($self, $self->table) );
222 # is_applicable() should return true or false, not OK or DECLINED, because
223 # the return value is never used as the return value from handler(). There's
224 # probably a lot of code out there supplying the return codes though, so
225 # instead of changing is_applicable() to return 0 or 1, the return value is
226 # passed through __to_boolean. I think it helps handler_guts() if we don't
227 # have multiple sets of return codes being checked for different things -drb.
228 sub is_model_applicable
232 # cater for applications that are using obsolete version
233 if ($self->can('is_applicable'))
235 warn "DEPRECATION WARNING: rewrite is_applicable to the interface ".
236 "of Maypole::is_model_applicable\n";
237 return $self->is_applicable == OK;
240 # Establish which tables should be processed by the model
241 my $config = $self->config;
243 $config->ok_tables || $config->ok_tables( $config->display_tables );
245 $config->ok_tables( { map { $_ => 1 } @{ $config->ok_tables } } )
246 if ref $config->ok_tables eq "ARRAY";
248 my $ok_tables = $config->ok_tables;
250 # Does this request concern a table to be processed by the model?
251 my $table = $self->table;
255 if (exists $ok_tables->{$table})
259 # implements tj's default_table_view(), but there's no _default_table_view()
260 # or _have_default_table_view() yet
263 # $ok = $self->default_table_view($self->path, $self->args)
264 # if $self->_have_default_table_view;
269 warn "We don't have that table ($table).\n"
270 . "Available tables are: "
271 . join( ",", keys %$ok_tables )
272 if $self->debug and not $ok_tables->{$table};
277 # Is the action public?
278 my $action = $self->action;
279 return 1 if $self->model_class->is_public($action);
281 warn "The action '$action' is not applicable to the table $table"
287 # *only* intended for translating the return code from is_applicable()
288 sub __to_boolean { $_[0] == OK ? 1 : 0 }
290 sub call_authenticate
294 # Check if we have a model class with an authenticate() to delegate to
295 return $self->model_class->authenticate($self)
296 if $self->model_class and $self->model_class->can('authenticate');
298 # Interface consistency is a Good Thing -
299 # the invocant and the argument may one day be different things
300 # (i.e. controller and request), like they are when authenticate()
301 # is called on a model class (i.e. model and request)
302 return $self->authenticate($self);
307 my ($self, $error) = @_;
309 # Check if we have a model class with an exception() to delegate to
310 if ( $self->model_class && $self->model_class->can('exception') )
312 my $status = $self->model_class->exception( $self, $error );
313 return $status if $status == OK;
316 return $self->exception($error);
319 sub default_table_view {
320 my ($self,$path,$args) = @_;
322 my $default_table_view = __PACKAGE__->_default_table_view;
323 # (path class action field)
324 my @path = $self->{path} =~ m{([^/]+)/?}g;
325 my $search_value = shift(@path);
326 if ($default_table_view->{path}) {
327 if ($default_table_view->{path} eq $search_value) {
328 $search_value = shift(@path);
334 $self->{table} = $default_table_view->{class};
335 $self->{action} = $default_table_view->{action};
336 $self->{args} = [ $search_value,@path ];
340 sub additional_data { }
342 sub authenticate { return OK }
344 sub exception { return ERROR }
350 $self->path || $self->path('frontpage');
352 my @pi = grep {length} split '/', $self->path;
354 $self->table(shift @pi);
356 $self->action( shift @pi or 'index' );
361 # like CGI::param(), but read only
364 my ($self, $key) = @_;
366 return keys %{$self->params} unless defined $key;
368 return unless exists $self->params->{$key};
370 my $val = $self->params->{$key};
372 return ref $val ? @$val : ($val) if wantarray;
374 return ref $val ? $val->[0] : $val;
377 sub get_template_root {'.'}
381 die "get_protocol is a virtual method. Do not use Maypole directly; use Apache::MVC or similar";
385 die "parse_location is a virtual method. Do not use Maypole directly; use Apache::MVC or similar";
388 sub redirect_request {
389 die "parse_location is a virtual method. Do not use Maypole directly; use Apache::MVC or similar";
392 sub redirect_internal_request {
397 die "send_output is a virtual method. Do not use Maypole directly; use Apache::MVC or similar";
400 # Session and Repeat Submission Handling
403 use Maypole::Session;
404 return Maypole::Session::generate_unique_id();
409 Maypole - MVC web application framework
413 See L<Maypole::Application>.
417 This documents the Maypole request object. See the L<Maypole::Manual>, for a
418 detailed guide to using Maypole.
420 Maypole is a Perl web application framework similar to Java's struts. It is
421 essentially completely abstracted, and so doesn't know anything about
422 how to talk to the outside world.
424 To use it, you need to create a package which represents your entire
425 application. In our example above, this is the C<BeerDB> package.
427 This needs to first use L<Maypole::Application> which will make your package
428 inherit from the appropriate platform driver such as C<Apache::MVC> or
429 C<CGI::Maypole>, and then call setup. This sets up the model classes and
430 configures your application. The default model class for Maypole uses
431 L<Class::DBI> to map a database to classes, but this can be changed by altering
432 configuration. (B<Before> calling setup.)
438 Returns the L<Maypole::Config> object
442 My::App->setup($data_source, $user, $password, \%attr);
444 Initialise the maypole application and model classes. Your application should
445 call this after setting configuration via L<"config">
449 You should not call this directly, but you may wish to override this to
451 application-specific initialisation.
455 Constructs a very minimal new Maypole request object.
459 Get/set the Maypole::View object
463 sub My::App::debug {1}
465 Returns the debugging flag. Override this in your application class to
466 enable/disable debugging.
468 =head2 INSTANCE METHODS
470 =head3 parse_location
472 Turns the backend request (e.g. Apache::MVC, Maypole, CGI) into a
474 request. It does this by setting the C<path>, and invoking C<parse_path>
478 You should only need to define this method if you are writing a new
484 Returns the request path
488 Parses the request path and sets the C<args>, C<action> and C<table>
493 The table part of the Maypole request path
497 The action part of the Maypole request path
501 A list of remaining parts of the request path after table and action
507 A L<Maypole::Headers> object containing HTTP headers for the request
511 A L<HTTP::Headers> object that contains HTTP headers for the output
515 Turns post data and query string paramaters into a hash of C<params>.
517 You should only need to define this method if you are writing a new
523 An accessor for request parameters. It behaves similarly to CGI::param() for
524 accessing CGI parameters.
528 Returns a hash of request parameters. The source of the parameters may vary
529 depending on the Maypole backend, but they are usually populated from request
530 query string and POST data.
532 B<Note:> Where muliple values of a parameter were supplied, the
535 will be an array reference.
537 =head3 get_template_root
539 Implementation-specific path to template root.
541 You should only need to define this method if you are writing a new
543 backend. Otherwise, see L<Maypole::Config/"template_root">
547 You should only need to define this method if you are writing a new
548 Maypole backend. It should return something that looks like an Apache
549 or CGI request object, it defaults to blank.
551 =head3 default_table_view
555 Returns a Maypole::Constant to indicate whether the request is valid.
557 B<This method is deprecated> as of version 2.11. If you have overridden it,
558 please override C<is_model_applicable> instead, and change the return type
559 from Maypole:Constants to true/false.
561 =head3 is_model_applicable
563 Returns true or false to indicate whether the request is valid.
565 The default implementation checks that C<< $r->table >> is publicly
566 accessible and that the model class is configured to handle the
571 Returns a Maypole::Constant to indicate whether the user is
575 The default implementation returns C<OK>
579 Returns the perl package name that will serve as the model for the
580 request. It corresponds to the request C<table> attribute.
582 =head3 additional_data
584 Called before the model processes the request, this method gives you a
586 to do some processing for each request, for example, manipulating
591 Get/set a list of model objects. The objects will be accessible in the
595 If the first item in C<$self-E<gt>args> can be C<retrieve()>d by the model
597 it will be removed from C<args> and the retrieved object will be added
599 C<objects> list. See L<Maypole::Model> for more information.
603 $self->template_args->{foo} = 'bar';
605 Get/set a hash of template variables.
609 A place to put custom application data. Not used by Maypole itself.
613 Get/set the template to be used by the view. By default, it returns
618 This method is called if any exceptions are raised during the
621 model/view processing. It should accept the exception as a parameter and
623 a Maypole::Constant to indicate whether the request should continue to
629 Get/set a request error
633 Get/set the response output. This is usually populated by the view
635 can skip view processing by setting the C<output>.
637 =head3 document_encoding
639 Get/set the output encoding. Default: utf-8.
643 Get/set the output content type. Default: text/html
647 Sends the output and additional headers to the user.
649 =head3 call_authenticate
651 This method first checks if the relevant model class
652 can authenticate the user, or falls back to the default
653 authenticate method of your Maypole application.
656 =head3 call_exception
658 This model is called to catch exceptions, first after authenticate, then after
659 processing the model class, and finally to check for exceptions from the view
662 This method first checks if the relevant model class
663 can handle exceptions the user, or falls back to the default
664 exception method of your Maypole application.
666 =head3 make_random_id
668 returns a unique id for this request can be used to prevent or detect repeat
673 Returns the protocol the request was made with, i.e. https
675 =head3 redirect_request
677 Sets output headers to redirect based on the arguments provided
679 Accepts either a single argument of the full url to redirect to, or a hash of named parameters :
681 $r->redirect_request('http://www.example.com/path');
685 $r->redirect_request(protocol=>'https', domain=>'www.example.com', path=>'/path/file?arguments', status=>'302', url=>'..');
687 The named parameters are protocol, domain, path, status and url
689 Only 1 named parameter is required but other than url, they can be combined as required and current values (from the request) will be used in place of any missing arguments. The url argument must be a full url including protocol and can only be combined with status.
693 This method sets up the class if it's not done yet, sets some
694 defaults and leaves the dirty work to handler_guts.
698 This is the main request handling method and calls various methods to handle the request/response
699 and defines the workflow within Maypole.
701 Currently undocumented and liable to be refactored without warning.
705 There's more documentation, examples, and a information on our mailing lists
706 at the Maypole web site:
708 L<http://maypole.perl.org/>
710 L<Maypole::Application>, L<Apache::MVC>, L<CGI::Maypole>.
714 Maypole is currently maintained by Simon Flack C<simonflk#cpan.org>
716 =head1 AUTHOR EMERITUS
718 Simon Cozens, C<simon#cpan.org>
720 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
724 Sebastian Riedel, Danijel Milicevic, Dave Slack, Jesse Sheidlower, Jody Belka,
725 Marcus Ramberg, Mickael Joanne, Randal Schwartz, Simon Flack, Steve Simms,
726 Veljko Vidovic and all the others who've helped.
730 You may distribute this code under the same terms as Perl itself.