Muster/Hooks.pm

filter: Statementstmt 43.3% 36 / 83 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 10 / 15 Podpod 100% 5 / 5 totaltotal 47.6% 51 / 107 CC 8 SCAR 25.8
CC
8
Cov
41%
init
42.0
run_hooks
6.0
run_filters
6.0
CRAP 13.2
« Muster/Scanner.pm ↑ Summary Muster/PagesHelper.pm »
1 package Muster::Hooks;
2 24 $Muster::Hooks::VERSION = '0.96';
3 #ABSTRACT: Muster::Hooks - scanning and processing hooks
4 =head1 NAME
5  
6 Muster::Hooks - scanning and processing hooks
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 DESCRIPTION
13  
14 Content Management System
15 scanning and processing hooks
16  
17 =cut
18  
19 24 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 24 ▶use Muster::MetaDb;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 24 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 24 ▶use Muster::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 24 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 24 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 24 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 24 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 24 ▶use Module::Pluggable search_path => ['Muster::Hook'], instantiate => 'new';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 PACKAGE CONSTANTS
31  
32 =over
33  
34 =item $PHASE_SCAN
35  
36 Hooks are currently in scanning phase where pages are scanned for meta-data.
37  
38 =item $PHASE_BUILD
39  
40 Hooks are currently in build/assemble phase, where the pages are read and built.
41  
42 =item $PHASE_FILTER
43  
44 Hooks are currently in filter phase, where the page has already been converted to HTML, and needs post-processing.
45  
46 =back
47  
48 =cut
49  
50 24 const our $PHASE_SCAN => 'scan';
51 24 const our $PHASE_POST_SCAN => 'post_scan';
52 24 const our $PHASE_BUILD => 'build';
53 24 const our $PHASE_FILTER => 'filter';
54  
55 =head1 METHODS
56  
57 =head2 init
58  
59 Set the defaults for the object if they are not defined already.
60  
61 =cut
62 0 ▶sub init {
Subroutine init: not called CC=6 SCAR=37.4
Pod documented
63 0 my $self = shift;
64 0 my $config = shift;
65  
66 # connect to the metadb because some hooks might need the info
67 0 $self->{metadb} = Muster::MetaDb->new(%{$config});
68 0 $self->{metadb}->init();
69  
70 # Hooks are defined by Muster::Hook objects. The Pluggable module will find
71 # all possible hooks but the config will have defined a subset in the order
72 # we want to apply them.
73 # The way this is done is that we call "register" for the hooks in that order,
74 # and while a given hook object may have more than one callback, at least
75 # all of the hooks for THAT module will come after the module before, etc.
76 0 $self->{hooks} = {};
77 0 $self->{hookorder} = [];
78 0 my %phooks = ();
79 0 foreach my $ph ($self->plugins())
80 {
81 0 $phooks{ref $ph} = $ph;
82 }
83 0 foreach my $hookmod (@{$config->{hooks}})
84 {
85 0 ▶ if ($phooks{$hookmod})
Branch: 1 branch
BranchTrueFalseTotal
if ($phooks{$hookmod}) { } 0 0 0
86 {
87 0 $phooks{$hookmod}->register($self,$config);
88 }
89 else
90 {
91 0 warn "Hook '$hookmod' does not exist";
92 }
93 }
94  
95 # Filters use register_filter instead
96 # This will be a no-op for most hooks.
97 0 $self->{filters} = {};
98 0 $self->{filterorder} = [];
99 0 foreach my $mod (@{$config->{hooks}})
100 {
101 0 ▶ if ($phooks{$mod})
Branch: 1 branch
BranchTrueFalseTotal
if ($phooks{$mod}) { } 0 0 0
102 {
103 0 $phooks{$mod}->register_filter($self,$config);
104 }
105 else
106 {
107 0 warn "Filter '$mod' does not exist";
108 }
109 }
110  
111 0 return $self;
112 } # init
113  
114 =head2 add_hook
115  
116 Add a hook.
117  
118 =cut
119 0 ▶sub add_hook {
Subroutine add_hook: not called CC=1 SCAR=6.9
Pod documented
120 0 my ($self, $name, $call) = @_;
121 0 $self->{hooks}->{$name} = $call;
122 0 push @{$self->{hookorder}}, $name;
123 0 return $self;
124 } # add_hook
125  
126 =head2 run_hooks
127  
128 Run the hooks over the given leaf.
129 Leaf must already be created and reclassified.
130 The "phase" flag says what phase we are in (e.g. scanning)
131
132 $leaf = $self->run_hooks(leaf=>$leaf,phase=>$phase);
133  
134 =cut
135  
136 0 ▶sub run_hooks {
Subroutine run_hooks: not called CC=2 SCAR=17.9
Pod documented
137 0 my $self = shift;
138 0 my %args = @_;
139  
140 0 my $leaf = $args{leaf};
141  
142 0 foreach my $hn (@{$self->{hookorder}})
143 {
144 0 $leaf = $self->{hooks}->{$hn}(%args);
145 }
146  
147 0 return $leaf;
148 } # run_hooks
149  
150 =head2 add_filter
151  
152 Add a post-processing filter.
153  
154 =cut
155 0 ▶sub add_filter {
Subroutine add_filter: not called CC=1 SCAR=6.9
Pod documented
156 0 my ($self, $name, $call) = @_;
157 0 $self->{filters}->{$name} = $call;
158 0 push @{$self->{filterorder}}, $name;
159 0 return $self;
160 } # add_filter
161  
162 =head2 run_filters
163  
164 Run post-processing filters over already-rendered HTML.
165
166 $html = $self->run_filters(html=>$html,phase=>$phase);
167  
168 =cut
169  
170 0 ▶sub run_filters {
Subroutine run_filters: not called CC=2 SCAR=17.9
Pod documented
171 0 my $self = shift;
172 0 my %args = @_;
173  
174 0 my $html = $args{html};
175  
176 0 foreach my $hn (@{$self->{filterorder}})
177 {
178 0 $html = $self->{filters}->{$hn}(%args);
179 }
180  
181 0 return $html;
182 } # run_filters
183  
184 24 1; # End of Muster::Hooks
185 __END__
« Muster/Scanner.pm ↑ Summary Muster/PagesHelper.pm »