File Coverage

blib/lib/Dancer2/Debugger.pm
Criterion Covered Total %
statement 24 26 92.3
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 33 35 94.2


line stmt bran cond sub pod time code
1             package Dancer2::Debugger;
2              
3 1     1   47892 use 5.006;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         16  
5 1     1   3 use warnings;
  1         8  
  1         36  
6              
7             =head1 NAME
8              
9             Dancer2::Debugger - Dancer2 panels for Plack::Debugger
10              
11             =head1 VERSION
12              
13             0.008
14              
15             =cut
16              
17             our $VERSION = '0.008';
18              
19 1     1   553 use Dancer2::Core::Types;
  1         8102  
  1         238  
20 1     1   7 use File::Spec;
  1         1  
  1         20  
21 1     1   461 use JSON::MaybeXS;
  1         5678  
  1         74  
22 1     1   410 use Module::Find qw/findallmod/;
  1         876  
  1         48  
23 1     1   18 use Module::Runtime qw/use_module/;
  1         2  
  1         5  
24 1     1   347 use Plack::App::Debugger;
  0            
  0            
25             use Plack::Builder ();
26             use Plack::Debugger;
27             use Plack::Debugger::Storage;
28             use Plack::Middleware::Debugger::Injector;
29              
30             use Moo;
31             use namespace::clean;
32              
33             =head1 SYNOPSIS
34              
35             In your .psgi file:
36              
37             #!/usr/bin/env perl
38              
39             use strict;
40             use warnings;
41             use FindBin;
42             use lib "$FindBin::Bin/../lib";
43              
44             use Plack::Builder;
45              
46             use Dancer2::Debugger;
47             my $debugger = Dancer2::Debugger->new;
48              
49             use MyApp;
50             my $app = MyApp->to_app;
51              
52             builder {
53             $debugger->mount;
54             mount '/' => builder {
55             $debugger->enable;
56             $app;
57             }
58             };
59              
60             In environments/development.yml file:
61              
62             plugins:
63             Debugger:
64             enabled: 1
65              
66             In MyApp.pm:
67              
68             use Dancer2::Plugin::Debugger
69              
70             =head1 DESCRIPTION
71              
72             L makes using the excellent L much more
73             convenient and in addition provides a number of Dancer2 panels.
74              
75             Current panels included with this distribution:
76              
77             =over
78              
79             =item L
80              
81             =item L
82              
83             =item L
84              
85             =item L
86              
87             =item L
88              
89             =item L
90              
91             =back
92              
93             Some of the debugger panels make use of collectors which are imported into
94             your L app using L which is also
95             included in this distribution.
96              
97             =head1 ATTRIBUTES
98              
99             =head2 app
100              
101             Instantiated L object.
102              
103             =cut
104              
105             has app => (
106             is => 'ro',
107             lazy => 1,
108             default => sub {
109             my $self = shift;
110             Plack::App::Debugger->new( debugger => $self->debugger );
111             },
112             );
113              
114             =head2 data_dir
115              
116             See L.
117              
118             Defaults to C in the system temp directory (usually C
119             on Linux/UNIX systems).
120              
121             Attempts to create the directory if it does not exist.
122              
123             =cut
124              
125             has data_dir => (
126             is => 'ro',
127             default => sub {
128             my $dir = File::Spec->catfile( File::Spec->tmpdir, 'debugger_panel' );
129             return $dir if ( -d $dir || mkdir $dir );
130             die "Unable to create data_dir $dir: $!";
131             },
132             );
133              
134             =head2 debugger
135              
136             Instantiated L object.
137              
138             =cut
139              
140             has debugger => (
141             is => 'ro',
142             lazy => 1,
143             default => sub {
144             my $self = shift;
145             Plack::Debugger->new(
146             storage => $self->storage,
147             panels => $self->panel_objects,
148             );
149             }
150             );
151              
152             =head2 deserializer
153              
154             See L.
155              
156             Defaults to the value of L.
157              
158             =cut
159              
160             has deserializer => (
161             is => 'ro',
162             isa => Object,
163             lazy => 1,
164             default => sub { shift->serializer },
165             );
166              
167             =head2 filename_fmt
168              
169             See L.
170              
171             Defaults to C<%s.json>.
172              
173             =cut
174              
175             has filename_fmt => (
176             is => 'ro',
177             default => '%s.json',
178             );
179              
180             =head2 injector_ignore_status
181              
182             If set to a true value then we override
183             L to always
184             return false so that the injector tries to add the javascript snippet to the
185             page irrespective of the http status code.
186              
187             Defaults to false.
188              
189             =cut
190              
191             has injector_ignore_status => (
192             is => 'ro',
193             isa => Bool,
194             default => 0,
195             );
196              
197             =head2 panels
198              
199             Array reference of panel class names to load. Defaults to all classes
200             found in C<@INC> under L.
201              
202             =cut
203              
204             has panels => (
205             is => 'ro',
206             isa => ArrayRef,
207             default => sub {
208             my @found = findallmod Plack::Debugger::Panel;
209             return [ sort @found ];
210             },
211             );
212              
213             =head2 panel_objects
214              
215             Imported and instantiated panel objects.
216              
217             =cut
218              
219             has panel_objects => (
220             is => 'ro',
221             isa => ArrayRef,
222             lazy => 1,
223             default => sub {
224             my $self = shift;
225             my @panels;
226             foreach my $panel ( @{ $self->panels } ) {
227             push @panels, use_module($panel)->new;
228             }
229             return [@panels];
230             },
231             );
232              
233             =head2 serializer
234              
235             See L.
236              
237             Defaults to C<< JSON::MaybeXS->new( convert_blessed => 1, utf8 => 1 ) >>
238              
239             =cut
240              
241             has serializer => (
242             is => 'ro',
243             isa => Object,
244             default => sub {
245             JSON::MaybeXS->new(
246             convert_blessed => 1,
247             allow_blessed => 1,
248             allow_unknown => 1,
249             utf8 => 1,
250             );
251             },
252             );
253              
254             =head2 storage
255              
256             Instantiated L object.
257              
258             =cut
259              
260             has storage => (
261             is => 'ro',
262             lazy => 1,
263             default => sub {
264             my $self = shift;
265             Plack::Debugger::Storage->new(
266             data_dir => $self->data_dir,
267             serializer => sub { $self->serializer->encode(shift) },
268             deserializer => sub { $self->deserializer->decode(shift) },
269             filename_fmt => $self->filename_fmt,
270             );
271             },
272             );
273              
274             =head1 METHODS
275              
276             =head2 BUILD
277              
278             Handle L if it is true.
279              
280             =cut
281              
282             sub BUILD {
283             my $self = shift;
284             if ( $self->injector_ignore_status ) {
285             no warnings 'redefine';
286             *Plack::Middleware::Debugger::Injector::should_ignore_status = sub {
287             return 0;
288             };
289             }
290             }
291              
292             =head2 enable
293              
294             Convenience method for use in psgi file which runs the following methods:
295              
296             L and
297             L.
298              
299             =cut
300              
301             sub enable {
302             my $self = shift;
303             Plack::Builder::enable $self->app->make_injector_middleware;
304             Plack::Builder::enable $self->debugger->make_collector_middleware;
305             }
306              
307             =head2 mount
308              
309             Convenience method for use in psgi file to mount L.
310              
311             =cut
312              
313             sub mount {
314             my $self = shift;
315             Plack::Builder::mount $self->app->base_url => $self->app->to_app;
316             }
317              
318             1;
319             __END__