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.3521';
5 26     26   58191 use strict;
  26         111  
  26         768  
6 26     26   165 use warnings;
  26         75  
  26         631  
7 26     26   146 use Carp;
  26         62  
  26         1563  
8              
9 26     26   196 use base 'Exporter';
  26         81  
  26         2710  
10 26     26   201 use Dancer::Config 'setting';
  26         79  
  26         1219  
11 26     26   175 use Dancer::Hook;
  26         63  
  26         732  
12 26     26   217 use Dancer::Factory::Hook;
  26         70  
  26         825  
13 26     26   172 use Dancer::Exception qw(:all);
  26         66  
  26         3408  
14              
15 26     26   200 use base 'Exporter';
  26         63  
  26         1131  
16 26     26   186 use vars qw(@EXPORT);
  26         68  
  26         17536  
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 225 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 14 my $plugin_orig_name = caller();
39 4         16 (my $plugin_name = $plugin_orig_name) =~ s/Dancer::Plugin:://;
40              
41 4   100     13 return setting('plugins')->{$plugin_name} ||= {};
42             }
43              
44             sub register_hook {
45 2     2 1 588 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 12 Dancer::Factory::Hook->instance->execute_hooks(@_);
57             }
58              
59             sub register($&) {
60 30     30 1 6821 my ($keyword, $code) = @_;
61 30         90 my $plugin_name = caller();
62              
63 30 100       267 $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       120 if (
68 1944         2883 grep { $_ eq $keyword }
69 1944         2578 map { s/^(?:\$|%|&|@|\*)//; $_ }
  1944         2654  
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       11 if (grep { $_->[0] eq $keyword } @$keywords) {
  3         19  
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     209 $_keywords->{$plugin_name} ||= [];
82 27         56 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         95 my ($plugin) = caller();
88              
89 27         97 my @symbols = set_plugin_symbols($plugin);
90             {
91 26     26   218 no strict 'refs';
  26         87  
  26         4189  
  27         63  
92             # tried to use unshift, but it yields an undef warning on $plugin (perl v5.12.1)
93 27         48 @{"${plugin}::ISA"} = ('Dancer::Plugin', @{"${plugin}::ISA"});
  27         504  
  27         142  
94             # this works because Dancer::Plugin already ISA Exporter
95 27         91 push @{"${plugin}::EXPORT"}, @symbols;
  27         180  
96             }
97 27         115 return 1;
98             }
99              
100             sub set_plugin_symbols {
101 27     27 0 85 my ($plugin) = @_;
102              
103 27         59 for my $keyword (@{$_keywords->{$plugin}}) {
  27         80  
104 27         70 my ($name, $code) = @$keyword;
105             {
106 26     26   211 no strict 'refs';
  26         73  
  26         2984  
  27         61  
107 27         45 *{"${plugin}::${name}"} = $code;
  27         232  
108             }
109             }
110 27         65 return map { $_->[0] } @{$_keywords->{$plugin}};
  27         139  
  27         76  
111             }
112              
113             1;
114              
115             __END__