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.3514_04'; # TRIAL
5             $Dancer::Plugin::VERSION = '1.351404';
6 26     26   47062 use strict;
  26         59  
  26         629  
7 26     26   120 use warnings;
  26         203  
  26         526  
8 26     26   106 use Carp;
  26         49  
  26         1311  
9              
10 26     26   179 use base 'Exporter';
  26         46  
  26         2363  
11 26     26   277 use Dancer::Config 'setting';
  26         72  
  26         981  
12 26     26   143 use Dancer::Hook;
  26         66  
  26         618  
13 26     26   150 use Dancer::Factory::Hook;
  26         49  
  26         639  
14 26     26   129 use Dancer::Exception qw(:all);
  26         57  
  26         2738  
15              
16 26     26   151 use base 'Exporter';
  26         54  
  26         862  
17 26     26   136 use vars qw(@EXPORT);
  26         61  
  26         14613  
18              
19             @EXPORT = qw(
20             add_hook
21             register
22             register_plugin
23             plugin_setting
24             register_hook
25             execute_hooks
26             execute_hook
27             plugin_args
28             );
29              
30             sub register($&);
31              
32             my $_keywords = {};
33              
34 18     18 0 157 sub add_hook { Dancer::Hook->new(@_) }
35              
36 0     0 1 0 sub plugin_args { (undef, @_) }
37              
38             sub plugin_setting {
39 4     4 1 9 my $plugin_orig_name = caller();
40 4         11 (my $plugin_name = $plugin_orig_name) =~ s/Dancer::Plugin:://;
41              
42 4   100     10 return setting('plugins')->{$plugin_name} ||= {};
43             }
44              
45             sub register_hook {
46 2     2 1 440 Dancer::Factory::Hook->instance->install_hooks(@_);
47             }
48              
49             sub execute_hooks {
50 0     0 1 0 Dancer::Deprecation->deprecated(reason => "use 'execute_hook'",
51             version => '1.3098',
52             fatal => 0);
53 0         0 Dancer::Factory::Hook->instance->execute_hooks(@_);
54             }
55              
56             sub execute_hook {
57 1     1 0 9 Dancer::Factory::Hook->instance->execute_hooks(@_);
58             }
59              
60             sub register($&) {
61 30     30 1 5913 my ($keyword, $code) = @_;
62 30         71 my $plugin_name = caller();
63              
64 30 100       221 $keyword =~ /^[a-zA-Z_]+[a-zA-Z0-9_]*$/
65             or raise core_plugin => "You can't use '$keyword', it is an invalid name"
66             . " (it should match ^[a-zA-Z_]+[a-zA-Z0-9_]*$ )";
67              
68 29 100       88 if (
69 1944         2293 grep { $_ eq $keyword }
70 1944         2104 map { s/^(?:\$|%|&|@|\*)//; $_ }
  1944         2194  
71             (@Dancer::EXPORT, @Dancer::EXPORT_OK)
72             ) {
73 2         11 raise core_plugin => "You can't use '$keyword', this is a reserved keyword";
74             }
75 27         171 while (my ($plugin, $keywords) = each %$_keywords) {
76 3 50       6 if (grep { $_->[0] eq $keyword } @$keywords) {
  3         17  
77 0         0 raise core_plugin => "You can't use $keyword, "
78             . "this is a keyword reserved by $plugin";
79             }
80             }
81              
82 27   50     163 $_keywords->{$plugin_name} ||= [];
83 27         43 push @{$_keywords->{$plugin_name}}, [$keyword => $code];
  27         105  
84             }
85              
86             sub register_plugin {
87 27   66 27 1 605 my ($application) = shift || caller(1);
88 27         92 my ($plugin) = caller();
89              
90 27         75 my @symbols = set_plugin_symbols($plugin);
91             {
92 26     26   195 no strict 'refs';
  26         80  
  26         3330  
  27         51  
93             # tried to use unshift, but it yields an undef warning on $plugin (perl v5.12.1)
94 27         71 @{"${plugin}::ISA"} = ('Dancer::Plugin', @{"${plugin}::ISA"});
  27         425  
  27         123  
95             # this works because Dancer::Plugin already ISA Exporter
96 27         87 push @{"${plugin}::EXPORT"}, @symbols;
  27         126  
97             }
98 27         90 return 1;
99             }
100              
101             sub set_plugin_symbols {
102 27     27 0 53 my ($plugin) = @_;
103              
104 27         39 for my $keyword (@{$_keywords->{$plugin}}) {
  27         85  
105 27         57 my ($name, $code) = @$keyword;
106             {
107 26     26   196 no strict 'refs';
  26         50  
  26         2541  
  27         282  
108 27         60 *{"${plugin}::${name}"} = $code;
  27         185  
109             }
110             }
111 27         53 return map { $_->[0] } @{$_keywords->{$plugin}};
  27         143  
  27         57  
112             }
113              
114             1;
115              
116             __END__