File Coverage

blib/lib/Dancer/Plugin.pm
Criterion Covered Total %
statement 76 80 95.0
branch 5 6 83.3
condition 5 7 71.4
subroutine 19 21 90.4
pod 6 9 66.6
total 111 123 90.2


line stmt bran cond sub pod time code
1             package Dancer::Plugin;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: helper for writing Dancer plugins
4             $Dancer::Plugin::VERSION = '1.3520';
5 26     26   57194 use strict;
  26         73  
  26         772  
6 26     26   151 use warnings;
  26         67  
  26         636  
7 26     26   135 use Carp;
  26         67  
  26         1473  
8              
9 26     26   172 use base 'Exporter';
  26         51  
  26         2770  
10 26     26   212 use Dancer::Config 'setting';
  26         95  
  26         1254  
11 26     26   189 use Dancer::Hook;
  26         71  
  26         806  
12 26     26   217 use Dancer::Factory::Hook;
  26         81  
  26         803  
13 26     26   171 use Dancer::Exception qw(:all);
  26         68  
  26         3535  
14              
15 26     26   206 use base 'Exporter';
  26         77  
  26         1201  
16 26     26   187 use vars qw(@EXPORT);
  26         66  
  26         18199  
17              
18             @EXPORT = qw(
19             add_hook
20             register
21             register_plugin
22             plugin_setting
23             register_hook
24             execute_hooks
25             execute_hook
26             plugin_args
27             );
28              
29             sub register($&);
30              
31             my $_keywords = {};
32              
33 18     18 0 209 sub add_hook { Dancer::Hook->new(@_) }
34              
35 0     0 1 0 sub plugin_args { (undef, @_) }
36              
37             sub plugin_setting {
38 4     4 1 11 my $plugin_orig_name = caller();
39 4         15 (my $plugin_name = $plugin_orig_name) =~ s/Dancer::Plugin:://;
40              
41 4   100     11 return setting('plugins')->{$plugin_name} ||= {};
42             }
43              
44             sub register_hook {
45 2     2 1 643 Dancer::Factory::Hook->instance->install_hooks(@_);
46             }
47              
48             sub execute_hooks {
49 0     0 1 0 Dancer::Deprecation->deprecated(reason => "use 'execute_hook'",
50             version => '1.3098',
51             fatal => 0);
52 0         0 Dancer::Factory::Hook->instance->execute_hooks(@_);
53             }
54              
55             sub execute_hook {
56 1     1 0 17 Dancer::Factory::Hook->instance->execute_hooks(@_);
57             }
58              
59             sub register($&) {
60 30     30 1 6581 my ($keyword, $code) = @_;
61 30         116 my $plugin_name = caller();
62              
63 30 100       264 $keyword =~ /^[a-zA-Z_]+[a-zA-Z0-9_]*$/
64             or raise core_plugin => "You can't use '$keyword', it is an invalid name"
65             . " (it should match ^[a-zA-Z_]+[a-zA-Z0-9_]*$ )";
66              
67 29 100       139 if (
68 1944         2827 grep { $_ eq $keyword }
69 1944         2529 map { s/^(?:\$|%|&|@|\*)//; $_ }
  1944         2646  
70             (@Dancer::EXPORT, @Dancer::EXPORT_OK)
71             ) {
72 2         12 raise core_plugin => "You can't use '$keyword', this is a reserved keyword";
73             }
74 27         224 while (my ($plugin, $keywords) = each %$_keywords) {
75 3 50       10 if (grep { $_->[0] eq $keyword } @$keywords) {
  3         22  
76 0         0 raise core_plugin => "You can't use $keyword, "
77             . "this is a keyword reserved by $plugin";
78             }
79             }
80              
81 27   50     210 $_keywords->{$plugin_name} ||= [];
82 27         53 push @{$_keywords->{$plugin_name}}, [$keyword => $code];
  27         138  
83             }
84              
85             sub register_plugin {
86 27   66 27 1 354 my ($application) = shift || caller(1);
87 27         107 my ($plugin) = caller();
88              
89 27         90 my @symbols = set_plugin_symbols($plugin);
90             {
91 26     26   234 no strict 'refs';
  26         76  
  26         4191  
  27         61  
92             # tried to use unshift, but it yields an undef warning on $plugin (perl v5.12.1)
93 27         51 @{"${plugin}::ISA"} = ('Dancer::Plugin', @{"${plugin}::ISA"});
  27         499  
  27         168  
94             # this works because Dancer::Plugin already ISA Exporter
95 27         95 push @{"${plugin}::EXPORT"}, @symbols;
  27         193  
96             }
97 27         109 return 1;
98             }
99              
100             sub set_plugin_symbols {
101 27     27 0 118 my ($plugin) = @_;
102              
103 27         59 for my $keyword (@{$_keywords->{$plugin}}) {
  27         76  
104 27         71 my ($name, $code) = @$keyword;
105             {
106 26     26   234 no strict 'refs';
  26         61  
  26         2917  
  27         58  
107 27         57 *{"${plugin}::${name}"} = $code;
  27         243  
108             }
109             }
110 27         75 return map { $_->[0] } @{$_keywords->{$plugin}};
  27         128  
  27         73  
111             }
112              
113             1;
114              
115             __END__