blib/lib/KelpX/Hooks.pm

filter: Statementstmt 100% 39 / 39 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 98.2% 57 / 58 CC 5 SCAR 5.4
CC
5
Cov
98%
hook
3.0
__ANON__
2.1
__ANON__
2.0
CRAP 1.7
↑ Summary
1 package KelpX::Hooks;
2 2 $KelpX::Hooks::VERSION = '1.02';
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our @EXPORT = qw(
11 hook
12 );
13  
14 sub hook
15 {
16 6 ▶ my ($subname, $decorator) = @_;
Subroutine hook: called CC=3 SCAR=11.0
Pod documented
17 6 my $package = caller;
18  
19 6 ▶ croak "Hooking build() method is forbidden"
Branch: 1 branch
BranchTrueFalseTotal
if $subname eq "build" 1 5 6
20 if $subname eq "build";
21  
22 5 my $build_method = $package->can("build");
23 5 ▶ croak "Can't hook $subname: no build() method in $package"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $build_method 1 4 5
24 unless defined $build_method;
25  
26 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 *{"${package}::build"} = sub {
30 4 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
31  
32 4 my $hooked_method = $package->can($subname);
33 4 ▶ croak "Trying to hook $subname, which doesn't exist"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hooked_method 1 3 4
34 unless defined $hooked_method;
35  
36 3 *{"${package}::$subname"} = sub {
37 4 ▶ my ($kelp, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
38  
39 return wantarray
40 ?
41 4 ▶ $decorator->($hooked_method, $kelp, @args)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
42 :
43 scalar $decorator->($hooked_method, $kelp, @args);
44 3 };
45  
46 3 goto $build_method;
47 4 };
48 4 return;
49 }
50  
51 2 1;
52  
53 __END__
54  
55 =head1 NAME
56  
57 KelpX::Hooks - Override any method in your Kelp application
58  
59 =head1 SYNOPSIS
60  
61 # in your Kelp application
62 use KelpX::Hooks;
63  
64 # and then...
65 hook "template" => sub {
66 return "No templates for you!";
67 };
68  
69 =head1 DESCRIPTION
70  
71 This module allows you to override methods in your Kelp application class. The provided L</hook> method can be compared to Moose's C<around>, and it mimics its interface. The difference is in how and when the replacement of the actual method occurs.
72  
73 The problem here is that Kelp's modules are modifying the symbol table for the module at the runtime, which makes common attempts to change their methods` behavior futile. You can't override them, you can't change them with method modifiers, you can only replace them with different methods.
74  
75 This module fights the symbol table magic with more symbol table magic. It will replace any method with your anonymous subroutine after the application is built and all the modules have been loaded.
76  
77 =head2 EXPORT
78  
79 =head3 hook
80  
81 hook "sub_name" => sub {
82 my ($original_sub, $self, @arguments) = @_;
83  
84 # your code, preferably do this at some point:
85 return $self->$original_sub(@arguments);
86 };
87  
88 Allows you to provide your own subroutine in place of the one specified. The first argument is the subroutine that's being replaced. It won't be run at all unless you call it explicitly.
89  
90 Please note that Kelp::Less is not supported.
91  
92 =head1 CAVEATS
93  
94 This module works by replacing the build method in symbol tables. Because of this, you cannot hook the build method itself.
95  
96 =head1 SEE ALSO
97  
98 L<Kelp>, L<Moose::Manual::MethodModifiers>
99  
100 =head1 AUTHOR
101  
102 Bartosz Jarzyna, E<lt>bbrtj.pro@gmail.comE<gt>
103  
104 =head1 COPYRIGHT AND LICENSE
105  
106 Copyright (C) 2020 - 2022 by Bartosz Jarzyna
107  
108 This library is free software; you can redistribute it and/or modify
109 it under the same terms as Perl itself.
110  
111  
112 =cut
113  
↑ Summary