Cache/Page.pm

filter: Statementstmt 42.8% 21 / 49 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 35.0% 28 / 80 CC 12 SCAR 38.9
CC
12
Cov
30%
__ANON__
90.0
register
17.4
BEGIN
1.0
CRAP 49.1
↑ Summary Cache/Action.pm »
1 package Mojolicious::Plugin::Cache::Page;
2 {
3 1 $Mojolicious::Plugin::Cache::Page::VERSION = '0.0017'; # TRIAL
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Path qw/make_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base qw/Mojolicious::Plugin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Module implementation
13 #
14  
15 1 ▶sub register {
Subroutine register: called CC=4 SCAR=28.6
Pod documented
16 1 my ( $self, $app, $conf ) = @_;
17  
18 1 my $home = $app->home;
19 1 ▶ my $cache_dir
Branch: 1 branch
BranchTrueFalseTotal
-e $app->home->rel_dir('public') ? : 0 0 0
20 = -e $app->home->rel_dir('public')
21 ? $app->home->rel_dir('public')
22 : $home->to_string;
23  
24 #if given as an option
25 0 ▶ if ( defined $conf->{cache_directory} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"cache_directory"}) 0 0 0
26 0 $cache_dir = $conf->{cache_directory};
27 }
28  
29 0 my $actions;
30 0 ▶ if ( defined $conf->{actions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"actions"}) 0 0 0
31 0 $actions = { map { $_ => 1 } @{ $conf->{actions} } };
32 }
33  
34 $app->hook(
35 0 ▶ 'after_dispatch' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
36 0 my ($c) = @_;
37  
38 ## - has to be GET request
39 0 ▶ return if $c->req->method ne 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method ne "GET" 0 0 0
40  
41 ## - only successful response
42 0 ▶ return if $c->res->code != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->code != 200 0 0 0
43  
44 ## - only html response
45 0 ▶ return if $c->res->headers->content_type !~ /^text\/html/;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->res->headers->content_type =~ m[^text/html] 0 0 0
46  
47 ## - have to match the action
48 0 my $name = $c->stash('action');
49 return
50 if defined $conf->{actions}
51 0 ▶ and not exists $actions->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"actions"} and not exists $actions->{$name} 0 0 0
Condition0%0/3
defined $conf->{"actions"} and not exists $actions->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $conf->{"actions"} and not exists $actions->{$name}
defined $conf->{"actions"} not exists $actions->{$name}
Truth table0%0/3
A and not(B)
A= defined $conf->{"actions"}B= not exists $actions->{$name}
ABresult
0X0
100
111
52  
53 0 my $parts = $c->req->url->path->parts;
54 0 my $file_name;
55 0 ▶ if ( @$parts == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$parts == 1) { } 0 0 0
56 0 $file_name = catfile( $cache_dir, $parts->[0] . '.html' );
57 }
58 else {
59 0 my $end = pop @$parts;
60 0 my $folder = $cache_dir;
61 0 $folder = catdir( $folder, $_ ) for @$parts;
62 0 make_path($folder);
63 0 $file_name = catfile( $folder, $end . '.html' );
64 }
65  
66 0 $app->log->debug(
67 "storing in cache for action **$name** in file $file_name");
68  
69 0 ▶ my $handler = IO::File->new( $file_name, 'w' )
Branch: 1 branch
BranchTrueFalseTotal
my $handler = "IO::File"->new($file_name, "w") or croak("cannot create file:$!") 0 0 0
70 or croak "cannot create file:$!";
71 0 $handler->print( $c->res->body );
72 0 $handler->close;
73 }
74 0 );
75 0 return;
76 }
77  
78 1 1;
79  
80 # ABSTRACT: Page caching plugin
81  
82 __END__
↑ Summary Cache/Action.pm »