blib/lib/Mojolicious/Plugin/Alias.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 83.3% 15 / 18 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 9 / 9 Podpod 25.0% 1 / 4 totaltotal 82.4% 80 / 97 CC 14 SCAR 16.0
CC
14
Cov
88%
alias
9.9
register
3.0
match
3.0
CRAP 5.0
↑ Summary
1 package Mojolicious::Plugin::Alias;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 1 our $VERSION = '0.0.4';
10  
11 1 our $aliases = {};
12 1 our $saved_static_dispatcher;
13  
14 78 ▶sub aliases {
Subroutine aliases: called CC=1 SCAR=0.0
Pod undocumented
15 78 my ($self) = @_;
16 78 my %by_lengths = map { $_ => length $_ } keys %$aliases;
17 78 my @aliases = sort { $by_lengths{$b} <=> $by_lengths{$a} } keys %$aliases;
18 78 return @aliases;
19 }
20  
21 17 ▶sub alias {
Subroutine alias: called CC=8 SCAR=22.9
Pod undocumented
22 17 my $self = shift;
23 17 my $alias = shift;
24 17 ▶ return unless ( $alias && $alias =~ m{^/.*} );
Branch: 1 branch
BranchTrueFalseTotal
unless $alias and $alias =~ m[^/.*]u 0 17 17
Condition33%1/3
$alias and $alias =~ m[^/.*]u
!ll&&!rl&&r
0017
MC/DC0%0/2
$alias and $alias =~ m[^/.*]u
$alias $alias =~ m[^/.*]u
Truth table33%1/3
A and B
A= $aliasB= $alias =~ m[^/.*]u
ABresult
0X0
100
111
25 17 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 7 10 17
26 7 ▶ my @args = (@_ == 1 && ! ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && !ref($_[0]) ? : 3 4 7
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
043
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
27 ? (paths => [ @_ ])
28 : @_ ;
29 7 $aliases->{$alias} = Mojolicious::Static->new(@args);
30 }
31 17 ▶ if ($alias && exists $aliases->{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias and exists $Mojolicious::Plugin::Alias::aliases->{$alias}) 17 0 17
Condition33%1/3
$alias and exists $Mojolicious::Plugin::Alias::aliases->{$alias}
!ll&&!rl&&r
0017
MC/DC0%0/2
$alias and exists $Mojolicious::Plugin::Alias::aliases->{$alias}
$alias exists $Mojolicious::Plugin::Alias::aliases->{$alias}
Truth table33%1/3
A and B
A= $aliasB= exists $Mojolicious::Plugin::Alias::aliases->{$alias}
ABresult
0X0
100
111
32 17 return $aliases->{$alias};
33 }
34 0 return;
35 }
36  
37 78 ▶sub match {
Subroutine match: called CC=3 SCAR=11.0
Pod undocumented
38 78 my ($self, $req_path) = @_;
39 78 foreach my $alias ($self->aliases) {
40 508 ▶ if ($req_path =~ /^$alias.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_path =~ /^$alias.*/u) 10 498 508
41 # print STDERR "$req_path matches $alias";
42 10 return $alias;
43 }
44 }
45 68 return;
46 }
47  
48  
49 6 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
50 6 my ($self, $app, $conf) = @_;
51  
52 6 ▶ if ($conf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf) 6 0 6
53 6 ▶ while(my ($alias, $def) = each %$conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$conf) 7 6 13
54 7 $self->alias($alias, $def);
55 }
56 };
57  
58 $app->hook(
59 78 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
60 78 my ($c) = @_;
61 78 my $req_path = $c->req->url->path;
62 78 ▶ return unless (my $alias = $self->match($req_path));
Branch: 1 branch
BranchTrueFalseTotal
unless my $alias = $self->match($req_path) 68 10 78
63 # rewrite req_path
64 10 $req_path =~ s/^$alias//;
65 10 $c->req->url->path($req_path);
66 # change static
67 10 my $dispatcher = $self->alias($alias);
68 10 $saved_static_dispatcher = $c->app->static;
69 10 $c->app->static($dispatcher);
70 # Pushy?
71 6 } );
72 $app->hook(
73 66 ▶ after_static => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 66 my ($c) = @_;
75 66 ▶ if ($saved_static_dispatcher) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mojolicious::Plugin::Alias::saved_static_dispatcher) 9 57 66
76 9 $c->app->static($saved_static_dispatcher);
77 9 $saved_static_dispatcher = undef;
78 }
79 6 } );
80 }
81  
82  
83 1 1;
84 __END__
↑ Summary