| 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
Condition33%1/3
$alias and $alias =~ m[^/.*]u
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
| |||||||||||||||||||||||||||||
| 25 | 17 | ▶ | if (@_ > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 7 | ▶ | my @args = (@_ == 1 && ! ref $_[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and not ref $_[0]
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]
| |||||||||||||||||||||||||||||
| 27 | ? (paths => [ @_ ]) | ||||||||||||||||||||||||||||
| 28 | : @_ ; | ||||||||||||||||||||||||||||
| 29 | 7 | $aliases->{$alias} = Mojolicious::Static->new(@args); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 17 | ▶ | if ($alias && exists $aliases->{$alias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$alias and exists $Mojolicious::Plugin::Alias::aliases->{$alias}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | 6 | ▶ | while(my ($alias, $def) = each %$conf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||