File Coverage

blib/lib/Dancer2/Debugger.pm
Criterion Covered Total %
statement 18 20 90.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 25 27 92.5


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