use
5.008;
our
$VERSION
=
'0.18'
;
sub
call {
my
(
$self
,
$env
) =
@_
;
my
$panel
=
$self
->default_panel;
my
$after
=
$self
->run(
$env
,
$panel
);
$self
->response_cb(
$self
->app->(
$env
),
sub
{
my
$res
=
shift
;
$after
->(
$res
)
if
$after
&&
ref
$after
eq
'CODE'
;
push
@{
$env
->{
'plack.debug.panels'
}},
$panel
;
});
}
sub
run { }
sub
panel_id {
my
$self
=
shift
;
(
my
$name
=
ref
$self
) =~ s/.*:://;
$name
. Scalar::Util::refaddr(
$self
);
}
sub
panel_name {
my
$self
=
shift
;
(
my
$name
=
ref
$self
) =~ s/.*:://;
$name
=~ s/(?<=[a-z])(?=[A-Z])/ /g;
$name
;
}
sub
default_panel {
my
(
$self
,
$env
) =
@_
;
my
$id
=
$self
->panel_id;
my
$name
=
$self
->panel_name;
my
$panel
= Plack::Middleware::Debug::Panel->new;
$panel
->dom_id(
"plDebug${id}Panel"
);
$panel
->url(
'#'
);
$panel
->title(
$name
);
$panel
->nav_title(
$name
);
$panel
->nav_subtitle(
''
);
$panel
->content(
''
);
$panel
;
}
sub
vardump {
my
$scalar
=
shift
;
return
'(undef)'
unless
defined
$scalar
;
return
"$scalar"
unless
ref
$scalar
;
return
scalar
Dumper(
$scalar
);
}
sub
build_template {
my
$class
=
shift
;
Text::MicroTemplate->new(
template
=>
$_
[0],
tag_start
=>
'<%'
,
tag_end
=>
'%>'
,
line_start
=>
'%'
,
)->build;
}
sub
render {
my
(
$self
,
$template
,
$vars
) =
@_
;
$template
->(
$vars
);
}
my
$list_section_template
= __PACKAGE__->build_template(
<<'EOTMPL');
% foreach my $s (@{$_[0]->{sections}}) {
<h3><%= ucfirst $s %></h3>
% if (scalar @{$_[0]->{list}->{$s}}) {
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
% my $i;
% while (@{$_[0]->{list}->{$s}}) {
% my($key, $value) = splice(@{$_[0]->{list}->{$s}}, 0, 2);
<tr class="<%= ++$i % 2 ? 'plDebugOdd' : 'plDebugEven' %>">
<td><%= $key %></td>
<td><%= vardump($value) %></td>
</tr>
% }
</tbody>
</table>
% }
% }
EOTMPL
my
$list_template
= __PACKAGE__->build_template(
<<'EOTMPL');
<table>
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
% my $i;
% while (@{$_[0]->{list}}) {
% my($key, $value) = splice(@{$_[0]->{list}}, 0, 2);
<tr class="<%= ++$i % 2 ? 'plDebugOdd' : 'plDebugEven' %>">
<td><%= $key %></td>
<td><pre><%= vardump($value) %></pre></td>
</tr>
% }
</tbody>
</table>
EOTMPL
my
$line_template
= __PACKAGE__->build_template(
<<'EOTMPL');
<table>
<tbody>
% my $i;
% if (defined $_[0]->{lines}) {
% my @lines = ref $_[0]->{lines} eq 'ARRAY' ? @{$_[0]->{lines}} : split /\r?\n/, $_[0]->{lines};
% for my $line (@lines) {
<tr class="<%= ++$i % 2 ? 'plDebugEven' : 'plDebugOdd' %>">
<td><%= $line %></td>
</tr>
% }
% }
</tbody>
</table>
EOTMPL
sub
render_lines {
my
(
$self
,
$lines
) =
@_
;
$self
->render(
$line_template
, {
lines
=>
$lines
});
}
sub
render_list_pairs {
my
(
$self
,
$list
,
$sections
) =
@_
;
if
(
$sections
) {
$self
->render(
$list_section_template
, {
list
=>
$list
,
sections
=>
$sections
});
}
else
{
$self
->render(
$list_template
, {
list
=>
$list
});
}
}
sub
render_hash {
my
(
$self
,
$hash
,
$sections
) =
@_
;
if
(
$sections
) {
my
%hash
;
foreach
my
$section
(
keys
%$hash
) {
push
@{
$hash
{
$section
} },
map
{
$_
=>
$hash
->{
$section
}->{
$_
} }
sort
keys
%{
$hash
->{
$section
} };
}
$self
->render(
$list_section_template
,
{
sections
=>
$sections
,
list
=> \
%hash
} );
}
else
{
my
@hash
=
map
{
$_
=>
$hash
->{
$_
} }
sort
keys
%$hash
;
$self
->render(
$list_template
, {
list
=> \
@hash
} );
}
}
1;