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