lib/Rapi/Blog/Template/Dispatcher/ScaffoldFile.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 123 CC 3 SCAR 12.0
CC
3
Cov
0%
_resolve_file
6.0
resolved
6.0
rank
2.0
CRAP 3.3
1 package Rapi::Blog::Template::Dispatcher::ScaffoldFile;
2 use strict;
3 use warnings;
4
5 use RapidApp::Util qw(:all);
6 use Rapi::Blog::Util;
7 use List::Util;
8
9 use Moo;
10 extends 'Rapi::Blog::Template::Dispatcher';
11
12 use Types::Standard ':all';
13
14
15 has '_resolve_file_cache', is => 'ro', default => sub {{}};
16 sub _resolve_file {
17 my ($self,$path) = @_;
18 my $h = $self->_resolve_file_cache;
19 $h->{$path} = $self->Scaffold->resolve_file($path) unless (exists $h->{$path});
20 $h->{$path}
21 }
22
23 has 'effective_path', is => 'ro', lazy => 1, default => sub {
24 my $self = shift;
25 return $self->path if (
26 $self->is_static
27 || ! $self->Scaffold->default_ext
28 || $self->_resolve_file($self->path)
29 );
30
31 my $epath = join('.',$self->path,$self->Scaffold->default_ext);
32 $self->_resolve_file($epath) ? $epath : $self->path
33 }, isa => Str;
34
35
36 has 'File', is => 'ro', lazy => 1, default => sub {
37 my $self = shift;
38 $self->_resolve_file( $self->effective_path )
39 }, isa => Maybe[InstanceOf['Path::Class::File']];
40
41
42 has '+exists', default => sub {
43 my $self = shift;
44 $self->File ? 1 : 0
45 };
46
47 has '+mtime', default => sub {
48 my $self = shift;
49 my $File = $self->File or return undef;
50 my $Stat = $File->stat or return undef;
51 $Stat->mtime
52 };
53
54 has '+content', default => sub {
55 my $self = shift;
56 my $File = $self->File or return undef;
57 scalar $File->slurp
58 };
59
60
61 has 'renders_static', is => 'ro', lazy => 1, default => sub {
62 my $self = shift;
63 return 0 unless $self->File;
64 return 1 if $self->is_static;
65
66 my ($path_fn) = reverse split(/\//,$self->effective_path);
67
68 $path_fn eq $self->File->basename && ! $self->Scaffold->_is_valid_template_name($self->effective_path)
69
70 }, isa => Bool;
71
72
73 sub resolved {
74 my $self = shift;
75 $self->exists ? $self : $self->_factory_for('NotFound')
76 }
77
78
79 sub rank { 100 }
80
81
82
83
84 has '+maybe_psgi_response', default => sub {
85 my $self = shift;
86
87 # Deny *outside* requests to private paths
88 $self->is_private and return $self->_factory_for('NotFound')->maybe_psgi_response;
89
90 $self->renders_static or return undef;
91
92 my $tpl = $self->path;
93
94 $self->Scaffold->static_path_app->({
95 %{ $self->ctx->req->env },
96 PATH_INFO => "/$tpl",
97 SCRIPT_NAME => ''
98 })
99 };
100
101
102
103 1;