blib/lib/Dancer2/Plugin.pm

filter: Statementstmt 98.1% 262 / 267 Branchbran 85.0% 68 / 80 Conditioncond 62.9% 17 / 27 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 96.4% 55 / 57 Podpod 25.0% 6 / 24 totaltotal 87.2% 411 / 471 CC 72 SCAR 9.6
CC
72
Cov
93%
_exporter_expand_tag
8.1
_exporter_app
7.1
__ANON__
6.2
CRAP 2.6
« lib/Dancer2/Core/Role/SessionFactory.pm ↑ Summary lib/Dancer2/Core/HTTP.pm »
1 package Dancer2::Plugin;
2 # ABSTRACT: base class for Dancer2 plugins
3 23 $Dancer2::Plugin::VERSION = '2.3.0';
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 38 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use List::Util qw/ reduce /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Ref::Util qw<is_arrayref is_coderef>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 23 our $CUR_PLUGIN;
16  
17 23 extends 'Exporter::Tiny';
18  
19 23 with 'Dancer2::Core::Role::Hookable';
20  
21 23 has app => (
22 is => 'ro',
23 weak_ref => 1,
24 required => 1,
25 );
26  
27 has config => (
28 is => 'ro',
29 lazy => 1,
30 9 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 9 my $self = shift;
32 9 my $config = $self->app->config;
33 9 my $package = ref $self; # TODO
34 9 $package =~ s/Dancer2::Plugin:://;
35 9 ▶ $config->{plugins}{$package} || {};
Branch: 1 branch
BranchTrueFalseTotal
$config->{"plugins"}{$package} or {} 8 1 9
36 },
37 23 );
38  
39 23 my $_keywords = {};
40 0 ▶sub keywords { $_keywords }
Subroutine keywords: not called CC=1 SCAR=6.9
Pod documented
41  
42 23 my $REF_ADDR_REGEX = qr{
43 [A-Za-z0-9\:\_]+
44 =HASH
45 \(
46 ([0-9a-fx]+)
47 \)
48 }x;
49 23 my %instances;
50  
51 # backwards compatibility
52 23 our $_keywords_by_plugin = {};
53  
54 has '+hooks' => (
55 7 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 7 my $plugin = shift;
57 7 my $name = 'plugin.' . lc ref $plugin;
58 7 $name =~ s/Dancer2::Plugin:://i;
59 7 $name =~ s/::/_/g;
60  
61 +{
62 8 map { join( '.', $name, $_ ) => [] }
63 7 @{ $plugin->ClassHooks }
64 };
65 },
66 23 );
67  
68 7 ▶sub add_hooks {
Subroutine add_hooks: called CC=1 SCAR=0.0
Pod undocumented
69 7 my $class = shift;
70 7 push @{ $class->ClassHooks }, @_;
71 }
72  
73 1 ▶sub execute_plugin_hook {
Subroutine execute_plugin_hook: called CC=2 SCAR=7.0
Pod undocumented
74 1 my ( $self, $name, @args ) = @_;
75 1 my $plugin_class = ref $self;
76  
77 1 ▶ $self->isa('Dancer2::Plugin')
Branch: 1 branch
BranchTrueFalseTotal
$self->isa("Dancer2::Plugin") or croak("Cannot call plugin hook ($name) from outside plugin") 1 0 1
78 or croak "Cannot call plugin hook ($name) from outside plugin";
79 1 $plugin_class =~ s/^Dancer2::Plugin:://; # short names
80  
81 1 my $full_name = 'plugin.' . lc($plugin_class) . ".$name";
82 1 $full_name =~ s/::/_/g;
83  
84 1 $self->app->execute_hook( $full_name, @args );
85 }
86  
87 2 ▶sub find_plugin {
Subroutine find_plugin: called CC=1 SCAR=0.0
Pod undocumented
88 2 my ( $self, $name ) = @_;
89 2 return $self->app->find_plugin($name);
90 }
91  
92 # both functions are there for D2::Core::Role::Hookable
93 # back-compatibility. Aren't used
94 0 ▶sub supported_hooks { [] }
Subroutine supported_hooks: not called CC=1 SCAR=6.9
Pod undocumented
95 20 ▶sub hook_aliases { $_[0]->{'hook_aliases'} ||= {} }
Subroutine hook_aliases: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
$_[0]{"hook_aliases"} ||= {}
l!l
1010
MC/DC100%1/1
$_[0]{"hook_aliases"} ||= {}
$_[0]{"hook_aliases"}
Truth table100%2/2
A
A= $_[0]{"hook_aliases"}
Aresult
11
00
96  
97 ### has() STUFF ########################################
98  
99 # our wrapping around Moo::has, done to be able to intercept
100 # both 'from_config' and 'plugin_keyword'
101 23 ▶sub _p2_has {
Subroutine _p2_has: called CC=1 SCAR=0.0
102 23 my $class = shift;
103 23 $class->_p2_has_from_config( $class->_p2_has_keyword( @_ ) );
104 };
105  
106 23 ▶sub _p2_has_from_config {
Subroutine _p2_has_from_config: called CC=6 SCAR=17.9
107 23 my( $class, $name, %args ) = @_;
108  
109 23 ▶ my $config_name = delete $args{'from_config'}
Branch: 1 branch
BranchTrueFalseTotal
my $config_name = delete $args{"from_config"} or return $name, %args 8 15 23
110 or return ( $name, %args );
111  
112 8 $args{lazy} = 1;
113  
114 8 ▶ if ( is_coderef($config_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_coderef($config_name)) 2 6 8
115 2 ▶ $args{default} ||= $config_name;
Condition66%2/3
$args{"default"} ||= $config_name
l!l&&r!l&&!r
110
MC/DC0%0/2
$args{"default"} ||= $config_name
$args{"default"} $config_name
Truth table66%2/3
A ||= B
A= $args{"default"}B= $config_name
ABresult
1X1
011
000
116 2 $config_name = 1;
117 }
118  
119 8 ▶ $config_name = $name if $config_name eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $config_name eq 1 7 1 8
120 8 ▶ my $orig_default = $args{default} || sub{};
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$args{'default'} || sub { }
l!l&&r!l&&!r
350
MC/DC0%0/2
$args{'default'} || sub { }
$args{'default'} sub { }
Truth table66%2/3
A || B
A= $args{'default'}B= sub { }
ABresult
1X1
011
000
121 8 ▶ $args{default} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
122 8 my $plugin = shift;
123 8 ▶ my $value = reduce { eval { $a->{$b} } } $plugin->config, split /\./, $config_name;
Subroutine __ANON__: called CC=1 SCAR=0.0
124 8 ▶ return defined $value ? $value: $orig_default->($plugin);
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 4 4 8
125 8 };
126  
127 8 return $name => %args;
128 }
129  
130 23 ▶sub _p2_has_keyword {
Subroutine _p2_has_keyword: called CC=5 SCAR=16.1
131 23 my( $class, $name, %args ) = @_;
132  
133 23 ▶ if( my $keyword = delete $args{plugin_keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keyword = delete $args{"plugin_keyword"}) 8 15 23
134  
135 8 ▶ $keyword = $name if $keyword eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $keyword eq 1 6 2 8
136  
137 6 ▶ $class->keywords->{$_} = sub { (shift)->$name(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
138 8 ▶ for ref $keyword ? @$keyword : $keyword;
Branch: 1 branch
BranchTrueFalseTotal
ref $keyword ? : 1 7 8
139 }
140  
141 23 return $name => %args;
142 }
143  
144 ### ATTRIBUTE HANDLER STUFF ########################################
145  
146 # :PluginKeyword shenanigans
147  
148 6 ▶sub PluginKeyword :ATTR(CODE,BEGIN) {
Subroutine PluginKeyword: called CC=5 SCAR=16.2
Pod undocumented
149 6 my( $class, $sym_ref, $code, undef, $args ) = @_;
150  
151 # importing at BEGIN stage doesn't work with 5.10 :-(
152 6 ▶ return unless ref $sym_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sym_ref 0 6 6
153  
154 6 my $func_name = *{$sym_ref}{NAME};
155  
156 6 ▶ $args = join '', @$args if is_arrayref($args);
Branch: 1 branch
BranchTrueFalseTotal
if is_arrayref($args) 1 5 6
157  
158 6 ▶ for my $name ( split ' ', $args || $func_name ) {
Condition66%2/3
$args || $func_name
l!l&&r!l&&!r
240
MC/DC0%0/2
$args || $func_name
$args $func_name
Truth table66%2/3
A || B
A= $argsB= $func_name
ABresult
1X1
011
000
159 7 $class->keywords->{$name} = $code;
160 }
161  
162 24 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164 ## EXPORT STUFF ##############################################################
165  
166 # this @EXPORT will only be taken
167 # into account when we do a 'use Dancer2::Plugin'
168 # I.e., it'll only do its magic for the
169 # plugins themselves, not when they are
170 # called
171 23 our @EXPORT = qw/ :plugin /;
172  
173 # compatibility - it will be removed soon!
174 23 my $no_dsl = {};
175 23 my $exported_app = {};
176 69 ▶sub _exporter_expand_tag {
Subroutine _exporter_expand_tag: called CC=7 SCAR=21.0
177 69 my( $class, $name, $args, $global ) = @_;
178  
179 69 my $caller = $global->{into};
180  
181 69 ▶ $name eq 'no_dsl' and $no_dsl->{$caller} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$name eq "no_dsl" and $no_dsl->{$caller} = 1 1 68 69
182 # no_dsl check here is for compatibility only
183 # it will be removed soon!
184 69 ▶ return _exporter_plugin($caller)
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "plugin" or $name eq "no_dsl" 38 31 69
Condition100%3/3
$name eq "plugin" or $name eq "no_dsl"
l!l&&r!l&&!r
37131
MC/DC100%2/2
$name eq "plugin" or $name eq "no_dsl"
$name eq "plugin" $name eq "no_dsl"
Truth table100%3/3
A or B
A= $name eq "plugin"B= $name eq "no_dsl"
ABresult
1X1
011
000
185 if $name eq 'plugin' or $name eq 'no_dsl';
186  
187 return _exporter_app($class,$caller,$global)
188 31 ▶ if $name eq 'app' and $caller->can('app') and !$no_dsl->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "app" and $caller->can("app") and not $no_dsl->{$class} 31 0 31
Condition33%2/6
$name eq "app" and $caller->can("app") and not $no_dsl->{$class}
!ll&&!rl&&r
0031
$name eq "app" and $caller->can("app")
!ll&&!rl&&r
0031
MC/DC0%0/3
$name eq "app" and $caller->can("app") and not $no_dsl->{$class}
$name eq "app" $caller->can("app") not $no_dsl->{$class}
Truth table25%1/4
A and B and not(C)
A= $name eq "app"B= $caller->can("app")C= not $no_dsl->{$class}
ABCresult
0XX0
10X0
1100
1111
189  
190 0 return;
191  
192 }
193  
194 # plugin has been called within a D2 app. Modify
195 # the app and export keywords
196 31 ▶sub _exporter_app {
Subroutine _exporter_app: called CC=7 SCAR=19.6
197 31 my( $class, $caller, $global ) = @_;
198  
199 31 $exported_app->{$caller} = 1;
200  
201 # The check for ->dsl->app is to handle plugins as well.
202 # Otherwise you can only import from a plugin to an app,
203 # but with this, you can import to anything
204 # that has a DSL with an app, which translates to "also plugins"
205 31 ▶ my $app = eval("${caller}::app()") || eval { $caller->dsl->app } ## no critic qw(BuiltinFunctions::ProhibitStringyEval)
Branch: 1 branch
BranchTrueFalseTotal
my $app = eval "${caller}::app()" || eval { do { $caller->dsl->app } } or return 31 0 31
Condition66%2/3
eval "${caller}::app()" || eval { do { $caller->dsl->app } }
l!l&&r!l&&!r
3010
MC/DC0%0/2
eval "${caller}::app()" || eval { do { $caller->dsl->app } }
eval "${caller}::app()" eval { do { $caller->dsl->app } }
Truth table66%2/3
A || B
A= eval "${caller}::app()"B= eval { do { $caller->dsl->app } }
ABresult
1X1
011
000
206 or return; ## no critic
207  
208 31 ▶ return unless $app->can('with_plugin');
Branch: 1 branch
BranchTrueFalseTotal
unless $app->can("with_plugin") 0 31 31
209  
210 31 my $plugin = $app->with_plugin( '+' . $class );
211 31 $global->{'plugin'} = $plugin;
212  
213 31 ▶ return unless $class->can('keywords');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("keywords") 0 31 31
214  
215 # Add our hooks to the app, so they're recognized
216 # this is for compatibility so you can call execute_hook()
217 # without the fully qualified plugin name.
218 # The reason we need to do this here instead of when adding a
219 # hook is because we need to register in the app, and only now it
220 # exists.
221 # This adds a caveat that two plugins cannot register
222 # the same hook name, but that will be deprecated anyway.
223 {;
224 31 foreach my $hook ( @{ $plugin->ClassHooks } ) {
225 7 my $full_name = 'plugin.' . lc($class) . ".$hook";
226 7 $full_name =~ s/Dancer2::Plugin:://i;
227 7 $full_name =~ s/::/_/g;
228  
229 # this adds it to the plugin
230 7 $plugin->hook_aliases->{$hook} = $full_name;
231  
232 # this adds it to the app
233 7 $plugin->app->hook_aliases->{$hook} = $full_name;
234  
235 # copy the hooks from the plugin to the app
236 # this is in case they were created at import time
237 # rather than after
238 7 @{ $plugin->app->hooks }{ keys %{ $plugin->hooks } } =
239 7 values %{ $plugin->hooks };
240 }
241 }
242  
243 {
244 # get the reference
245 31 my ($plugin_addr) = "$plugin" =~ $REF_ADDR_REGEX;
246  
247 31 ▶ $instances{$plugin_addr}{'config'} = sub { $plugin->config };
Subroutine __ANON__: called CC=1 SCAR=0.0
248 31 $instances{$plugin_addr}{'app'} = $plugin->app;
249  
250 31 Scalar::Util::weaken( $instances{$plugin_addr}{'app'} );
251  
252 ## no critic
253 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
254  
255 # we used a forward declaration
256 # so the compiled form "plugin_setting;" can be overridden
257 # with this implementation,
258 # which works on runtime ("plugin_setting()")
259 # we can't use can() here because the forward declaration will
260 # create a CODE stub
261 23 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
262 31 ▶ *{"${class}::plugin_setting"} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
263 5 my ($plugin_addr) = "$CUR_PLUGIN" =~ $REF_ADDR_REGEX;
264  
265 5 ▶ $plugin_addr
Branch: 1 branch
BranchTrueFalseTotal
$plugin_addr or Carp::croak("Can't find originating plugin") 5 0 5
266 or Carp::croak('Can\'t find originating plugin');
267  
268 # we need to do this because plugins might call "set"
269 # in order to change plugin configuration but it doesn't
270 # change the plugin object, it changes the app object
271 # so we merge them.
272 5 my $name = ref $CUR_PLUGIN;
273 5 $name =~ s/^Dancer2::Plugin:://g;
274  
275 5 my $plugin_inst = $instances{$plugin_addr};
276 5 my $plugin_config = $plugin_inst->{'config'}->();
277 5 my $app_plugin_config = $plugin_inst->{'app'}->config->{'plugins'}{$name};
278  
279 5 ▶ return { %{ $plugin_config || {} }, %{ $app_plugin_config || {} } };
Condition50%2/4
$plugin_config || {}
l!l
50
$app_plugin_config || {}
l!l
50
MC/DC0%0/1
$plugin_config || {}
$plugin_config
MC/DC0%0/1
$app_plugin_config || {}
$app_plugin_config
Truth table50%1/2
A
A= $plugin_config
Aresult
11
00
Truth table50%1/2
A
A= $app_plugin_config
Aresult
11
00
280 31 };
281  
282 # FIXME:
283 # why doesn't this work? it's like it's already defined somewhere
284 # but i'm not sure where. seems like AUTOLOAD runs it.
285 #$class->can('execute_hook') or
286 31 ▶ *{"${class}::execute_hook"} = sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
287 # this can also be called by App.pm itself
288 # if the plugin is a
289 # "candidate" for a hook
290 # See: App.pm "execute_hook" method, "around" modifier
291 5 ▶ if ( $_[0]->isa('Dancer2::Plugin') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->isa("Dancer2::Plugin")) 2 3 5
292 # this means it's probably our hook, we need to verify it
293 2 my ( $plugin_self, $hook_name, @args ) = @_;
294  
295 2 my $plugin_class = lc $class;
296 2 $plugin_class =~ s/^dancer2::plugin:://;
297 2 $plugin_class =~ s{::}{_}g;
298  
299 # you're either calling it with the full qualifier or not
300 # if not, use the execute_plugin_hook instead
301 2 ▶ if ( $plugin->hooks->{"plugin.$plugin_class.$hook_name"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->hooks->{"plugin.$plugin_class.$hook_name"}) 0 2 2
302 0 Carp::carp("Please use fully qualified hook name or "
303 . "the method execute_plugin_hook");
304 0 $hook_name = "plugin.$plugin_class.$hook_name";
305 }
306  
307 2 ▶ $hook_name =~ /^plugin\.$plugin_class/
Branch: 1 branch
BranchTrueFalseTotal
$hook_name =~ /^plugin\.$plugin_class/ or Carp::croak("Unknown plugin called through other plugin") 2 0 2
308 or Carp::croak('Unknown plugin called through other plugin');
309  
310 # now we can't really use the app to execute it because
311 # the "around" modifier is the one calling us to begin
312 # with, so we need to call it directly ourselves
313 # this is okay because the modifier is there only to
314 # call candidates, like us (this is in fact how and
315 # why we were called)
316 $_->( $plugin_self, @args )
317 2 for @{ $plugin->hooks->{$hook_name} };
318  
319 2 return;
320 }
321  
322 3 return $plugin->app->execute_hook(@_);
323 31 };
324 }
325  
326 31 local $CUR_PLUGIN = $plugin;
327 31 $_->($plugin) for @{ $plugin->_DANCER2_IMPORT_TIME_SUBS() };
328  
329 31 map { [ $_ => {plugin => $plugin} ] } keys %{ $plugin->keywords };
330 }
331  
332 # turns the caller namespace into
333 # a D2P2 class, with exported keywords
334 38 ▶sub _exporter_plugin {
Subroutine _exporter_plugin: called CC=3 SCAR=11.0
335 38 my $caller = shift;
336 38 require_module('Dancer2::Core::DSL');
337 38 my $keywords_list = join ' ', keys %{ Dancer2::Core::DSL->dsl_keywords };
338  
339 38 ▶ eval <<"END"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
340 {
341 package $caller;
342 use Moo;
343 use Carp ();
344 use Attribute::Handlers;
345  
346 extends 'Dancer2::Plugin';
347  
348 our \@EXPORT = ( ':app' );
349  
350 around has => sub {
351 my( \$orig, \$name, \%args ) = \@_;
352  
353 if (ref \$name eq 'ARRAY'
354 && exists \$args{'plugin_keyword'}
355 && ref \$args{'plugin_keyword'} eq 'ARRAY') {
356  
357 Carp::croak('Setting "plugin_keyword" to an array is disallowed'
358 . ' when defining multiple attributes simultaneously');
359 }
360  
361 \$orig->( ${caller}->_p2_has( \$_, \%args) )
362 for ref \$name ? @\$name : \$name;
363 };
364  
365 sub PluginKeyword :ATTR(CODE,BEGIN) {
366 goto &Dancer2::Plugin::PluginKeyword;
367 }
368  
369 sub execute_plugin_hook {
370 goto &Dancer2::Plugin::execute_plugin_hook;
371 }
372  
373 my \$_keywords = {};
374 sub keywords { \$_keywords }
375  
376 my \$_ClassHooks = [];
377 sub ClassHooks { \$_ClassHooks }
378  
379 # this is important as it'll do the keywords mapping between the
380 # plugin and the app
381 sub register_plugin { Dancer2::Plugin::register_plugin(\@_) }
382  
383 sub register {
384 my ( \$keyword, \$sub ) = \@_;
385 \$_keywords->{\$keyword} = \$sub;
386  
387 \$keyword =~ /^[a-zA-Z_]+[a-zA-Z0-9_]*\$/
388 or Carp::croak(
389 "You can't use '\$keyword', it is an invalid name"
390 . " (it should match ^[a-zA-Z_]+[a-zA-Z0-9_]*\\\$ )");
391  
392  
393 grep +( \$keyword eq \$_ ), qw<$keywords_list>
394 and Carp::croak("You can't use '\$keyword', this is a reserved keyword");
395  
396 \$Dancer2::Plugin::_keywords_by_plugin->{\$keyword}
397 and Carp::croak("You can't use \$keyword, "
398 . "this is a keyword reserved by "
399 . \$Dancer2::Plugin::_keywords_by_plugin->{\$keyword});
400  
401 \$Dancer2::Plugin::_keywords_by_plugin->{\$keyword} = "$caller";
402  
403 # Exporter::Tiny doesn't seem to generate the subs
404 # in the caller properly, so we have to do it manually
405 {
406 no strict 'refs';
407 *{"${caller}::\$keyword"} = \$sub;
408 }
409 }
410  
411 my \@_DANCER2_IMPORT_TIME_SUBS;
412 sub _DANCER2_IMPORT_TIME_SUBS {\\\@_DANCER2_IMPORT_TIME_SUBS}
413 sub on_plugin_import (&) {
414 push \@_DANCER2_IMPORT_TIME_SUBS, \$_[0];
415 }
416  
417 sub register_hook { goto &plugin_hooks }
418  
419 sub plugin_setting { Carp::croak "DEPRECATED: Plugin DSL method 'plugin_setting'. "
420 . "Please use '\\\$self->config' instead\n" };
421  
422 sub plugin_args {
423 Carp::carp "Plugin DSL method 'plugin_args' is deprecated. "
424 . "Use '\\\@_' instead'.\n";
425  
426 \@_;
427 }
428 }
429 END
430  
431 38 ▶ $no_dsl->{$caller} or eval <<"END"; ## no critic
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$no_dsl->{$caller} or eval qq[ {\n package $caller;\n\n # FIXME: AUTOLOAD might pick up on this\n sub dancer_app {\n Carp::croak "DEPRECATED: Plugin DSL method 'dancer_app'. "\n . "Please use '\\\$self->app' instead'.\n";\n }\n\n # FIXME: AUTOLOAD might pick up on this\n sub request {\n Carp::croak "DEPRECATED: Plugin DSL method 'request'. "\n . "Please use '\\\$self->app->request' instead'.\n";\n }\n\n # FIXME: AUTOLOAD might pick up on this\n sub var {\n Carp::croak "DEPRECATED: Plugin DSL method 'var'. "\n . "Please use '\\\$self->app->request->var' instead'.\n";\n }\n\n # FIXME: AUTOLOAD might pick up on this\n sub hook {\n Carp::croak "DEPRECATED: Plugin DSL method 'hook'. "\n . "Please use '\\\$self->app->add_hook' instead'.\n";\n }\n\n }\n] 1 37 38
432 {
433 package $caller;
434  
435 # FIXME: AUTOLOAD might pick up on this
436 sub dancer_app {
437 Carp::croak "DEPRECATED: Plugin DSL method 'dancer_app'. "
438 . "Please use '\\\$self->app' instead'.\n";
439 }
440  
441 # FIXME: AUTOLOAD might pick up on this
442 sub request {
443 Carp::croak "DEPRECATED: Plugin DSL method 'request'. "
444 . "Please use '\\\$self->app->request' instead'.\n";
445 }
446  
447 # FIXME: AUTOLOAD might pick up on this
448 sub var {
449 Carp::croak "DEPRECATED: Plugin DSL method 'var'. "
450 . "Please use '\\\$self->app->request->var' instead'.\n";
451 }
452  
453 # FIXME: AUTOLOAD might pick up on this
454 sub hook {
455 Carp::croak "DEPRECATED: Plugin DSL method 'hook'. "
456 . "Please use '\\\$self->app->add_hook' instead'.\n";
457 }
458  
459 }
460 END
461  
462 38 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 38 38
463  
464 {
465 ## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
466 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
467 23 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
468 38 ▶ *{"${caller}::dsl"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
469 35 my $app_dsl_cb = _find_consumer($caller);
470 21 ▶ return $app_dsl_cb ? $app_dsl_cb->() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$app_dsl_cb ? : 13 1 14
471 38 };
472 }
473  
474 38 return map { [ $_ => { class => $caller } ] }
475 qw/ plugin_keywords plugin_hooks /;
476 }
477  
478 32 ▶sub _find_consumer {
Subroutine _find_consumer: called CC=5 SCAR=16.1
479 48 my %skip = map +( $_ => 1 ), @_;
480 29 my $class;
481  
482 ## no critic qw(ControlStructures::ProhibitCStyleForLoops)
483 130 ▶ for ( my $i = 1; my $caller = caller($i); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = caller $i) 97 6 103
484 116 ▶ next if $skip{$caller};
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$caller} 10 92 102
485 90 ▶ next if eval { $caller->isa('Dancer2::Plugin') };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $caller->isa("Dancer2::Plugin") } } 36 53 89
486 62 ▶ $class = $caller->can('dsl')
Branch: 1 branch
BranchTrueFalseTotal
$class = $caller->can("dsl") and last 17 39 56
487 and last;
488 }
489  
490 # If you use a Dancer2 plugin outside a Dancer App, this fails.
491 # It also breaks a bunch of the tests. -- SX
492 #$class
493 # or croak('Could not find Dancer2 app');
494  
495 25 return $class;
496 }
497  
498 # This has to be called for now at the end of every plugin package, in order to
499 # map the keywords of the associated app to the plugin, so that these keywords
500 # can be called from within the plugin code. This function is deprecated, as
501 # it's tied to the old plugin system. It's kept here for backcompat reason, but
502 # should go away with the old plugin system.
503 41 ▶sub register_plugin {
Subroutine register_plugin: called CC=1 SCAR=0.0
Pod undocumented
504  
505 18 my $plugin_module = caller(1);
506  
507 # if you ask yourself why we do the injection in the plugin
508 # module namespace every time the plugin is used, and not only
509 # once, it's because it can be used by different app that could
510 # have a different DSL with a different list of keywords.
511  
512 17 my $_DANCER2_IMPORT_TIME_SUBS = $plugin_module->_DANCER2_IMPORT_TIME_SUBS;
513 23 ▶ unshift(@$_DANCER2_IMPORT_TIME_SUBS, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
514 17 my $app_dsl_cb = _find_consumer($plugin_module);
515  
516 # Here we want to verify that "register_plugin" compat keyword
517 # was in fact only called from an app.
518 17 ▶ $app_dsl_cb
Branch: 1 branch
BranchTrueFalseTotal
$app_dsl_cb or Carp::croak("I could not find a Dancer App for this plugin") 11 3 14
519 or Carp::croak(
520 'I could not find a Dancer App for this plugin');
521  
522 17 my $dsl = $app_dsl_cb->();
523  
524 17 foreach my $keyword ( keys %{ $dsl->dsl_keywords} ) {
525 # if not yet defined, inject the keyword in the plugin
526 # namespace, but make sure the code will always get the
527 # coderef from the right associated app, because one plugin
528 # can be used by multiple apps. Note that we remove the
529 # first parameter (plugin instance) from what we pass to
530 # the keyword implementation of the App
531 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
532 $plugin_module->can($keyword)
533 745 ▶ or *{"${plugin_module}::$keyword"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
534 $_[0]
535 14 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 7 4 11
536 11 my $cb = shift()->app->name->can($keyword);
537 11 $cb->(@_);
538 }
539 : $app_dsl_cb->(@_);
540 900 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$plugin_module->can($keyword) or *{"${plugin_module}::$keyword";} = sub { $_[0] ? do { my $cb = (shift())->app->name->can($keyword); &$cb(@_) } : &$app_dsl_cb(@_); } 154 740 894
541 }
542 16 });
543 }
544  
545 148 ▶sub _exporter_expand_sub {
Subroutine _exporter_expand_sub: called CC=4 SCAR=13.9
546 131 my( $plugin, $name, $args, $global ) = @_;
547 131 my $class = $args->{class};
548  
549 131 ▶ return _exported_plugin_keywords($plugin,$class)
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "plugin_keywords" 39 92 131
550 if $name eq 'plugin_keywords';
551  
552 93 ▶ return _exported_plugin_hooks($class)
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "plugin_hooks" 40 52 92
553 if $name eq 'plugin_hooks';
554  
555 55 ▶ $exported_app->{ $global->{'into'} }
Branch: 1 branch
BranchTrueFalseTotal
$$exported_app{$global->{"into"}} or Carp::croak("Specific subroutines cannot be exported from plugin") 52 2 54
556 or Carp::croak('Specific subroutines cannot be exported from plugin');
557  
558 # otherwise, we're exporting a keyword
559  
560 55 my $p = $args->{plugin};
561 55 my $sub = $p->keywords->{$name};
562 48 ▶ return $name => sub(@) {
Subroutine __ANON__: called CC=1 SCAR=0.0
563 # localize the plugin so we can get it later
564 46 local $CUR_PLUGIN = $p;
565 46 $sub->($p,@_);
566 }
567 55 }
568  
569 # "There's a good reason for this, I swear!"
570 # -- Sawyer X
571 # basically, if someone adds a hook to the app directly
572 # that needs to access a DSL that needs the current object
573 # (such as "plugin_setting"),
574 # that object needs to be available
575 # So:
576 # we override App's "add_hook" to provide a register a
577 # different hook callback, that closes over the plugin when
578 # it's available, relocalizes it when the callback runs and
579 # after localizing it, calls the original hook callback
580 {
581 ## no critic;
582 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
583 23 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
584 23 my $orig_cb = Dancer2::Core::App->can('add_hook');
585 11 ▶ $orig_cb and *{'Dancer2::Core::App::add_hook'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
586 15 my ( $app, $hook ) = @_;
587  
588 15 ▶ my $hook_code = Scalar::Util::blessed($hook) ? $hook->code : $hook->{code};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($hook) ? : 13 2 15
589 15 my $plugin = $CUR_PLUGIN;
590  
591 24 ▶ $hook->{'code'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
592 21 local $CUR_PLUGIN = $plugin;
593 21 $hook_code->(@_);
594 15 };
595  
596 15 $orig_cb->(@_);
597 23 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$orig_cb and *{"Dancer2::Core::App::add_hook";} = sub { my($app, $hook) = @_; my $hook_code = builtin::blessed($hook) ? $hook->code : $hook->{"code"}; my $plugin = $CUR_PLUGIN; $hook->{"code"} = sub { local $CUR_PLUGIN = $plugin; &$hook_code(@_); } ; &$orig_cb(@_); } 11 12 23
598 }
599  
600  
601 # define the exported 'plugin_keywords'
602 47 ▶sub _exported_plugin_keywords{
Subroutine _exported_plugin_keywords: called CC=1 SCAR=0.0
603 40 my( $plugin, $class ) = @_;
604  
605 19 ▶ return plugin_keywords => sub(@) {
Subroutine __ANON__: called CC=6 SCAR=18.3
606 17 ▶ while( my $name = shift @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @_) 22 15 37
607 ## no critic
608 24 ▶ my $sub = is_coderef($_[0])
Branch: 2 branches
BranchTrueFalseTotal
is_coderef($_[0]) ? : 1 21 22
ref $name ? : 0 21 21
609 ? shift @_
610 : eval '\&'.$class."::" . ( ref $name ? $name->[0] : $name );
611 22 ▶ $class->keywords->{$_} = $sub for ref $name ? @$name : $name;
Branch: 1 branch
BranchTrueFalseTotal
ref $name ? : 0 22 22
612 }
613 }
614 40 }
615  
616 41 ▶sub _exported_plugin_hooks {
Subroutine _exported_plugin_hooks: called CC=1 SCAR=0.0
617 38 my $class = shift;
618 7 ▶ return plugin_hooks => sub (@) { $class->add_hooks(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
619 38 }
620  
621 23 1;
622  
623 __END__
« lib/Dancer2/Core/Role/SessionFactory.pm ↑ Summary lib/Dancer2/Core/HTTP.pm »