| 1 | package MasonX::Maypole::View; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Maypole::Constants; | ||
| 6 | |||
| 7 | use Symbol 'qualify_to_ref'; | ||
| 8 | |||
| 9 | use base 'Maypole::View::Base'; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | MasonX::Maypole::View - Mason view subclass for MasonX::Maypole + Maypole 2 | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | See L<MasonX::Maypole|MasonX::Maypole>. | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =over | ||
| 22 | |||
| 23 | =item template | ||
| 24 | |||
| 25 | Loads the Maypole template vars into Mason components' namespace. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | sub template { | ||
| 30 | my ( $self, $maypole ) = @_; | ||
| 31 | |||
| 32 | eval { | ||
| 33 | my $pkg = $maypole->config->masonx->{in_package}; | ||
| 34 | |||
| 35 | my %vars = $self->vars( $maypole ); | ||
| 36 | |||
| 37 | warn "got template vars: " . YAML::Dump( \%vars ) if $maypole->debug > 2; | ||
| 38 | |||
| 39 | warn "BUG IN " . __PACKAGE__ . " - template vars not getting cleaned up" | ||
| 40 | if $maypole->debug > 1; | ||
| 41 | |||
| 42 | foreach my $varname ( keys %vars ) | ||
| 43 | { | ||
| 44 | my $export = qualify_to_ref( $varname, $pkg ); | ||
| 45 | *$export = \$vars{ $varname }; | ||
| 46 | |||
| 47 | # this does _not_ seem to be cleaning up | ||
| 48 | $maypole->ar->register_cleanup( sub { undef *$export; 1 } ); | ||
| 49 | |||
| 50 | # no strict 'refs'; | ||
| 51 | # *{"$pkg\::$varname"} = \$vars{ $varname }; | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | |||
| 55 | if ( my $error = $@ ) | ||
| 56 | { | ||
| 57 | $maypole->error( 'Error populating template vars: ' . $error ); | ||
| 58 | return ERROR; | ||
| 59 | } | ||
| 60 | |||
| 61 | return OK; | ||
| 62 | } | ||
| 63 | |||
| 64 | =item error | ||
| 65 | |||
| 66 | Handles errors by sending a plain text error report (i.e. not through any | ||
| 67 | templates) to the browser. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub error { | ||
| 72 | my ( $self, $maypole, $error ) = @_; | ||
| 73 | |||
| 74 | # Some parts of Maypole will store the error in the Maypole request 'error' slot | ||
| 75 | # (e.g. see above). Others pass the error as an argument (e.g. Maypole::handler_guts) | ||
| 76 | my @errors = $error, $maypole->error; | ||
| 77 | |||
| 78 | unshift @errors, 'Maypole error caught by ' . __PACKAGE__ . ':', | ||
| 79 | Devel::StackTrace->new->as_string | ||
| 80 | if $maypole->debug; | ||
| 81 | |||
| 82 | my $errors = join "\n", @errors; | ||
| 83 | |||
| 84 | #print STDERR $errors if $maypole->debug; | ||
| 85 | |||
| 86 | $maypole->content_type( 'text/plain' ); | ||
| 87 | |||
| 88 | $maypole->output( $errors ); | ||
| 89 | |||
| 90 | $maypole->send_output; | ||
| 91 | |||
| 92 | return ERROR; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 |