blib/lib/Catalyst/Plugin/Hooks.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 72 CC 2 SCAR 10.6
CC
2
Cov
0%
initialize_action
6.0
remove_all_hooks
2.0
BEGIN
2.0
CRAP 2.9
1 package Catalyst::Plugin::Hooks;
2  
3 use strict;
4 use warnings;
5  
6 use NEXT;
7 use Carp;
8  
9 our $VERSION = '0.03';
10  
11 my %actions;
12 @actions{ qw(
13 handle_request
14 prepare
15 prepare_request
16 prepare_connection
17 prepare_query_parameters
18 prepare_headers
19 prepare_cookies
20 prepare_path
21 prepare_body
22 prepare_body_parameters
23 prepare_parameters
24 prepare_uploads
25 prepare_action
26 dispatch
27 finalize
28 finalize_uploads
29 finalize_error
30 finalize_headers
31 finalize_cookies
32 finalize_body
33 ) } = ();
34  
35 for my $action ( keys %actions ) {
36 no strict 'refs';
37 *{"add_". $action ."_hook"} = sub {
38 my ( $c, $hook ) = @_;
39 croak "add_". $action ."_hook( CODE )" unless ref $hook eq "CODE";
40 __PACKAGE__->initialize_action( $action )
41 unless $action->{initialized};
42  
43 push @{ $actions{$action}->{before} }, $hook;
44 };
45 *{"add_before_". $action ."_hook"} = \*{"add_". $action ."_hook"};
46  
47 *{"add_after_". $action ."_hook"} = sub {
48 my ( $c, $hook ) = @_;
49 croak "add_after_". $action ."_hook( CODE )" unless ref $hook eq "CODE";
50 __PACKAGE__->initialize_action( $action )
51 unless $action->{initialized};
52  
53 push @{ $actions{$action}->{after} }, $hook;
54 };
55  
56 *{"remove_all_". $action ."_hooks"} = sub {
57 $actions{$action}->{before} = [];
58 $actions{$action}->{after} = [];
59 };
60 }
61  
62 sub initialize_action {
63 my ( $self, $action ) = @_;
64  
65 no strict 'refs';
66 ### Had to use eval for NEXT::$action
67 eval q/ sub /. $action .q/ {
68 my $c = shift;
69 my $action = '/. $action .q/';
70  
71 for my $hook ( @{ $actions{$action}->{before} } ) {
72 $hook->( $c, @_ );
73 }
74 $c->NEXT::/. $action .q/(@_);
75 for my $hook ( @{ $actions{$action}->{after} } ) {
76 $hook->( $c, @_ );
77 }
78 } /;
79 die $@ if $@;
80 $actions{ $action }->{initialized} = 1;
81 }
82  
83 sub remove_all_hooks {
84 my $self = shift;
85  
86 for my $action ( values %actions ) {
87 $action->{before} = [];
88 $action->{after} = [];
89 }
90 }
91  
92  
93 1
94  
95 __END__