File Coverage

blib/lib/Dancer2/Core/Hook.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Manipulate hooks with Dancer2
2             $Dancer2::Core::Hook::VERSION = '0.400000';
3             use Moo;
4 148     148   137045 use Dancer2::Core::Types;
  148         19517  
  148         878  
5 148     148   49949 use Carp;
  148         343  
  148         1208  
6 148     148   1222373  
  148         391  
  148         38077  
7             has name => (
8             is => 'rw',
9             isa => Str,
10             required => 1,
11             coerce => sub {
12             my ($hook_name) = @_;
13              
14             # XXX at the moment, we have a filer position named "before_template".
15             # this one is renamed "before_template_render", so we need to alias it.
16             # maybe we need to deprecate 'before_template' to enforce the use
17             # of 'hook before_template_render => sub {}' ?
18             $hook_name = 'before_template_render'
19             if $hook_name eq 'before_template';
20             return $hook_name;
21             },
22             );
23              
24             has code => (
25             is => 'ro',
26             isa => CodeRef,
27             required => 1,
28             coerce => sub {
29             my ($hook) = @_;
30             sub {
31             my $res;
32             eval { $res = $hook->(@_) };
33             croak "Hook error: $@" if $@;
34             return $res;
35             };
36             },
37             );
38              
39             1;
40              
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             Dancer2::Core::Hook - Manipulate hooks with Dancer2
49              
50             =head1 VERSION
51              
52             version 0.400000
53              
54             =head1 SYNOPSIS
55              
56             # inside a plugin
57             use Dancer2::Core::Hook;
58             Dancer2::Core::Hook->register_hooks_name(qw/before_auth after_auth/);
59              
60             =head1 METHODS
61              
62             =head2 register_hook ($hook_name, $code)
63              
64             hook 'before' => sub {...};
65              
66             Attaches a hook at some point.
67              
68             =head2 register_hooks_name
69              
70             Add a new hook name, so application developers can insert some code at this point.
71              
72             package My::Dancer2::Plugin;
73             Dancer2::Core::Hook->instance->register_hooks_name(qw/before_auth after_auth/);
74              
75             =head2 execute_hook
76              
77             Execute a hook
78              
79             =head2 get_hooks_for
80              
81             Returns the list of coderef registered for a given position
82              
83             =head2 hook_is_registered
84              
85             Test if a hook with this name has already been registered.
86              
87             =head1 AUTHOR
88              
89             Dancer Core Developers
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is copyright (c) 2022 by Alexis Sukrieh.
94              
95             This is free software; you can redistribute it and/or modify it under
96             the same terms as the Perl 5 programming language system itself.
97              
98             =cut