Plugin/VersionedURI.pm

filter: Statementstmt 96.5% 56 / 58 Branchbran 75.0% 12 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 83.1% 84 / 101 CC 16 SCAR 17.0
CC
16
Cov
88%
initialize_uri_regex
9.9
uri_version
6.0
__ANON__
5.0
CRAP 5.5
↑ Summary Controller/VersionedURI.pm »
1 package Catalyst::Plugin::VersionedURI;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: add version component to uris
4 5 $Catalyst::Plugin::VersionedURI::VERSION = '1.2.0';
5  
6 5 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our @uris;
16  
17 4 ▶sub initialize_uri_regex {
Subroutine initialize_uri_regex: called CC=7 SCAR=22.9
Pod undocumented
18 4 my $self = shift;
19  
20 4 ▶ if ( not exists $self->config->{'Plugin::VersionedURI'}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->config->{"Plugin::VersionedURI"} and exists $self->config->{"VersionedURI"}) 0 4 4
Condition33%1/3
not exists $self->config->{"Plugin::VersionedURI"} and exists $self->config->{"VersionedURI"}
!ll&&!rl&&r
400
MC/DC0%0/2
not exists $self->config->{"Plugin::VersionedURI"} and exists $self->config->{"VersionedURI"}
not exists $self->config->{"Plugin::VersionedURI"} exists $self->config->{"VersionedURI"}
Truth table33%1/3
not(A) and B
A= not exists $self->config->{"Plugin::VersionedURI"}B= exists $self->config->{"VersionedURI"}
ABresult
0X0
100
111
21 and exists $self->config->{'VersionedURI'} ) {
22 0 warn <<'END_DEPRECATION';
23 Catalyst::Plugin::VersionedURI configuration set under 'VersionedURI' is deprecated
24 Please move your configuration to 'Plugin::VersionedURI'
25 END_DEPRECATION
26  
27 $self->config->{'Plugin::VersionedURI'}
28 0 = $self->config->{'VersionedURI'};
29 }
30  
31  
32 my $conf = $self->config->{'Plugin::VersionedURI'}{uri}
33 4 ▶ || '/static';
Condition50%1/2
$self->config->{'Plugin::VersionedURI'}{'uri'} || '/static'
l!l
40
MC/DC0%0/1
$self->config->{'Plugin::VersionedURI'}{'uri'} || '/static'
$self->config->{'Plugin::VersionedURI'}{'uri'}
Truth table50%1/2
A
A= $self->config->{'Plugin::VersionedURI'}{'uri'}
Aresult
11
00
34  
35 4 ▶ @uris = ref($conf) ? @$conf : ( $conf );
Branch: 1 branch
BranchTrueFalseTotal
ref $conf ? : 4 0 4
36 4 s#^/## for @uris;
37 4 s#(?<!/)$#/# for @uris;
38  
39 4 return join '|', @uris;
40 }
41  
42 13 ▶sub versioned_uri_regex {
Subroutine versioned_uri_regex: called CC=1 SCAR=0.0
Pod undocumented
43 13 my $self = shift;
44 13 state $uris_re = $self->initialize_uri_regex;
45 13 return $uris_re;
46 }
47  
48 8 ▶sub uri_version {
Subroutine uri_version: called CC=6 SCAR=18.0
Pod undocumented
49 8 my ( $self, $uri ) = @_;
50  
51 8 state $app_version = $self->VERSION;
52  
53 return $app_version
54 8 ▶ unless state $mtime = $self->config->{'Plugin::VersionedURI'}{mtime};
Branch: 1 branch
BranchTrueFalseTotal
unless state $mtime = $self->config->{"Plugin::VersionedURI"}{"mtime"} 6 2 8
55
56 2 state %cache; # Would be nice to make this shared across processes
57  
58 # Return the cached value if there is one
59 2 ▶ return $cache{$uri} if defined $cache{$uri};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache{$uri} 0 2 2
60  
61 # Strip off the request base, so we can find the file referenced
62 2 ( my $file = $uri ) =~ s/^\Q@{[ $self->req->base ]}\E//;
63  
64 # Search the include_path(s) provided in config or the
65 # project root if no include_path was specified
66 state $include_paths =
67 $self->config->{'Plugin::VersionedURI'}{include_path} //
68 2 ▶ [ $self->config->{root} ];
Condition50%1/2
$self->config->{"Plugin::VersionedURI"}{"include_path"} // [$self->config->{"root"}]
l!l
01
MC/DC0%0/1
$self->config->{"Plugin::VersionedURI"}{"include_path"} // [$self->config->{"root"}]
$self->config->{"Plugin::VersionedURI"}{"include_path"}
Truth table50%1/2
A
A= $self->config->{"Plugin::VersionedURI"}{"include_path"}
Aresult
11
00
69  
70 # Return/cache the file's mtime
71 2 for my $path ( map { path( $_, $file ) } @$include_paths ) {
72 2 ▶ return $cache{$uri} = $path->stat->mtime if -f $path;
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 1 1 2
73 }
74  
75 # No file was found. Store and return the application's version as
76 # a fallback.
77 1 return $cache{$uri} = $app_version;
78 }
79  
80 12 ▶around uri_for => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
81 12 my ( $code, $self, @args ) = @_;
82  
83 12 my $uri = $self->$code(@args);
84  
85 12 ▶ my $uris_re = $self->versioned_uri_regex
Branch: 1 branch
BranchTrueFalseTotal
my $uris_re = $self->versioned_uri_regex or return $uri 12 0 12
86 or return $uri;
87  
88 12 ▶ return $uri unless $uri->path =~ m#^/($uris_re)#;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->path =~ m[^/($uris_re)] 4 8 12
89  
90 8 my $version = $self->uri_version( $uri, @args );
91  
92 8 ▶ if ( state $in_path = $self->config->{'Plugin::VersionedURI'}{in_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (state $in_path = $self->config->{'Plugin::VersionedURI'}{'in_path'}) { } 2 6 8
93 2 my $path = $uri->path;
94 2 $path =~ s#^/($uris_re)#${1}v$version/#;
95 2 $uri->path( $path );
96 }
97 else {
98 6 ▶ state $version_name = $self->config->{'Plugin::VersionedURI'}{param} || 'v';
Condition100%2/2
$self->config->{'Plugin::VersionedURI'}{'param'} || 'v'
l!l
12
MC/DC100%1/1
$self->config->{'Plugin::VersionedURI'}{'param'} || 'v'
$self->config->{'Plugin::VersionedURI'}{'param'}
Truth table100%2/2
A
A= $self->config->{'Plugin::VersionedURI'}{'param'}
Aresult
11
00
99 6 $uri->query_param( $version_name => $version );
100 }
101  
102 8 return $uri;
103 5 };
104  
105 5 1;
106  
107 __END__
↑ Summary Controller/VersionedURI.pm »