line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Alias; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2193
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
21
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.0.4'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $aliases = {}; |
12
|
|
|
|
|
|
|
our $saved_static_dispatcher; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub aliases { |
15
|
78
|
|
|
78
|
0
|
189
|
my ($self) = @_; |
16
|
78
|
|
|
|
|
354
|
my %by_lengths = map { $_ => length $_ } keys %$aliases; |
|
546
|
|
|
|
|
1229
|
|
17
|
78
|
|
|
|
|
456
|
my @aliases = sort { $by_lengths{$b} <=> $by_lengths{$a} } keys %$aliases; |
|
936
|
|
|
|
|
1792
|
|
18
|
78
|
|
|
|
|
376
|
return @aliases; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub alias { |
22
|
17
|
|
|
17
|
0
|
26
|
my $self = shift; |
23
|
17
|
|
|
|
|
30
|
my $alias = shift; |
24
|
17
|
50
|
33
|
|
|
312
|
return unless ( $alias && $alias =~ m{^/.*} ); |
25
|
17
|
100
|
|
|
|
54
|
if (@_ > 0) { |
26
|
7
|
100
|
66
|
|
|
42
|
my @args = (@_ == 1 && ! ref $_[0]) |
27
|
|
|
|
|
|
|
? (paths => [ @_ ]) |
28
|
|
|
|
|
|
|
: @_ ; |
29
|
7
|
|
|
|
|
28
|
$aliases->{$alias} = Mojolicious::Static->new(@args); |
30
|
|
|
|
|
|
|
} |
31
|
17
|
50
|
33
|
|
|
170
|
if ($alias && exists $aliases->{$alias}) { |
32
|
17
|
|
|
|
|
200
|
return $aliases->{$alias}; |
33
|
|
|
|
|
|
|
} |
34
|
0
|
|
|
|
|
0
|
return; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub match { |
38
|
78
|
|
|
78
|
0
|
248
|
my ($self, $req_path) = @_; |
39
|
78
|
|
|
|
|
163
|
foreach my $alias ($self->aliases) { |
40
|
508
|
100
|
|
|
|
34911
|
if ($req_path =~ /^$alias.*/) { |
41
|
|
|
|
|
|
|
# print STDERR "$req_path matches $alias"; |
42
|
10
|
|
|
|
|
640
|
return $alias; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
68
|
|
|
|
|
4373
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub register { |
50
|
6
|
|
|
6
|
1
|
842
|
my ($self, $app, $conf) = @_; |
51
|
|
|
|
|
|
|
|
52
|
6
|
50
|
|
|
|
16
|
if ($conf) { |
53
|
6
|
|
|
|
|
22
|
while(my ($alias, $def) = each %$conf) { |
54
|
7
|
|
|
|
|
15
|
$self->alias($alias, $def); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$app->hook( |
59
|
|
|
|
|
|
|
before_dispatch => sub { |
60
|
78
|
|
|
78
|
|
891059
|
my ($c) = @_; |
61
|
78
|
|
|
|
|
709
|
my $req_path = $c->req->url->path; |
62
|
78
|
100
|
|
|
|
8234
|
return unless (my $alias = $self->match($req_path)); |
63
|
|
|
|
|
|
|
# rewrite req_path |
64
|
10
|
|
|
|
|
111
|
$req_path =~ s/^$alias//; |
65
|
10
|
|
|
|
|
5146
|
$c->req->url->path($req_path); |
66
|
|
|
|
|
|
|
# change static |
67
|
10
|
|
|
|
|
2384
|
my $dispatcher = $self->alias($alias); |
68
|
10
|
|
|
|
|
322
|
$saved_static_dispatcher = $c->app->static; |
69
|
10
|
|
|
|
|
656
|
$c->app->static($dispatcher); |
70
|
|
|
|
|
|
|
# Pushy? |
71
|
6
|
|
|
|
|
43
|
} ); |
72
|
|
|
|
|
|
|
$app->hook( |
73
|
|
|
|
|
|
|
after_static => sub { |
74
|
66
|
|
|
66
|
|
37683
|
my ($c) = @_; |
75
|
66
|
100
|
|
|
|
202
|
if ($saved_static_dispatcher) { |
76
|
9
|
|
|
|
|
305
|
$c->app->static($saved_static_dispatcher); |
77
|
9
|
|
|
|
|
383
|
$saved_static_dispatcher = undef; |
78
|
|
|
|
|
|
|
} |
79
|
6
|
|
|
|
|
254
|
} ); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
__END__ |