| 1 | package Catalyst::ActionRole::RenderView; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.002'; | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Catalyst::ActionRole::RenderView::Utils::NoView; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | requires 'execute'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | around 'execute', sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | my ($orig, $self, $controller, $c, @args) = @_; | |||||||||||||||||||
| 12 | 1 | my @return = $self->$orig($controller, $c, @args); | |||||||||||||||||||
| 13 | 1 | $self->try_forwarding_to_view($c); | |||||||||||||||||||
| 14 | 1 | return @return; | |||||||||||||||||||
| 15 | 1 | }; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | ▶ | sub try_forwarding_to_view { | ||||||||||||||||||
|
Subroutine
try_forwarding_to_view: called CC=7 SCAR=21.8
Pod
undocumented
| |||||||||||||||||||||
| 18 | 1 | my ($self, $c) = @_; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | ▶ | return 0 if $c->req->method eq 'HEAD'; # Don't need a response for a HEAD request | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 1 | ▶ | return 0 if defined $c->response->body; # Don't need a response if we have one | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 1 | ▶ | return 0 if scalar @{ $c->error }; # Don't try to make a response if there's an unhandled error | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 1 | ▶ | return 0 if $c->response->status =~ /^(?:204|3\d\d)$/; # Don't response if its a redirect or 'No Content' response | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # This will either be the default_view (from config) or the 'current' view, as set by | ||||||||||||||||||||
| 26 | # stash flags 'current_view' or 'current_view_instance'. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 1 | ▶ | my $view = $c->view() || Catalyst::ActionRole::RenderView::Utils::NoView->throw; | ||||||||||||||||||
|
Condition33%1/3
$c->view || 'Catalyst::ActionRole::RenderView::Utils::NoView'->throw
MC/DC0%0/2
$c->view || 'Catalyst::ActionRole::RenderView::Utils::NoView'->throw
$c->view
'Catalyst::ActionRole::RenderView::Utils::NoView'->throw
Truth table33%1/3
A || B
A= $c->viewB= 'Catalyst::ActionRole::RenderView::Utils::NoView'->throw
| |||||||||||||||||||||
| 29 | 0 | ▶ | $c->log->debug("Forwarding to view: @{[ $view->catalyst_component_name ]}") if $c->debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 1 | $c->forward($view); | |||||||||||||||||||
| 31 | 1 | return 1; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | 1; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 NAME | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Catalyst::ActionRole::RenderView - Call the default view | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head1 SYNOPSIS | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | package Example::Controller::Root; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | use Moose; | ||||||||||||||||||||
| 45 | use MooseX::MethodAttributes; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | extends 'Catalyst::Controller'; | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | sub end : Action Does(RenderView) {} | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | This is basically L<Catalyst::Action::RenderView> done as an action role (basically a L<Moose> | ||||||||||||||||||||
| 54 | role) rather than as a base class. This is a bit more flexible if you plan to do fancy | ||||||||||||||||||||
| 55 | stuff with your end action. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Two things it doesn't do that the classic L<Catalyst::Action::RenderView> does is it doesn't | ||||||||||||||||||||
| 58 | set a default content type if none is found (old one just set C<text/html> which was probably | ||||||||||||||||||||
| 59 | ok back in the 'Aughts but not always true now) and we don't support the C<dump_info> when in | ||||||||||||||||||||
| 60 | debug mode since I really think something like that belongs in another part of the stack. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | I'm willing to be proven wrong, just send me your use cases and patches. | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head1 EXCEPTIONS | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | This class can throw the following exceptions which are compatible with L<CatalystX::Errors> | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =head2 No View found | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | If there's no view found when calling '$c->view()' we throw L<Catalyst::ActionRole::RenderView::Utils::NoView>. | ||||||||||||||||||||
| 71 | You can use L<CatalystX::Errors> to catch and handle that or roll your own error handling. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head1 AUTHOR | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | John Napiorkowski <jnapiork@cpan.org> | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 COPYRIGHT | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Copyright (c) 2023 the above named AUTHOR | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head1 LICENSE | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | You may distribute this code under the same terms as Perl itself. | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||