blib/lib/Plack/Middleware/Debug.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 71 | 75 | 94.6 |
branch | 10 | 16 | 62.5 |
condition | 3 | 7 | 42.8 |
subroutine | 19 | 19 | 100.0 |
pod | 2 | 4 | 50.0 |
total | 105 | 121 | 86.7 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Plack::Middleware::Debug; | ||||||
2 | 2 | 2 | 12641 | use 5.008_001; | |||
2 | 14 | ||||||
3 | 2 | 2 | 11 | use strict; | |||
2 | 4 | ||||||
2 | 36 | ||||||
4 | 2 | 2 | 9 | use warnings; | |||
2 | 2 | ||||||
2 | 73 | ||||||
5 | 2 | 2 | 12 | use parent qw(Plack::Middleware); | |||
2 | 4 | ||||||
2 | 9 | ||||||
6 | our $VERSION = '0.17'; | ||||||
7 | |||||||
8 | 2 | 2 | 14539 | use Encode; | |||
2 | 11216 | ||||||
2 | 163 | ||||||
9 | 2 | 2 | 962 | use File::ShareDir; | |||
2 | 12717 | ||||||
2 | 89 | ||||||
10 | 2 | 2 | 878 | use Plack::App::File; | |||
2 | 13143 | ||||||
2 | 72 | ||||||
11 | 2 | 2 | 387 | use Plack::Builder; | |||
2 | 2760 | ||||||
2 | 133 | ||||||
12 | 2 | 2 | 12 | use Plack::Util::Accessor qw(panels renderer files); | |||
2 | 5 | ||||||
2 | 6 | ||||||
13 | 2 | 2 | 114 | use Plack::Util; | |||
2 | 4 | ||||||
2 | 37 | ||||||
14 | 2 | 2 | 947 | use Plack::Middleware::Debug::Panel; | |||
2 | 6 | ||||||
2 | 57 | ||||||
15 | 2 | 2 | 1037 | use Text::MicroTemplate; | |||
2 | 6127 | ||||||
2 | 100 | ||||||
16 | 2 | 2 | 1026 | use Try::Tiny; | |||
2 | 3918 | ||||||
2 | 1329 | ||||||
17 | |||||||
18 | sub TEMPLATE { | ||||||
19 | 3 | 3 | 0 | 29 | <<'EOTMPL' } | ||
20 | % my $stash = $_[0]; | ||||||
21 | |||||||
22 | |||||||
23 | |||||||
26 | |
||||||
27 | |||||||
28 | |
||||||
29 | % if ($stash->{panels}) { | ||||||
30 | |
||||||
31 | % } else { | ||||||
32 | |
||||||
33 | % } | ||||||
34 | % for my $panel (reverse @{$stash->{panels}}) { | ||||||
35 | |
||||||
36 | % if ($panel->content) { | ||||||
37 | |||||||
38 | % } else { | ||||||
39 | |
||||||
40 | % } | ||||||
41 | <%= $panel->nav_title %> | ||||||
42 | % if ($panel->nav_subtitle) { | ||||||
43 | <%= $panel->nav_subtitle %> |
||||||
44 | % } | ||||||
45 | % if ($panel->content) { | ||||||
46 | |||||||
47 | % } else { | ||||||
48 | |||||||
49 | % } | ||||||
50 | |||||||
51 | % } # end for | ||||||
52 | |||||||
53 | |||||||
54 | |||||||
55 | « | ||||||
56 | |||||||
57 | % for my $panel (reverse @{$stash->{panels}}) { | ||||||
58 | % if ($panel->content) { | ||||||
59 | |
||||||
60 | |
||||||
61 | Close | ||||||
62 | <%= $panel->title %> |
||||||
63 | |||||||
64 | |
||||||
65 | |
||||||
66 | % my $content = ref $panel->content eq 'CODE' ? $panel->content->() : $panel->content; | ||||||
67 | % $content = Encode::encode('latin1', $content, Encode::FB_XMLCREF); | ||||||
68 | <%= Text::MicroTemplate::encoded_string($content) %> | ||||||
69 | |||||||
70 | |||||||
71 | |||||||
72 | % } | ||||||
73 | % } # end for | ||||||
74 | |||||||
75 | |||||||
76 | EOTMPL | ||||||
77 | |||||||
78 | sub default_panels { | ||||||
79 | 1 | 1 | 0 | 48 | [qw(Environment Response Timer Memory Session DBITrace)]; | ||
80 | } | ||||||
81 | |||||||
82 | sub prepare_app { | ||||||
83 | 3 | 3 | 1 | 7067 | my $self = shift; | ||
84 | 3 | 50 | 3 | 26 | my $root = try { File::ShareDir::dist_dir('Plack-Middleware-Debug') } || 'share'; | ||
3 | 150 | ||||||
85 | |||||||
86 | 3 | 539 | my $builder = Plack::Builder->new; | ||||
87 | |||||||
88 | 3 | 100 | 30 | for my $spec (@{ $self->panels || $self->default_panels }) { | |||
3 | 15 | ||||||
89 | 20 | 481 | my ($package, %args); | ||||
90 | 20 | 50 | 59 | if (ref $spec eq 'ARRAY') { | |||
91 | # For the backward compatibility | ||||||
92 | # [ 'PanelName', key1 => $value1, ... ] | ||||||
93 | 0 | 0 | $package = shift @$spec; | ||||
94 | 0 | 0 | 0 | $package = "Debug::$package" unless $package =~ /^\+/s; | |||
95 | 0 | 0 | $builder->add_middleware($package, @$spec); | ||||
96 | } else { | ||||||
97 | # $spec could be a code ref (middleware) or a string | ||||||
98 | # copy so that we do not change default_panels | ||||||
99 | 20 | 39 | my $spec_copy = $spec; | ||||
100 | 20 | 50 | 52 | unless (ref $spec_copy) { | |||
101 | 20 | 100 | 82 | $spec_copy = "Debug::$spec_copy" unless $spec_copy =~ /^\+/s;; | |||
102 | } | ||||||
103 | 20 | 71 | $builder->add_middleware($spec_copy); | ||||
104 | } | ||||||
105 | } | ||||||
106 | |||||||
107 | 3 | 695 | $self->app( $builder->to_app($self->app) ); | ||||
108 | |||||||
109 | 3 | 812 | $self->renderer( | ||||
110 | Text::MicroTemplate->new( | ||||||
111 | template => $self->TEMPLATE, | ||||||
112 | tag_start => '<%', | ||||||
113 | tag_end => '%>', | ||||||
114 | line_start => '%', | ||||||
115 | )->build | ||||||
116 | ); | ||||||
117 | |||||||
118 | 3 | 14556 | $self->files(Plack::App::File->new(root => $root)); | ||||
119 | } | ||||||
120 | |||||||
121 | sub call { | ||||||
122 | 3 | 3 | 1 | 41211 | my ($self, $env) = @_; | ||
123 | 3 | 50 | 15 | if ($env->{PATH_INFO} =~ m!^/debug_toolbar!) { | |||
124 | 0 | 0 | return $self->files->call($env); | ||||
125 | } | ||||||
126 | |||||||
127 | 3 | 11 | $env->{'plack.debug.panels'} = []; | ||||
128 | |||||||
129 | 3 | 15 | my $res = $self->app->($env); | ||||
130 | $self->response_cb($res, sub { | ||||||
131 | 3 | 3 | 77 | my $res = shift; | |||
132 | 3 | 36 | my $headers = Plack::Util::headers($res->[1]); | ||||
133 | 3 | 392 | my $panels = delete $env->{'plack.debug.panels'}; | ||||
134 | 3 | 50 | 50 | 27 | if ( ! Plack::Util::status_with_no_entity_body($res->[0]) | ||
33 | |||||||
135 | && ($headers->get('Content-Type') || '') =~ m!^(?:text/html|application/xhtml\+xml)!) { | ||||||
136 | |||||||
137 | my $vars = { | ||||||
138 | panels => [ grep !$_->disabled, @$panels ], | ||||||
139 | BASE_URL => $env->{SCRIPT_NAME}, | ||||||
140 | 3 | 339 | }; | ||||
141 | |||||||
142 | 3 | 160 | my $content = $self->renderer->($vars); | ||||
143 | return sub { | ||||||
144 | 6 | 181 | my $chunk = shift; | ||||
145 | 6 | 100 | 29 | return unless defined $chunk; | |||
146 | 3 | 42 | $chunk =~ s!(?= |
)!$content!i;