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 = '0.400001';
4 136     136   64669 use Moo;
  136         338  
  136         1028  
5 136     136   54059 use Carp 'croak';
  136         380  
  136         8296  
6 136     136   983 use Dancer2::Core::Types;
  136         336  
  136         1509  
7              
8             with qw<
9             Dancer2::Core::Role::Handler
10             Dancer2::Core::Role::StandardResponses
11             >;
12              
13             sub register {
14 202     202 1 650 my ( $self, $app ) = @_;
15              
16 202 100       3323 return unless $app->config->{auto_page};
17              
18             $app->add_route(
19             method => $_,
20             regexp => $self->regexp,
21             code => $self->code,
22 2         22 ) for $self->methods;
23             }
24              
25             sub code {
26             sub {
27 10     10   16 my $app = shift;
28 10         16 my $prefix = shift;
29              
30 10         128 my $template = $app->template_engine;
31 10 50       63 if ( !defined $template ) {
32 0         0 $app->response->has_passed(1);
33 0         0 return;
34             }
35              
36 10         32 my $page = $app->request->path;
37 10         181 my $layout_dir = $template->layout_dir;
38 10 100       86 if ( $page =~ m{^/\Q$layout_dir\E/} ) {
39 3         41 $app->response->has_passed(1);
40 3         108 return;
41             }
42              
43             # remove leading '/', ensuring paths relative to the view
44 7         24 $page =~ s{^/}{};
45 7         27 my $view_path = $template->view_pathname($page);
46              
47 7 100       22 if ( ! $template->pathname_exists( $view_path ) ) {
48 3         49 $app->response->has_passed(1);
49 3         129 return;
50             }
51              
52 4         23 my $ct = $template->process( $page );
53 4 50       23 return ( $app->request->method eq 'GET' ) ? $ct : '';
54 4     4 1 27 };
55             }
56              
57 4     4 1 12 sub regexp {'/**'}
58              
59 2     2 1 13 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 0.400001
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