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