File Coverage

blib/lib/Dancer2/Handler/AutoPage.pm
Criterion Covered Total %
statement 31 33 93.9
branch 8 10 80.0
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             package Dancer2::Handler::AutoPage;
2             # ABSTRACT: Class for handling the AutoPage feature
3             $Dancer2::Handler::AutoPage::VERSION = '1.0.0';
4 137     137   70940 use Moo;
  137         382  
  137         1100  
5 137     137   56313 use Carp 'croak';
  137         457  
  137         8816  
6 137     137   1129 use Dancer2::Core::Types;
  137         454  
  137         1655  
7              
8             with qw<
9             Dancer2::Core::Role::Handler
10             Dancer2::Core::Role::StandardResponses
11             >;
12              
13             sub register {
14 203     203 1 668 my ( $self, $app ) = @_;
15              
16 203 100       3447 return unless $app->config->{auto_page};
17              
18             $app->add_route(
19             method => $_,
20             regexp => $self->regexp,
21             code => $self->code,
22 2         26 ) for $self->methods;
23             }
24              
25             sub code {
26             sub {
27 10     10   19 my $app = shift;
28 10         13 my $prefix = shift;
29              
30 10         189 my $template = $app->template_engine;
31 10 50       90 if ( !defined $template ) {
32 0         0 $app->response->has_passed(1);
33 0         0 return;
34             }
35              
36 10         40 my $page = $app->request->path;
37 10         255 my $layout_dir = $template->layout_dir;
38 10 100       114 if ( $page =~ m{^/\Q$layout_dir\E/} ) {
39 3         49 $app->response->has_passed(1);
40 3         136 return;
41             }
42              
43             # remove leading '/', ensuring paths relative to the view
44 7         27 $page =~ s{^/}{};
45 7         29 my $view_path = $template->view_pathname($page);
46              
47 7 100       29 if ( ! $template->pathname_exists( $view_path ) ) {
48 3         60 $app->response->has_passed(1);
49 3         174 return;
50             }
51              
52 4         24 my $ct = $template->process( $page );
53 4 50       23 return ( $app->request->method eq 'GET' ) ? $ct : '';
54 4     4 1 30 };
55             }
56              
57 4     4 1 18 sub regexp {'/**'}
58              
59 2     2 1 55 sub methods {qw(head get)}
60              
61             1;
62              
63             __END__
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Dancer2::Handler::AutoPage - Class for handling the AutoPage feature
72              
73             =head1 VERSION
74              
75             version 1.0.0
76              
77             =head1 DESCRIPTION
78              
79             The AutoPage feature is a Handler (turned off by default) that is
80             responsible for serving pages that match an existing template. If a
81             view exists with a name that matches the requested path, Dancer2
82             processes the request using the Autopage handler.
83              
84             To turn it add to your config file:
85              
86             auto_page: 1
87              
88             This allows you to easily serve simple pages without having to write a
89             route definition for them.
90              
91             If there's no view with the name request, the route passes, allowing
92             other matching routes to be dispatched.
93              
94             =head1 METHODS
95              
96             =head2 register
97              
98             Creates the routes.
99              
100             =head2 code
101              
102             A code reference that processes the route request.
103              
104             =head2 methods
105              
106             The methods that should be served for autopages.
107              
108             Default: B<head>, B<get>.
109              
110             =head2 regexp
111              
112             The regexp (path) we want to match.
113              
114             Default: B</:page>.
115              
116             =head1 AUTHOR
117              
118             Dancer Core Developers
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2023 by Alexis Sukrieh.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut