lib/eris/role/pluggable.pm

filter: Statementstmt 41.9% 26 / 62 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod n/a totaltotal 34.7% 32 / 92 CC 12 SCAR 42.3
CC
12
Cov
33%
_build_plugins
130.7
__ANON__
2.0
__ANON__
2.0
CRAP 68.9
« lib/eris/log/context/attacks/url.pm ↑ Summary lib/eris/log/context/pfSense/filterlog.pm »
1 package eris::role::pluggable;
2 # ABSTRACT: Implements the plumbing for an object to support plugins
3  
4 8 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Types::Standard qw(ArrayRef HashRef InstanceOf Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = '0.008'; # VERSION
11  
12  
13 8 has namespace => (
14 is => 'ro',
15 isa => Str,
16 lazy => 1,
17 builder => '_build_namespace',
18 );
19  
20  
21 has search_path => (
22 is => 'ro',
23 isa => ArrayRef[Str],
24 lazy => 1,
25 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 8 );
27  
28  
29 has disabled => (
30 is => 'ro',
31 isa => ArrayRef[Str],
32 lazy => 1,
33 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 8 );
35  
36  
37 8 has 'loader' => (
38 is => 'ro',
39 isa => InstanceOf['Module::Pluggable::Object'],
40 lazy => 1,
41 builder => '_build_loader',
42 );
43  
44 sub _build_loader {
45 0 ▶ my ($self) = @_;
Subroutine _build_loader: not called CC=1 SCAR=6.9
46 my $loader = Module::Pluggable::Object->new(
47 0 search_path => [ $self->namespace, @{$self->search_path} ],
48 except => $self->disabled,
49 require => 1,
50 );
51 0 return $loader;
52 }
53  
54  
55 has 'plugins_config' => (
56 is => 'ro',
57 isa => HashRef,
58 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 8 init_arg => 'config',
60 );
61  
62  
63 8 has 'plugins' => (
64 is => 'ro',
65 isa => ArrayRef,
66 lazy => 1,
67 builder => '_build_plugins',
68 );
69  
70 sub _build_plugins {
71 0 ▶ my $self = shift;
Subroutine _build_plugins: not called CC=12 SCAR=48.7
72 0 my @plugins = ();
73  
74 # Make short hand configs possible
75 0 my %config = ();
76 0 ▶ my @search = grep { defined && length } ($self->namespace, @{ $self->search_path });
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 0 0 0
77 0 foreach my $alias ( keys %{ $self->plugins_config } ) {
78 # Copy into our local hash
79 0 $config{$alias} = $self->plugins_config->{$alias};
80 # If we find our search path as a prefix, skip
81 0 ▶ next if any { /^$alias/ } @search;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /^$alias/; } , @search) 0 0 0
82 0 foreach my $prefix (@search) {
83 0 my $class = join('::', $prefix, $alias);
84 0 ▶ next if exists $config{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config{$class} 0 0 0
85 # Copy the config
86 0 $config{$class} = $config{$alias};
87 }
88 }
89 0 foreach my $class ( $self->loader->plugins ) {
90 eval {
91 0 ▶ my $opts = $config{$class} || {};
Condition0%0/2
$config{$class} || {}
l!l
00
MC/DC0%0/1
$config{$class} || {}
$config{$class}
Truth table0%0/2
A
A= $config{$class}
Aresult
11
00
92 0 $opts->{namespace} = $self->namespace;
93 0 push @plugins, $class->new(%{ $opts });
94 0 1;
95 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $opts = $config{$class} || {}; $opts->{"namespace"} = $self->namespace; push @plugins, $class->new(%{$opts;}); 1 } } or do { my $err = $@; no strict 'refs'; my $warn_var = sprintf('%s::SuppressWarnings', $class); my $suppress_warnings = eval "$$warn_var" || 0; warn $err unless $suppress_warnings } 0 0 0
96 0 my $err = $@;
97 ## no critic
98 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 0 my $warn_var = sprintf '%s::SuppressWarnings', $class;
100 0 ▶ my $suppress_warnings = eval "$$warn_var" || 0;
Condition0%0/2
eval "$$warn_var" || 0
l!l
00
MC/DC0%0/1
eval "$$warn_var" || 0
eval "$$warn_var"
Truth table0%0/2
A
A= eval "$$warn_var"
Aresult
11
00
101 0 ▶ warn $err unless $suppress_warnings;
Branch: 1 branch
BranchTrueFalseTotal
unless $suppress_warnings 0 0 0
102 ## use critic
103 };
104 }
105 return [
106 0 ▶ sort { $a->priority <=> $b->priority || $a->name cmp $b->name }
Branch: 1 branch
BranchTrueFalseTotal
$a->priority <=> $b->priority or $a->name cmp $b->name 0 0 0
107 0 grep { $_->enabled }
108 @plugins
109 ];
110 }
111  
112  
113 8 1;
114  
115 __END__
116  
117 =pod
118  
119 =encoding UTF-8
120  
121 =head1 NAME
122  
123 eris::role::pluggable - Implements the plumbing for an object to support plugins
124  
125 =head1 VERSION
126  
127 version 0.008
128  
129 =head1 SYNOPSIS
130  
131 Implements helpers around creating plugins to make things easier to
132 plug.
133  
134 package eris::schemas;
135  
136 use Moo;
137 with qw(eris::role::pluggable);
138  
139 sub _build_namespace { 'eris::schema' }
140  
141 sub find {
142 my ($self,$log) = @_;
143  
144 foreach my $p ($self->plugins) {
145 return $p->name if $p->match_log($log);
146 }
147 }
148  
149 package main;
150  
151 my $schemas = eris::schemas->new(
152 search_path => [ qw( my::app::schema ) ],
153 disabled => [ qw( eris::schema::syslog ) ],
154 config => {
155 access => { index_name => 'www-%Y.%m.%d' }
156 }
157 );
158  
159 =head1 ATTRIBUTES
160  
161 =head2 namespace
162  
163 Primary namespace for the plugins for this object. No default provided, you
164 must implement C<_build_namespace> in your plugin.
165  
166 =head2 search_path
167  
168 An ArrayRef of additional namespaces or directories to search to load our
169 plugins. Default is an empty array.
170  
171 =head2 disabled
172  
173 An ArrayRef of explicitly disallowed package namespaces to prevent loading.
174 Default is an empty array.
175  
176 =head2 loader
177  
178 An instance of L<Module::Pluggable::Object> to use to locate plugins.
179  
180 You shouldn't need this considering the options available, but always nice
181 to have the option to override it with C<_build_loader>.
182  
183 B<This plugin class expects the loader's plugin() call to return a list of
184 class names, not instantiated objects.>
185  
186 =head2 plugins_config
187  
188 A HashRef of configs for passing along to our plugins. The init arg for this
189 parameter is 'config' to simplify creation and config files.
190  
191 Special considerations are taken when processing the hash. The C<namespace> and C<search_path> are
192 automatically prepended to all keys to allow pretty config. This means I can pass a config like this:
193  
194 my $schema = eris::schemas->new(
195 search_path => [qw(my::app::schema)],
196 config => {
197 syslog => { enabled => 0 },
198 eris::schema::syslog => { enabled => 1 },
199 access => { enabled => 0 },
200 },
201 );
202  
203 This will expand the config to:
204  
205 config => {
206 my::app::schema::access => { enabled => 0 },
207 eris::schema::access => { enabled => 0 },
208 my::app::schema::syslog => { enabled => 0 },
209 eris::schema::syslog => { enabled => 1 },
210 },
211  
212 The explicit config for 'eris::schema::syslog' is retained.
213  
214 =head2 plugins
215  
216 The priority sorted list of plugin objects found by the loader. The C<plugins> call
217 expects the C<loader> function to return a list of class names, not objects.
218  
219 =head1 SEE ALSO
220  
221 L<eris::role::plugin>, L<eris::schemas>, L<eris::log::contexts>, L<eris::log::decoders>
222  
223 =head1 AUTHOR
224  
225 Brad Lhotsky <brad@divisionbyzero.net>
226  
227 =head1 COPYRIGHT AND LICENSE
228  
229 This software is Copyright (c) 2015 by Brad Lhotsky.
230  
231 This is free software, licensed under:
232  
233 The (three-clause) BSD License
234  
235 =cut
« lib/eris/log/context/attacks/url.pm ↑ Summary lib/eris/log/context/pfSense/filterlog.pm »