line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Builder; |
2
|
30
|
|
|
30
|
|
728144
|
use strict; |
|
30
|
|
|
|
|
145
|
|
|
30
|
|
|
|
|
831
|
|
3
|
30
|
|
|
30
|
|
4503
|
use parent qw( Exporter ); |
|
30
|
|
|
|
|
3362
|
|
|
30
|
|
|
|
|
144
|
|
4
|
|
|
|
|
|
|
our @EXPORT = qw( builder add enable enable_if mount ); |
5
|
|
|
|
|
|
|
|
6
|
30
|
|
|
30
|
|
1724
|
use Carp (); |
|
30
|
|
|
|
|
50
|
|
|
30
|
|
|
|
|
459
|
|
7
|
30
|
|
|
30
|
|
10336
|
use Plack::App::URLMap; |
|
30
|
|
|
|
|
64
|
|
|
30
|
|
|
|
|
736
|
|
8
|
30
|
|
|
30
|
|
11751
|
use Plack::Middleware::Conditional; # TODO delayed load? |
|
30
|
|
|
|
|
69
|
|
|
30
|
|
|
|
|
724
|
|
9
|
30
|
|
|
30
|
|
149
|
use Scalar::Util (); |
|
30
|
|
|
|
|
46
|
|
|
30
|
|
|
|
|
23268
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
60
|
|
|
60
|
0
|
5096
|
my $class = shift; |
13
|
60
|
|
|
|
|
214
|
bless { middlewares => [ ] }, $class; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub add_middleware { |
17
|
68
|
|
|
68
|
0
|
228
|
my($self, $mw, @args) = @_; |
18
|
|
|
|
|
|
|
|
19
|
68
|
100
|
|
|
|
201
|
if (ref $mw ne 'CODE') { |
20
|
67
|
|
|
|
|
193
|
my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware'); |
21
|
67
|
|
|
66
|
|
264
|
$mw = sub { $mw_class->wrap($_[0], @args) }; |
|
66
|
|
|
|
|
380
|
|
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
68
|
|
|
|
|
105
|
push @{$self->{middlewares}}, $mw; |
|
68
|
|
|
|
|
362
|
|
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub add_middleware_if { |
28
|
3
|
|
|
3
|
0
|
15
|
my($self, $cond, $mw, @args) = @_; |
29
|
|
|
|
|
|
|
|
30
|
3
|
100
|
|
|
|
11
|
if (ref $mw ne 'CODE') { |
31
|
2
|
|
|
|
|
7
|
my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware'); |
32
|
2
|
|
|
2
|
|
10
|
$mw = sub { $mw_class->wrap($_[0], @args) }; |
|
2
|
|
|
|
|
14
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
3
|
|
|
|
|
18
|
push @{$self->{middlewares}}, sub { |
36
|
3
|
|
|
3
|
|
20
|
Plack::Middleware::Conditional->wrap($_[0], condition => $cond, builder => $mw); |
37
|
3
|
|
|
|
|
6
|
}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# do you want remove_middleware() etc.? |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _mount { |
43
|
5
|
|
|
5
|
|
9
|
my ($self, $location, $app) = @_; |
44
|
|
|
|
|
|
|
|
45
|
5
|
50
|
|
|
|
11
|
if (!$self->{_urlmap}) { |
46
|
5
|
|
|
|
|
22
|
$self->{_urlmap} = Plack::App::URLMap->new; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
5
|
|
|
|
|
13
|
$self->{_urlmap}->map($location => $app); |
50
|
5
|
|
|
|
|
14
|
$self->{_urlmap}; # for backward compat. |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub to_app { |
54
|
59
|
|
|
59
|
0
|
143
|
my($self, $app) = @_; |
55
|
|
|
|
|
|
|
|
56
|
59
|
100
|
|
|
|
139
|
if ($app) { |
|
|
100
|
|
|
|
|
|
57
|
55
|
|
|
|
|
151
|
$self->wrap($app); |
58
|
|
|
|
|
|
|
} elsif ($self->{_urlmap}) { |
59
|
|
|
|
|
|
|
$self->{_urlmap} = $self->{_urlmap}->to_app |
60
|
3
|
50
|
|
|
|
12
|
if Scalar::Util::blessed($self->{_urlmap}); |
61
|
3
|
|
|
|
|
7
|
$self->wrap($self->{_urlmap}); |
62
|
|
|
|
|
|
|
} else { |
63
|
1
|
|
|
|
|
165
|
Carp::croak("to_app() is called without mount(). No application to build."); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub wrap { |
68
|
59
|
|
|
59
|
0
|
113
|
my($self, $app) = @_; |
69
|
|
|
|
|
|
|
|
70
|
59
|
100
|
100
|
|
|
183
|
if ($self->{_urlmap} && $app ne $self->{_urlmap}) { |
71
|
1
|
|
|
|
|
73
|
Carp::carp("WARNING: wrap() and mount() can't be used altogether in Plack::Builder.\n" . |
72
|
|
|
|
|
|
|
"WARNING: This causes all previous mount() mappings to be ignored."); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
59
|
|
|
|
|
127
|
for my $mw (reverse @{$self->{middlewares}}) { |
|
59
|
|
|
|
|
138
|
|
76
|
70
|
|
|
|
|
131
|
$app = $mw->($app); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
59
|
|
|
|
|
700
|
$app; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# DSL goes here |
83
|
|
|
|
|
|
|
our $_add = our $_add_if = our $_mount = sub { |
84
|
|
|
|
|
|
|
Carp::croak("enable/mount should be called inside builder {} block"); |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
|
87
|
60
|
|
|
60
|
0
|
383
|
sub enable { $_add->(@_) } |
88
|
2
|
|
|
2
|
0
|
15
|
sub enable_if(&$@) { $_add_if->(@_) } |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub mount { |
91
|
13
|
|
|
13
|
0
|
55
|
my $self = shift; |
92
|
13
|
100
|
|
|
|
47
|
if (Scalar::Util::blessed($self)) { |
93
|
5
|
|
|
|
|
11
|
$self->_mount(@_); |
94
|
|
|
|
|
|
|
}else{ |
95
|
8
|
|
|
|
|
17
|
$_mount->($self, @_); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub builder(&) { |
100
|
54
|
|
|
54
|
0
|
38032
|
my $block = shift; |
101
|
|
|
|
|
|
|
|
102
|
54
|
|
|
|
|
278
|
my $self = __PACKAGE__->new; |
103
|
|
|
|
|
|
|
|
104
|
54
|
|
|
|
|
98
|
my $mount_is_called; |
105
|
54
|
|
|
|
|
397
|
my $urlmap = Plack::App::URLMap->new; |
106
|
|
|
|
|
|
|
local $_mount = sub { |
107
|
8
|
|
|
8
|
|
12
|
$mount_is_called++; |
108
|
8
|
|
|
|
|
23
|
$urlmap->map(@_); |
109
|
8
|
|
|
|
|
21
|
$urlmap; |
110
|
54
|
|
|
|
|
216
|
}; |
111
|
|
|
|
|
|
|
local $_add = sub { |
112
|
60
|
|
|
60
|
|
156
|
$self->add_middleware(@_); |
113
|
54
|
|
|
|
|
181
|
}; |
114
|
|
|
|
|
|
|
local $_add_if = sub { |
115
|
2
|
|
|
2
|
|
6
|
$self->add_middleware_if(@_); |
116
|
54
|
|
|
|
|
460
|
}; |
117
|
|
|
|
|
|
|
|
118
|
54
|
|
|
|
|
136
|
my $app = $block->(); |
119
|
|
|
|
|
|
|
|
120
|
54
|
100
|
|
|
|
271
|
if ($mount_is_called) { |
121
|
4
|
100
|
|
|
|
17
|
if ($app ne $urlmap) { |
122
|
1
|
|
|
|
|
189
|
Carp::carp("WARNING: You used mount() in a builder block, but the last line (app) isn't using mount().\n" . |
123
|
|
|
|
|
|
|
"WARNING: This causes all mount() mappings to be ignored.\n"); |
124
|
|
|
|
|
|
|
} else { |
125
|
3
|
|
|
|
|
21
|
$app = $app->to_app; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
54
|
50
|
33
|
|
|
389
|
$app = $app->to_app if $app and Scalar::Util::blessed($app) and $app->can('to_app'); |
|
|
|
33
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
54
|
|
|
|
|
157
|
$self->to_app($app); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |